用Java编写人事管理系统代码要求 1、在用户界面中输入一个员工的工号,姓名,年龄,工资,绩效,保存在一个本地文本文档中。 2、可以对一个或所有员工的工号,年龄,工资,绩效显示在界面上。 3、可以对所有员工的(工资+绩效)的总数进行排序显示在界面上。
时间: 2024-02-05 20:14:03 浏览: 18
以下是一个简单的Java人事管理系统代码,实现了上述三个要求:
```
import java.util.*;
import java.io.*;
public class HRSystem {
private static ArrayList<Employee> employees = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1.添加员工");
System.out.println("2.查看所有员工信息");
System.out.println("3.按工资+绩效排序并查看所有员工信息");
System.out.println("4.退出程序");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addEmployee(scanner);
break;
case 2:
showAllEmployees();
break;
case 3:
sortBySalaryAndPerformance();
showAllEmployees();
break;
case 4:
System.out.println("程序已退出。");
System.exit(0);
break;
default:
System.out.println("无效的操作。");
break;
}
}
}
private static void addEmployee(Scanner scanner) {
System.out.print("请输入员工工号:");
int id = scanner.nextInt();
System.out.print("请输入员工姓名:");
String name = scanner.next();
System.out.print("请输入员工年龄:");
int age = scanner.nextInt();
System.out.print("请输入员工工资:");
double salary = scanner.nextDouble();
System.out.print("请输入员工绩效:");
double performance = scanner.nextDouble();
Employee employee = new Employee(id, name, age, salary, performance);
employees.add(employee);
saveToFile(employee);
System.out.println("员工信息已保存。");
}
private static void showAllEmployees() {
if (employees.isEmpty()) {
System.out.println("暂无员工信息。");
} else {
System.out.println("所有员工信息如下:");
for (Employee employee : employees) {
System.out.println(employee);
}
}
}
private static void sortBySalaryAndPerformance() {
Collections.sort(employees, new Comparator<Employee>() {
@Override
public int compare(Employee e1, Employee e2) {
double s1 = e1.getSalary() + e1.getPerformance();
double s2 = e2.getSalary() + e2.getPerformance();
return Double.compare(s2, s1);
}
});
}
private static void saveToFile(Employee employee) {
try {
FileWriter writer = new FileWriter("employees.txt", true);
writer.write(employee.toString());
writer.write(System.getProperty("line.separator"));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Employee {
private int id;
private String name;
private int age;
private double salary;
private double performance;
public Employee(int id, String name, int age, double salary, double performance) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
this.performance = performance;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public double getPerformance() {
return performance;
}
@Override
public String toString() {
return String.format("工号:%d 姓名:%s 年龄:%d 工资:%.2f 绩效:%.2f", id, name, age, salary, performance);
}
}
```
该程序使用了ArrayList来存储员工信息,每当添加一个员工时,会将其信息保存到本地文本文档中(文件名为"employees.txt")。可以通过选择操作来查看所有员工的信息,或按工资+绩效排序后查看所有员工的信息。