帮我写一个 java 的员工信息管理系统 用switch展示菜单 持久化数据
时间: 2024-03-02 22:48:27 浏览: 58
好的,以下是一个简单的Java员工信息管理系统的代码,使用了switch展示菜单和文件持久化数据:
```java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class EmployeeManagementSystem {
private List<Employee> employees;
private static final String FILE_PATH = "employees.txt";
public EmployeeManagementSystem() {
employees = new ArrayList<>();
}
public void start() {
System.out.println("Welcome to Employee Management System!");
loadEmployees(); // 加载员工数据
Scanner scanner = new Scanner(System.in);
boolean quit = false;
while (!quit) {
System.out.println("\nPlease select an option:");
System.out.println("1. Add an employee");
System.out.println("2. Update an employee");
System.out.println("3. Remove an employee");
System.out.println("4. View all employees");
System.out.println("5. Quit");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
addEmployee(scanner);
break;
case 2:
updateEmployee(scanner);
break;
case 3:
removeEmployee(scanner);
break;
case 4:
viewEmployees();
break;
case 5:
quit = true;
break;
default:
System.out.println("Invalid choice, please try again.");
break;
}
}
saveEmployees(); // 保存员工数据
System.out.println("Goodbye!");
}
private void addEmployee(Scanner scanner) {
System.out.println("\nPlease enter the employee ID:");
int id = scanner.nextInt();
scanner.nextLine();
System.out.println("Please enter the employee name:");
String name = scanner.nextLine();
System.out.println("Please enter the employee age:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.println("Please enter the employee department:");
String department = scanner.nextLine();
Employee employee = new Employee(id, name, age, department);
employees.add(employee);
System.out.println("Employee added successfully.");
}
private void updateEmployee(Scanner scanner) {
System.out.println("\nPlease enter the employee ID to update:");
int id = scanner.nextInt();
scanner.nextLine();
Employee employee = findEmployeeById(id);
if (employee != null) {
System.out.println("Please enter the employee name:");
String name = scanner.nextLine();
employee.setName(name);
System.out.println("Please enter the employee age:");
int age = scanner.nextInt();
scanner.nextLine();
employee.setAge(age);
System.out.println("Please enter the employee department:");
String department = scanner.nextLine();
employee.setDepartment(department);
System.out.println("Employee updated successfully.");
} else {
System.out.println("Employee not found.");
}
}
private void removeEmployee(Scanner scanner) {
System.out.println("\nPlease enter the employee ID to remove:");
int id = scanner.nextInt();
scanner.nextLine();
Employee employee = findEmployeeById(id);
if (employee != null) {
employees.remove(employee);
System.out.println("Employee removed successfully.");
} else {
System.out.println("Employee not found.");
}
}
private void viewEmployees() {
System.out.println("\nEmployee List:");
for (Employee employee : employees) {
System.out.println(employee);
}
}
private Employee findEmployeeById(int id) {
for (Employee employee : employees) {
if (employee.getId() == id) {
return employee;
}
}
return null;
}
private void loadEmployees() {
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {
String line = null;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
int id = Integer.parseInt(fields[0]);
String name = fields[1];
int age = Integer.parseInt(fields[2]);
String department = fields[3];
Employee employee = new Employee(id, name, age, department);
employees.add(employee);
}
System.out.println("Employee data loaded successfully.");
} catch (FileNotFoundException e) {
System.out.println("Employee data file not found, starting with empty data.");
} catch (IOException e) {
System.out.println("Error while reading employee data file.");
}
}
private void saveEmployees() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH))) {
for (Employee employee : employees) {
writer.write(employee.getId() + "," + employee.getName() + "," + employee.getAge() + ","
+ employee.getDepartment());
writer.newLine();
}
System.out.println("Employee data saved successfully.");
} catch (IOException e) {
System.out.println("Error while writing employee data file.");
}
}
public static void main(String[] args) {
EmployeeManagementSystem system = new EmployeeManagementSystem();
system.start();
}
}
```
在该示例中,我们定义了一个EmployeeManagementSystem类,其中包含了添加、更新、删除和查看员工信息的方法。在start方法中,我们使用switch语句展示菜单,根据用户选择调用相应的方法。在程序启动和退出时,我们分别调用loadEmployees和saveEmployees方法,从文件中加载员工数据,或将员工数据保存到文件中。
需要注意的是,该示例中的员工数据仅包含了ID、姓名、年龄和部门信息,实际项目中需要根据需求进行扩展。此外,我们还需要考虑员工数据的索引、查询等功能,以提高系统的查询和操作效率。
阅读全文