一、人事管理系统要求在用户界面中输入一个员工的工号,姓名,年龄,工资,绩效,保存在一个本地文本文档中。可以对一个或所有员工的工号,年龄,工资,绩效显示在界面上。可以对所有员工的(工资+绩效)的总数进行排序显示在界面上。用java实现
时间: 2024-03-02 22:51:54 浏览: 158
好的,下面是一个简单的 Java 实现,包含了员工类和人事管理系统类:
```java
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
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 void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void setPerformance(double performance) {
this.performance = performance;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", performance=" + performance +
'}';
}
}
class EmployeeSystem {
private ArrayList<Employee> employees;
public EmployeeSystem() {
employees = new ArrayList<>();
}
public void addEmployee(Employee employee) {
employees.add(employee);
saveToFile();
}
public void showAllEmployees() {
for (Employee employee : employees) {
System.out.println(employee);
}
}
public void showEmployee(int id) {
for (Employee employee : employees) {
if (employee.getId() == id) {
System.out.println(employee);
return;
}
}
System.out.println("Employee with ID " + id + " not found.");
}
public void showEmployeeStats() {
double total = 0;
for (Employee employee : employees) {
total += employee.getSalary() + employee.getPerformance();
}
System.out.println("Total salary and performance: " + total);
Collections.sort(employees, new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
return Double.compare(o2.getSalary() + o2.getPerformance(), o1.getSalary() + o1.getPerformance());
}
});
System.out.println("Employees sorted by salary and performance:");
for (Employee employee : employees) {
System.out.println(employee);
}
}
private void saveToFile() {
try {
FileWriter writer = new FileWriter("employees.txt");
for (Employee employee : employees) {
writer.write(String.format("%d,%s,%d,%.2f,%.2f\n", employee.getId(), employee.getName(), employee.getAge(), employee.getSalary(), employee.getPerformance()));
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadFromFile() {
try {
File file = new File("employees.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String[] parts = scanner.nextLine().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]);
employees.add(new Employee(id, name, age, salary, performance));
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) {
EmployeeSystem employeeSystem = new EmployeeSystem();
employeeSystem.loadFromFile();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Add employee");
System.out.println("2. Show all employees");
System.out.println("3. Show employee by ID");
System.out.println("4. Show employee statistics");
System.out.println("0. Exit");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.println("Enter employee ID:");
int id = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter employee name:");
String name = scanner.nextLine();
System.out.println("Enter employee age:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter employee salary:");
double salary = scanner.nextDouble();
scanner.nextLine();
System.out.println("Enter employee performance score:");
double performance = scanner.nextDouble();
scanner.nextLine();
employeeSystem.addEmployee(new Employee(id, name, age, salary, performance));
break;
case 2:
employeeSystem.showAllEmployees();
break;
case 3:
System.out.println("Enter employee ID:");
int id2 = scanner.nextInt();
scanner.nextLine();
employeeSystem.showEmployee(id2);
break;
case 4:
employeeSystem.showEmployeeStats();
break;
case 0:
return;
}
}
}
}
```
这个程序将员工信息保存在名为 `employees.txt` 的文本文件中,可以在下次运行程序时重新加载。在程序运行时,用户可以选择添加员工、显示所有员工、按 ID 显示员工、显示员工统计信息或退出程序。
阅读全文