编写java程序,创建有员工类,包括姓名、工号、性别、出生年月、家庭地址等。本系统能够按姓名或工号查找员工,并显示相关员工情况。包括:主控程序、员工档案管理子系统、员工类等。 其他要求: (1)实用,各模块自成系统 (2)完成最低要求:建立一个文件,包括同一个专业10个员工的必要信息,能对文件进行补充、修订、删除,并能进行查找。
时间: 2023-07-10 16:37:18 浏览: 89
用JAVA语言编写的职工信息查询的程序
4星 · 用户满意度95%
好的,以下是一份Java程序的示例,实现了您的要求:
Employee类:
```
public class Employee {
private String name;
private String id;
private String gender;
private String birthDate;
private String address;
public Employee(String name, String id, String gender, String birthDate, String address) {
this.name = name;
this.id = id;
this.gender = gender;
this.birthDate = birthDate;
this.address = address;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public String getGender() {
return gender;
}
public String getBirthDate() {
return birthDate;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
", gender='" + gender + '\'' +
", birthDate='" + birthDate + '\'' +
", address='" + address + '\'' +
'}';
}
}
```
EmployeeManagementSystem类:
```
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class EmployeeManagementSystem {
private static final String FILE_PATH = "employees.txt"; // 员工信息文件路径
private List<Employee> employees; // 员工信息列表
public EmployeeManagementSystem() {
employees = new ArrayList<>();
loadFromFile(); // 从文件中读取员工信息
}
// 从文件中读取员工信息
private void loadFromFile() {
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {
String line;
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(",");
Employee employee = new Employee(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4]);
employees.add(employee);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 将员工信息写入文件
private void writeToFile() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH))) {
for (Employee employee : employees) {
writer.write(employee.getName() + "," + employee.getId() + "," + employee.getGender() + "," +
employee.getBirthDate() + "," + employee.getAddress() + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 添加员工信息
public void addEmployee() {
Scanner scanner = new Scanner(System.in);
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 birthDate = scanner.nextLine();
System.out.print("请输入家庭地址:");
String address = scanner.nextLine();
Employee employee = new Employee(name, id, gender, birthDate, address);
employees.add(employee);
writeToFile();
System.out.println("员工信息添加成功!");
}
// 修改员工信息
public void modifyEmployee() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要修改的员工工号:");
String id = scanner.nextLine();
Employee employee = findEmployeeById(id);
if (employee == null) {
System.out.println("未找到工号为" + id + "的员工!");
return;
}
System.out.println("当前员工信息为:" + employee);
System.out.println("请输入修改后的信息:");
System.out.print("姓名(回车跳过):");
String name = scanner.nextLine();
if (!name.isEmpty()) {
employee.setName(name);
}
System.out.print("性别(回车跳过):");
String gender = scanner.nextLine();
if (!gender.isEmpty()) {
employee.setGender(gender);
}
System.out.print("出生年月(回车跳过):");
String birthDate = scanner.nextLine();
if (!birthDate.isEmpty()) {
employee.setBirthDate(birthDate);
}
System.out.print("家庭地址(回车跳过):");
String address = scanner.nextLine();
if (!address.isEmpty()) {
employee.setAddress(address);
}
writeToFile();
System.out.println("员工信息修改成功!");
}
// 删除员工信息
public void deleteEmployee() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的员工工号:");
String id = scanner.nextLine();
Employee employee = findEmployeeById(id);
if (employee == null) {
System.out.println("未找到工号为" + id + "的员工!");
return;
}
employees.remove(employee);
writeToFile();
System.out.println("员工信息删除成功!");
}
// 根据姓名查找员工信息
public void findEmployeeByName() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查找的员工姓名:");
String name = scanner.nextLine();
List<Employee> result = new ArrayList<>();
for (Employee employee : employees) {
if (employee.getName().equals(name)) {
result.add(employee);
}
}
if (result.isEmpty()) {
System.out.println("未找到姓名为" + name + "的员工!");
return;
}
System.out.println("查询结果:");
for (Employee employee : result) {
System.out.println(employee);
}
}
// 根据工号查找员工信息
public void findEmployeeById() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查找的员工工号:");
String id = scanner.nextLine();
Employee employee = findEmployeeById(id);
if (employee == null) {
System.out.println("未找到工号为" + id + "的员工!");
return;
}
System.out.println("查询结果:");
System.out.println(employee);
}
// 根据工号查找员工信息(内部方法)
private Employee findEmployeeById(String id) {
for (Employee employee : employees) {
if (employee.getId().equals(id)) {
return employee;
}
}
return null;
}
}
```
Main类:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
EmployeeManagementSystem system = new EmployeeManagementSystem();
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("0. 退出");
int choice = scanner.nextInt();
scanner.nextLine(); // 清除输入缓冲区中的换行符
switch (choice) {
case 1:
system.addEmployee();
break;
case 2:
system.modifyEmployee();
break;
case 3:
system.deleteEmployee();
break;
case 4:
system.findEmployeeByName();
break;
case 5:
system.findEmployeeById();
break;
case 0:
System.exit(0);
default:
System.out.println("无效的操作!");
}
}
}
}
```
这份程序使用了文件存储员工信息,员工信息文件的路径为当前目录下的"employees.txt"。您可以在文件中添加同一个专业10个员工的必要信息,程序可以对文件进行补充、修订、删除,并能进行查找。
阅读全文