用Java编写人事管理系统代码要求 1、在用户界面中输入一个员工的工号,姓名,年龄,工资,绩效,保存在一个本地文本文档中。 2、可以对一个或所有员工的工号,年龄,工资,绩效显示在界面上。 3、可以对所有员工的(工资+绩效)的总数进行排序显示在界面上。
时间: 2024-02-05 17:14:03 浏览: 47
以下是一个简单的人事管理系统的 Java 代码示例,可以满足您的需求:
```java
import java.io.*;
import java.util.*;
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;
}
public double getTotal() {
return salary + performance;
}
}
class EmployeeManagementSystem {
private static List<Employee> employees = new ArrayList<Employee>();
public static void addEmployee(Employee employee) {
employees.add(employee);
saveEmployees();
}
public static void showEmployeeInfo() {
System.out.println("ID\tName\tAge\tSalary\tPerformance");
for (Employee employee : employees) {
System.out.println(employee.getId() + "\t" + employee.getName() + "\t" + employee.getAge() + "\t" + employee.getSalary() + "\t" + employee.getPerformance());
}
}
public static void sortByTotal() {
Collections.sort(employees, new Comparator<Employee>() {
public int compare(Employee e1, Employee e2) {
if (e1.getTotal() < e2.getTotal()) {
return 1;
} else if (e1.getTotal() > e2.getTotal()) {
return -1;
} else {
return 0;
}
}
});
System.out.println("ID\tName\tAge\tSalary\tPerformance\tTotal");
for (Employee employee : employees) {
System.out.println(employee.getId() + "\t" + employee.getName() + "\t" + employee.getAge() + "\t" + employee.getSalary() + "\t" + employee.getPerformance() + "\t" + employee.getTotal());
}
}
private static void saveEmployees() {
try {
FileWriter fileWriter = new FileWriter("employees.txt");
for (Employee employee : employees) {
fileWriter.write(employee.getId() + "," + employee.getName() + "," + employee.getAge() + "," + employee.getSalary() + "," + employee.getPerformance() + "\n");
}
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void loadEmployees() {
try {
File file = new File("employees.txt");
if (!file.exists()) {
return;
}
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(",");
int id = Integer.parseInt(parts[0]);
String name = parts[1];
int age = Integer.parseInt(parts[2]);
double salary = Double.parseDouble(parts[3]);
double performance = Double.parseDouble(parts[4]);
Employee employee = new Employee(id, name, age, salary, performance);
employees.add(employee);
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
loadEmployees();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Please choose an operation:");
System.out.println("1. Add employee");
System.out.println("2. Show employee info");
System.out.println("3. Sort by total salary");
System.out.println("4. Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Please input employee info:");
System.out.print("ID: ");
int id = scanner.nextInt();
System.out.print("Name: ");
String name = scanner.next();
System.out.print("Age: ");
int age = scanner.nextInt();
System.out.print("Salary: ");
double salary = scanner.nextDouble();
System.out.print("Performance: ");
double performance = scanner.nextDouble();
Employee employee = new Employee(id, name, age, salary, performance);
addEmployee(employee);
break;
case 2:
showEmployeeInfo();
break;
case 3:
sortByTotal();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
break;
}
}
}
}
```
注意:这只是一个简单的示例,实际应用中可能需要更多的功能和错误处理。
阅读全文