用java写一个有关职工工作的添加,查询,显示,编辑,删除,统计,保存,读取功能的程序
时间: 2024-04-30 14:25:01 浏览: 10
以下是一个简单的职工管理系统的Java程序,包括添加、查询、显示、编辑、删除、统计、保存和读取职工信息的功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
public class EmployeeManagementSystem {
private static ArrayList<Employee> employees = new ArrayList<Employee>();
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("1. 添加职工信息");
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("8. 从文件中读取职工信息");
System.out.println("0. 退出");
System.out.print("请选择操作:");
int choice = input.nextInt();
switch (choice) {
case 1:
addEmployee();
break;
case 2:
searchEmployee();
break;
case 3:
displayEmployees();
break;
case 4:
editEmployee();
break;
case 5:
deleteEmployee();
break;
case 6:
statistics();
break;
case 7:
saveToFile();
break;
case 8:
readFromFile();
break;
case 0:
exit = true;
break;
default:
System.out.println("无效的选择,请重新输入!");
}
}
}
private static void addEmployee() {
Scanner input = new Scanner(System.in);
System.out.print("请输入职工姓名:");
String name = input.nextLine();
System.out.print("请输入职工工号:");
String id = input.nextLine();
System.out.print("请输入职工工资:");
double salary = input.nextDouble();
Employee employee = new Employee(name, id, salary);
employees.add(employee);
System.out.println("职工信息添加成功!");
}
private static void searchEmployee() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要查询的职工工号:");
String id = input.nextLine();
for (Employee employee : employees) {
if (employee.getId().equals(id)) {
System.out.println(employee);
return;
}
}
System.out.println("未找到该职工的信息!");
}
private static void displayEmployees() {
for (Employee employee : employees) {
System.out.println(employee);
}
}
private static void editEmployee() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要编辑的职工工号:");
String id = input.nextLine();
for (Employee employee : employees) {
if (employee.getId().equals(id)) {
System.out.print("请输入职工姓名(原姓名:" + employee.getName() + "):");
String name = input.nextLine();
System.out.print("请输入职工工资(原工资:" + employee.getSalary() + "):");
double salary = input.nextDouble();
employee.setName(name);
employee.setSalary(salary);
System.out.println("职工信息编辑成功!");
return;
}
}
System.out.println("未找到该职工的信息!");
}
private static void deleteEmployee() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要删除的职工工号:");
String id = input.nextLine();
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i).getId().equals(id)) {
employees.remove(i);
System.out.println("职工信息删除成功!");
return;
}
}
System.out.println("未找到该职工的信息!");
}
private static void statistics() {
double totalSalary = 0;
double maxSalary = Double.MIN_VALUE;
double minSalary = Double.MAX_VALUE;
Employee maxEmployee = null;
Employee minEmployee = null;
for (Employee employee : employees) {
totalSalary += employee.getSalary();
if (employee.getSalary() > maxSalary) {
maxSalary = employee.getSalary();
maxEmployee = employee;
}
if (employee.getSalary() < minSalary) {
minSalary = employee.getSalary();
minEmployee = employee;
}
}
System.out.println("职工总数:" + employees.size());
System.out.println("职工平均工资:" + (totalSalary / employees.size()));
System.out.println("最高工资的职工:" + maxEmployee);
System.out.println("最低工资的职工:" + minEmployee);
}
private static void saveToFile() {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employees.dat"));
out.writeObject(employees);
out.close();
System.out.println("职工信息保存成功!");
} catch (IOException e) {
System.out.println("保存职工信息失败:" + e);
}
}
private static void readFromFile() {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("employees.dat"));
employees = (ArrayList<Employee>) in.readObject();
in.close();
System.out.println("职工信息读取成功!");
} catch (IOException e) {
System.out.println("读取职工信息失败:" + e);
} catch (ClassNotFoundException e) {
System.out.println("读取职工信息失败:" + e);
}
}
}
class Employee implements Serializable {
private String name;
private String id;
private double salary;
public Employee(String name, String id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "姓名:" + name + ",工号:" + id + ",工资:" + salary;
}
}
```
在这个程序中,`Employee` 类表示一个职工,包含姓名、工号和工资三个属性。`EmployeeManagementSystem` 类是程序的主体,包含了各种操作职工信息的方法,例如添加、查询、显示、编辑、删除和统计职工信息。职工信息保存在一个 `ArrayList` 中,可以通过保存到文件和从文件读取的方式来实现职工信息的持久化。