You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

449 lines
16 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import java.util.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
// 员工实体类
class Employee {
private String id;
private String name;
private String gender;
private String department;
private String contact;
private String education;
private String role; // "admin" 或 "employee"
private String password;
private double baseSalary; // 基本工资
public Employee(String id, String name, String gender, String department,
String contact, String education, String role, String password, double baseSalary) {
this.id = id;
this.name = name;
this.gender = gender;
this.department = department;
this.contact = contact;
this.education = education;
this.role = role;
this.password = password;
this.baseSalary = baseSalary;
}
// Getters and setters
public String getId() { return id; }
public String getName() { return name; }
public String getGender() { return gender; }
public String getDepartment() { return department; }
public String getContact() { return contact; }
public String getEducation() { return education; }
public String getRole() { return role; }
public String getPassword() { return password; }
public double getBaseSalary() { return baseSalary; }
public void setName(String name) { this.name = name; }
public void setGender(String gender) { this.gender = gender; }
public void setDepartment(String department) { this.department = department; }
public void setContact(String contact) { this.contact = contact; }
public void setEducation(String education) { this.education = education; }
public void setBaseSalary(double baseSalary) { this.baseSalary = baseSalary; }
@Override
public String toString() {
return "ID: " + id + ", 姓名: " + name + ", 部门: " + department +
", 联系方式: " + contact + ", 基本工资: " + baseSalary;
}
}
// 考勤记录实体类
class Attendance {
private String id;
private String employeeId;
private LocalDate date;
private double workHours;
private double overtimeHours;
private String status; // 正常、迟到、早退、缺勤等
private String notes;
public Attendance(String id, String employeeId, LocalDate date,
double workHours, double overtimeHours, String status, String notes) {
this.id = id;
this.employeeId = employeeId;
this.date = date;
this.workHours = workHours;
this.overtimeHours = overtimeHours;
this.status = status;
this.notes = notes;
}
// Getters and setters
public String getEmployeeId() { return employeeId; }
public LocalDate getDate() { return date; }
public double getWorkHours() { return workHours; }
public double getOvertimeHours() { return overtimeHours; }
public String getStatus() { return status; }
@Override
public String toString() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return "员工ID: " + employeeId + ", 日期: " + date.format(formatter) +
", 工作小时: " + workHours + ", 加班小时: " + overtimeHours +
", 状态: " + status + ", 备注: " + notes;
}
}
// 薪资记录实体类
class Salary {
private String id;
private String employeeId;
private String month; // 格式: YYYY-MM
private double baseSalary;
private double overtimePay;
private double totalSalary;
public Salary(String id, String employeeId, String month,
double baseSalary, double overtimePay) {
this.id = id;
this.employeeId = employeeId;
this.month = month;
this.baseSalary = baseSalary;
this.overtimePay = overtimePay;
this.totalSalary = baseSalary + overtimePay;
}
// Getters
public String getEmployeeId() { return employeeId; }
public String getMonth() { return month; }
public double getBaseSalary() { return baseSalary; }
public double getOvertimePay() { return overtimePay; }
public double getTotalSalary() { return totalSalary; }
@Override
public String toString() {
return "员工ID: " + employeeId + ", 月份: " + month +
", 基本工资: " + baseSalary + ", 加班费: " + overtimePay +
", 实发工资: " + totalSalary;
}
}
// 工资管理系统主类
class InteractiveSalarySystem {
private Map<String, Employee> employees = new HashMap<>();
private List<Attendance> attendanceRecords = new ArrayList<>();
private List<Salary> salaryRecords = new ArrayList<>();
private Employee currentUser;
private Scanner scanner = new Scanner(System.in);
// 初始化一个管理员账户
public void initializeAdmin() {
Employee admin = new Employee("AD001", "系统管理员", "男", "管理部",
"000-00000000", "硕士", "admin", "admin123", 15000);
employees.put(admin.getId(), admin);
System.out.println("系统初始化完成,已创建管理员账户:");
System.out.println("用户名: AD001, 密码: admin123");
}
// 用户登录
public boolean login() {
System.out.println("\n=== 用户登录 ===");
System.out.print("请输入用户ID: ");
String userId = scanner.nextLine();
System.out.print("请输入密码: ");
String password = scanner.nextLine();
Employee employee = employees.get(userId);
if (employee != null && employee.getPassword().equals(password)) {
currentUser = employee;
System.out.println("登录成功!欢迎 " + employee.getName());
return true;
}
System.out.println("用户名或密码错误,请重新输入");
return false;
}
// 注销
public void logout() {
currentUser = null;
System.out.println("已成功注销");
}
// 添加员工
public void addEmployee() {
if (currentUser == null || !"admin".equals(currentUser.getRole())) {
System.out.println("权限不足,需要管理员权限");
return;
}
System.out.println("\n=== 添加新员工 ===");
System.out.print("员工ID: ");
String id = scanner.nextLine();
if (employees.containsKey(id)) {
System.out.println("员工ID已存在请使用不同的ID");
return;
}
System.out.print("姓名: ");
String name = scanner.nextLine();
System.out.print("性别: ");
String gender = scanner.nextLine();
System.out.print("部门: ");
String department = scanner.nextLine();
System.out.print("联系方式: ");
String contact = scanner.nextLine();
System.out.print("学历: ");
String education = scanner.nextLine();
System.out.print("基本工资: ");
double baseSalary = 0;
try {
baseSalary = Double.parseDouble(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("无效的工资数值设置默认值5000");
baseSalary = 5000;
}
System.out.print("初始密码: ");
String password = scanner.nextLine();
Employee newEmployee = new Employee(id, name, gender, department,
contact, education, "employee", password, baseSalary);
employees.put(id, newEmployee);
System.out.println("员工添加成功: " + name);
}
// 记录考勤
public void recordAttendance() {
if (currentUser == null || !"admin".equals(currentUser.getRole())) {
System.out.println("权限不足,需要管理员权限");
return;
}
System.out.println("\n=== 记录考勤 ===");
System.out.print("员工ID: ");
String employeeId = scanner.nextLine();
if (!employees.containsKey(employeeId)) {
System.out.println("员工ID不存在");
return;
}
System.out.print("日期 (yyyy-MM-dd): ");
String dateStr = scanner.nextLine();
LocalDate date;
try {
date = LocalDate.parse(dateStr);
} catch (DateTimeParseException e) {
System.out.println("日期格式错误,使用当前日期");
date = LocalDate.now();
}
System.out.print("工作小时: ");
double workHours = 8; // 默认8小时
try {
workHours = Double.parseDouble(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("无效的数值使用默认值8小时");
}
System.out.print("加班小时: ");
double overtimeHours = 0;
try {
overtimeHours = Double.parseDouble(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("无效的数值使用默认值0小时");
}
System.out.print("状态 (正常/迟到/早退/缺勤): ");
String status = scanner.nextLine();
if (status.isEmpty()) status = "正常";
System.out.print("备注: ");
String notes = scanner.nextLine();
String attId = "ATT" + System.currentTimeMillis();
Attendance attendance = new Attendance(attId, employeeId, date,
workHours, overtimeHours, status, notes);
attendanceRecords.add(attendance);
System.out.println("考勤记录添加成功");
}
// 计算工资
public void calculateSalary() {
if (currentUser == null || !"admin".equals(currentUser.getRole())) {
System.out.println("权限不足,需要管理员权限");
return;
}
System.out.println("\n=== 计算工资 ===");
System.out.print("员工ID: ");
String employeeId = scanner.nextLine();
if (!employees.containsKey(employeeId)) {
System.out.println("员工ID不存在");
return;
}
System.out.print("月份 (yyyy-MM): ");
String month = scanner.nextLine();
Employee employee = employees.get(employeeId);
double baseSalary = employee.getBaseSalary();
// 计算该月加班费
double overtimePay = 0;
for (Attendance att : attendanceRecords) {
if (att.getEmployeeId().equals(employeeId) &&
att.getDate().toString().startsWith(month)) {
// 加班费 = 加班小时 * 时薪 * 1.5
double hourlyRate = baseSalary / 22 / 8; // 假设每月22个工作日
overtimePay += att.getOvertimeHours() * hourlyRate * 1.5;
}
}
String salId = "SAL" + System.currentTimeMillis();
Salary salary = new Salary(salId, employeeId, month, baseSalary, overtimePay);
salaryRecords.add(salary);
System.out.println("工资计算完成:");
System.out.println("员工: " + employee.getName());
System.out.println("月份: " + month);
System.out.println("基本工资: " + baseSalary);
System.out.println("加班费: " + overtimePay);
System.out.println("实发工资: " + salary.getTotalSalary());
}
// 查看员工信息
public void viewEmployees() {
if (currentUser == null) {
System.out.println("请先登录");
return;
}
System.out.println("\n=== 员工列表 ===");
if (employees.isEmpty()) {
System.out.println("没有员工记录");
return;
}
if ("admin".equals(currentUser.getRole())) {
// 管理员可以查看所有员工
for (Employee emp : employees.values()) {
System.out.println(emp);
}
} else {
// 普通员工只能查看自己的信息
System.out.println(employees.get(currentUser.getId()));
}
}
// 查看工资信息
public void viewSalaries() {
if (currentUser == null) {
System.out.println("请先登录");
return;
}
System.out.println("\n=== 工资记录 ===");
if (salaryRecords.isEmpty()) {
System.out.println("没有工资记录");
return;
}
if ("admin".equals(currentUser.getRole())) {
// 管理员可以查看所有工资记录
for (Salary salary : salaryRecords) {
System.out.println(salary);
}
} else {
// 普通员工只能查看自己的工资
boolean found = false;
for (Salary salary : salaryRecords) {
if (salary.getEmployeeId().equals(currentUser.getId())) {
System.out.println(salary);
found = true;
}
}
if (!found) {
System.out.println("没有找到您的工资记录");
}
}
}
// 显示主菜单
public void showMenu() {
System.out.println("\n=== 职工工资管理系统 ===");
if (currentUser != null) {
System.out.println("当前用户: " + currentUser.getName() +
" (" + currentUser.getRole() + ")");
}
System.out.println("1. 登录");
if (currentUser != null) {
if ("admin".equals(currentUser.getRole())) {
System.out.println("2. 添加员工");
System.out.println("3. 记录考勤");
System.out.println("4. 计算工资");
}
System.out.println("5. 查看员工信息");
System.out.println("6. 查看工资记录");
System.out.println("7. 注销");
}
System.out.println("0. 退出系统");
System.out.print("请选择操作: ");
}
// 运行系统
public void run() {
initializeAdmin();
boolean running = true;
while (running) {
showMenu();
String choice = scanner.nextLine();
switch (choice) {
case "1":
login();
break;
case "2":
if (currentUser != null && "admin".equals(currentUser.getRole())) {
addEmployee();
} else {
System.out.println("无效选择");
}
break;
case "3":
if (currentUser != null && "admin".equals(currentUser.getRole())) {
recordAttendance();
} else {
System.out.println("无效选择");
}
break;
case "4":
if (currentUser != null && "admin".equals(currentUser.getRole())) {
calculateSalary();
} else {
System.out.println("无效选择");
}
break;
case "5":
viewEmployees();
break;
case "6":
viewSalaries();
break;
case "7":
logout();
break;
case "0":
running = false;
System.out.println("感谢使用职工工资管理系统,再见!");
break;
default:
System.out.println("无效选择,请重新输入");
}
}
}
public static void main(String[] args) {
InteractiveSalarySystem system = new InteractiveSalarySystem();
system.run();
}
}