在工资支付系统中,雇员的记录由:雇员号(int)、姓名(String)、地址(String)和工资(double)组成。编写一个程序完成雇员记录的添加、修改、删除、查询功能。要求在系统退出时将系统所有的雇员记录写入文件中;在下次进入系统时,从该文件中恢复雇员信息。Java
时间: 2024-03-26 15:34:04 浏览: 132
做一个工资管理系统,要能按照姓名和卡号查找员工的工资信息,以及员工工资信息的插入,删除同时这些信息要存txt
5星 · 资源好评率100%
好的,这是一个比较典型的CRUD操作程序,可以通过Java面向对象的方式实现。以下是一个简单的实现,具体细节可以根据实际需求进行调整:
```java
import java.io.*;
import java.util.*;
class Employee implements Serializable {
private int id;
private String name;
private String address;
private double salary;
public Employee(int id, String name, String address, double salary) {
this.id = id;
this.name = name;
this.address = address;
this.salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public double getSalary() {
return salary;
}
public void setAddress(String address) {
this.address = address;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String toString() {
return "ID: " + id + ", Name: " + name + ", Address: " + address + ", Salary: " + salary;
}
}
class EmployeeSystem {
private List<Employee> employees;
private String filename;
public EmployeeSystem(String filename) {
this.filename = filename;
employees = new ArrayList<>();
loadFromFile();
}
private void loadFromFile() {
try {
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file);
employees = (List<Employee>) in.readObject();
in.close();
file.close();
} catch (IOException | ClassNotFoundException e) {
System.out.println("Failed to load employees from file: " + filename);
}
}
private void saveToFile() {
try {
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(employees);
out.close();
file.close();
} catch (IOException e) {
System.out.println("Failed to save employees to file: " + filename);
}
}
public void addEmployee(Employee employee) {
employees.add(employee);
saveToFile();
}
public void updateEmployee(int id, String address, double salary) {
for (Employee employee : employees) {
if (employee.getId() == id) {
employee.setAddress(address);
employee.setSalary(salary);
saveToFile();
return;
}
}
System.out.println("Employee not found: " + id);
}
public void deleteEmployee(int id) {
Iterator<Employee> it = employees.iterator();
while (it.hasNext()) {
Employee employee = it.next();
if (employee.getId() == id) {
it.remove();
saveToFile();
return;
}
}
System.out.println("Employee not found: " + id);
}
public void queryEmployee(int id) {
for (Employee employee : employees) {
if (employee.getId() == id) {
System.out.println(employee);
return;
}
}
System.out.println("Employee not found: " + id);
}
public void printAllEmployees() {
for (Employee employee : employees) {
System.out.println(employee);
}
}
}
public class Main {
public static void main(String[] args) {
EmployeeSystem system = new EmployeeSystem("employees.dat");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Add employee");
System.out.println("2. Update employee");
System.out.println("3. Delete employee");
System.out.println("4. Query employee");
System.out.println("5. Print all employees");
System.out.println("0. Exit");
int choice = scanner.nextInt();
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 address:");
String address = scanner.nextLine();
System.out.println("Enter employee salary:");
double salary = scanner.nextDouble();
Employee employee = new Employee(id, name, address, salary);
system.addEmployee(employee);
break;
case 2:
System.out.println("Enter employee ID:");
id = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter new address (or leave blank to skip):");
address = scanner.nextLine();
System.out.println("Enter new salary (or leave blank to skip):");
String salaryStr = scanner.nextLine();
if (!address.isBlank()) {
if (!salaryStr.isBlank()) {
salary = Double.parseDouble(salaryStr);
system.updateEmployee(id, address, salary);
} else {
system.updateEmployee(id, address, 0);
}
} else if (!salaryStr.isBlank()) {
salary = Double.parseDouble(salaryStr);
system.updateEmployee(id, "", salary);
} else {
System.out.println("No changes made.");
}
break;
case 3:
System.out.println("Enter employee ID:");
id = scanner.nextInt();
system.deleteEmployee(id);
break;
case 4:
System.out.println("Enter employee ID:");
id = scanner.nextInt();
system.queryEmployee(id);
break;
case 5:
system.printAllEmployees();
break;
case 0:
return;
default:
System.out.println("Invalid choice.");
}
}
}
}
```
这个程序使用了一个Employee类来表示每个雇员,一个EmployeeSystem类来管理所有的雇员记录。Employee类实现了Serializable接口,这样就可以将它们存储到文件中。EmployeeSystem类提供了添加、修改、删除、查询和打印所有雇员记录的方法,这些方法都会在相应的操作后将所有雇员记录写入文件中。程序的主函数使用一个简单的菜单来调用这些方法,同时在启动时从文件中恢复所有雇员记录。
阅读全文