编写java程序:创建有员工类,包括姓名、工号、性别、出生年月、家庭地址并生成一个员工文件。本系统能够按姓名或工号查找员工,并显示相关员工情况。包括:主控程序、员工档案管理子系统、员工类等。 其他要求: (1)实用,各模块自成系统 (2)完成最低要求:建立一个文件,包括同一个专业10个员工的必要信息,能对文件进行补充、修订、删除,并能进行查找。
时间: 2024-03-23 22:42:50 浏览: 49
好的,我可以提供一个Java代码示例来实现这个功能。以下是一个基本的实现代码:
Employee.java
```java
public class Employee {
private String name; // 姓名
private String id; // 工号
private String gender; // 性别
private String birth; // 出生年月
private String address; // 家庭地址
public Employee(String name, String id, String gender, String birth, String address) {
this.name = name;
this.id = id;
this.gender = gender;
this.birth = birth;
this.address = address;
}
// getter和setter方法
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
// toString方法,用于输出员工信息
@Override
public String toString() {
return "姓名:" + name + " 工号:" + id + " 性别:" + gender + " 出生年月:" + birth + " 家庭地址:" + address;
}
}
```
EmployeeManagementSystem.java
```java
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class EmployeeManagementSystem {
// 存储所有员工信息的列表
private ArrayList<Employee> employees = new ArrayList<>();
// 添加员工信息到列表中
public void addEmployee(Employee employee) {
employees.add(employee);
System.out.println("添加成功!");
}
// 从列表中删除员工信息
public void deleteEmployee(String id) {
for (Employee employee : employees) {
if (employee.getId().equals(id)) {
employees.remove(employee);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到指定员工!");
}
// 更新员工信息
public void updateEmployee(String id) {
for (Employee employee : employees) {
if (employee.getId().equals(id)) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入新的员工姓名:");
String name = scanner.nextLine();
System.out.print("请输入新的员工性别:");
String gender = scanner.nextLine();
System.out.print("请输入新的员工出生年月:");
String birth = scanner.nextLine();
System.out.print("请输入新的员工家庭地址:");
String address = scanner.nextLine();
employee.setName(name);
employee.setGender(gender);
employee.setBirth(birth);
employee.setAddress(address);
System.out.println("更新成功!");
return;
}
}
System.out.println("未找到指定员工!");
}
// 按姓名查找员工
public void searchByName(String name) {
for (Employee employee : employees) {
if (employee.getName().equals(name)) {
System.out.println(employee);
return;
}
}
System.out.println("未找到指定员工!");
}
// 按工号查找员工
public void searchById(String id) {
for (Employee employee : employees) {
if (employee.getId().equals(id)) {
System.out.println(employee);
return;
}
}
System.out.println("未找到指定员工!");
}
// 从文件中读取员工信息
public void readFromFile(String filename) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
Employee employee = new Employee(parts[0], parts[1], parts[2], parts[3], parts[4]);
employees.add(employee);
}
reader.close();
} catch (IOException e) {
System.out.println("读取文件出错:" + e.getMessage());
}
}
// 将员工信息写入文件
public void writeToFile(String filename) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
for (Employee employee : employees) {
String line = employee.getName() + "," + employee.getId() + "," + employee.getGender() + "," + employee.getBirth() + "," + employee.getAddress();
writer.write(line);
writer.newLine();
}
writer.close();
} catch (IOException e) {
System.out.println("写入文件出错:" + e.getMessage());
}
}
public static void main(String[] args) {
EmployeeManagementSystem system = new EmployeeManagementSystem();
system.readFromFile("employees.txt"); // 从文件中读取员工信息
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
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.print("请输入操作编号:");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("请输入员工姓名:");
String name = scanner.nextLine();
System.out.print("请输入员工工号:");
String id = scanner.nextLine();
System.out.print("请输入员工性别:");
String gender = scanner.nextLine();
System.out.print("请输入员工出生年月:");
String birth = scanner.nextLine();
System.out.print("请输入员工家庭地址:");
String address = scanner.nextLine();
Employee employee = new Employee(name, id, gender, birth, address);
system.addEmployee(employee);
break;
case 2:
System.out.print("请输入要删除的员工工号:");
id = scanner.nextLine();
system.deleteEmployee(id);
break;
case 3:
System.out.print("请输入要更新的员工工号:");
id = scanner.nextLine();
system.updateEmployee(id);
break;
case 4:
System.out.print("请输入要查找的员工姓名:");
name = scanner.nextLine();
system.searchByName(name);
break;
case 5:
System.out.print("请输入要查找的员工工号:");
id = scanner.nextLine();
system.searchById(id);
break;
case 6:
system.writeToFile("employees.txt"); // 将员工信息写入文件
System.out.println("程序已退出。");
return;
default:
System.out.println("无效操作!");
break;
}
}
}
}
```
在上述代码中,我们将员工信息存储在一个ArrayList中,并通过文件读写操作实现了持久化。程序启动时,会从文件中读取数据到ArrayList中,程序结束时,会将ArrayList中的数据写入文件。通过交互式的菜单,用户可以对员工信息进行增、删、改、查等操作。
注意:为了简单起见,本示例中没有进行输入数据的有效性检查,实际应用中应该进行适当的验证和防范,以确保程序的健壮性和安全性。
阅读全文