编写java程序:创建有员工类,包括姓名、工号、性别、出生年月、家庭地址等。本系统能够按姓名或工号查找员工,并显示相关员工情况。包括:主控程序、员工档案管理子系统、员工类等。 其他要求: (1)实用,各模块自成系统 (2)完成最低要求:建立一个文件,包括同一个专业10个员工的必要信息,能对文件进行补充、修订、删除,并能进行查找。
时间: 2024-03-17 13:47:08 浏览: 68
以下是员工类的Java代码实现:
```java
public class Employee {
private String name;
private int id;
private String gender;
private String birthDate;
private String address;
public Employee(String name, int 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 int getId() {
return id;
}
public String getGender() {
return gender;
}
public String getBirthDate() {
return birthDate;
}
public String getAddress() {
return address;
}
}
```
以下是员工档案管理子系统的Java代码实现:
```java
import java.io.*;
import java.util.ArrayList;
public class EmployeeManagementSystem {
private ArrayList<Employee> employees;
private String fileName;
public EmployeeManagementSystem(String fileName) {
this.fileName = fileName;
readFromFile();
}
public void addEmployee(Employee employee) {
employees.add(employee);
writeToFile();
}
public void deleteEmployeeById(int id) {
for (Employee e : employees) {
if (e.getId() == id) {
employees.remove(e);
break;
}
}
writeToFile();
}
public Employee searchEmployeeByName(String name) {
for (Employee e : employees) {
if (e.getName().equals(name)) {
return e;
}
}
return null;
}
public Employee searchEmployeeById(int id) {
for (Employee e : employees) {
if (e.getId() == id) {
return e;
}
}
return null;
}
private void readFromFile() {
employees = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
String name = data[0];
int id = Integer.parseInt(data[1]);
String gender = data[2];
String birthDate = data[3];
String address = data[4];
Employee employee = new Employee(name, id, gender, birthDate, address);
employees.add(employee);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeToFile() {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
for (Employee e : employees) {
bw.write(e.getName() + "," + e.getId() + "," + e.getGender() + "," + e.getBirthDate() + "," + e.getAddress() + "\n");
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以下是主控程序的Java代码实现:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
EmployeeManagementSystem ems = new EmployeeManagementSystem("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("0. 退出程序");
int choice = scanner.nextInt();
if (choice == 0) {
break;
}
switch (choice) {
case 1:
System.out.println("请输入员工姓名:");
String name = scanner.next();
System.out.println("请输入员工工号:");
int id = scanner.nextInt();
System.out.println("请输入员工性别:");
String gender = scanner.next();
System.out.println("请输入员工出生年月:");
String birthDate = scanner.next();
System.out.println("请输入员工家庭地址:");
String address = scanner.next();
Employee employee = new Employee(name, id, gender, birthDate, address);
ems.addEmployee(employee);
System.out.println("员工添加成功!");
break;
case 2:
System.out.println("请输入员工工号:");
int deleteId = scanner.nextInt();
ems.deleteEmployeeById(deleteId);
System.out.println("员工删除成功!");
break;
case 3:
System.out.println("请输入员工姓名:");
String searchName = scanner.next();
Employee e1 = ems.searchEmployeeByName(searchName);
if (e1 != null) {
System.out.println("姓名:" + e1.getName());
System.out.println("工号:" + e1.getId());
System.out.println("性别:" + e1.getGender());
System.out.println("出生年月:" + e1.getBirthDate());
System.out.println("家庭地址:" + e1.getAddress());
} else {
System.out.println("未找到该员工!");
}
break;
case 4:
System.out.println("请输入员工工号:");
int searchId = scanner.nextInt();
Employee e2 = ems.searchEmployeeById(searchId);
if (e2 != null) {
System.out.println("姓名:" + e2.getName());
System.out.println("工号:" + e2.getId());
System.out.println("性别:" + e2.getGender());
System.out.println("出生年月:" + e2.getBirthDate());
System.out.println("家庭地址:" + e2.getAddress());
} else {
System.out.println("未找到该员工!");
}
break;
default:
System.out.println("输入有误,请重新输入!");
}
}
}
}
```
以上代码实现了一个简单的员工档案管理系统,可以进行员工的添加、删除、查找等操作,并将数据存储在文件中。
阅读全文