1.实现程序模拟简单的企业职工管理系统。编写程序实现以下要求: (1)请按继承层次构建企业的各种职工类型,包括超类Employee,以及相应的子类 Manager、Secretary和Programmer,从Manager继承又可得到Executive。超类 Employee有编号,姓名,年龄,薪金、地址和雇佣日期等信息。 (2)用户可以根据职工编号或职工姓名查找所需的信息。管理人员可以增加,删除数据;可以 写入一个数据,查找一个数据,或删除一个数据;此外,管理人员可以增加或降低每个职工的薪 金。程序通过ArrayList来实现增加,查找,删除数据
时间: 2023-12-28 08:03:22 浏览: 78
实现某部门职工信息管理。职工信息包括:职工号、姓名、性别、年龄、学历、基本工资、岗位津贴、住址、电话等(职工号不重复)。
4星 · 用户满意度95%
好的,这是一个比较复杂的程序,需要用到继承、ArrayList 等知识。以下是代码示例:
Employee.java
```java
import java.util.Date;
public class Employee {
private String id;
private String name;
private int age;
private double salary;
private String address;
private Date hireDate;
public Employee(String id, String name, int age, double salary, String address, Date hireDate) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
this.address = address;
this.hireDate = hireDate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
@Override
public String toString() {
return "Id: " + id + ", Name: " + name + ", Age: " + age + ", Salary: " + salary + ", Address: " + address
+ ", HireDate: " + hireDate.toString();
}
}
```
Manager.java
```java
public class Manager extends Employee {
public Manager(String id, String name, int age, double salary, String address, Date hireDate) {
super(id, name, age, salary, address, hireDate);
}
public void promote(double raise) {
this.setSalary(this.getSalary() + raise);
}
public void demote(double decrease) {
this.setSalary(this.getSalary() - decrease);
}
}
```
Secretary.java
```java
public class Secretary extends Employee {
public Secretary(String id, String name, int age, double salary, String address, Date hireDate) {
super(id, name, age, salary, address, hireDate);
}
}
```
Programmer.java
```java
public class Programmer extends Employee {
public Programmer(String id, String name, int age, double salary, String address, Date hireDate) {
super(id, name, age, salary, address, hireDate);
}
}
```
Executive.java
```java
public class Executive extends Manager {
public Executive(String id, String name, int age, double salary, String address, Date hireDate) {
super(id, name, age, salary, address, hireDate);
}
}
```
EmployeeManagementSystem.java
```java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class EmployeeManagementSystem {
private ArrayList<Employee> employees = new ArrayList<>();
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public void addEmployee(Employee employee) {
employees.add(employee);
}
public void removeEmployee(Employee employee) {
employees.remove(employee);
}
public void printEmployee(Employee employee) {
System.out.println(employee.toString());
}
public Employee findEmployeeById(String id) {
for (Employee employee : employees) {
if (employee.getId().equals(id)) {
return employee;
}
}
return null;
}
public ArrayList<Employee> findEmployeesByName(String name) {
ArrayList<Employee> foundEmployees = new ArrayList<>();
for (Employee employee : employees) {
if (employee.getName().equals(name)) {
foundEmployees.add(employee);
}
}
return foundEmployees;
}
public void promoteManager(Manager manager, double raise) {
manager.promote(raise);
}
public void demoteManager(Manager manager, double decrease) {
manager.demote(decrease);
}
public static void main(String[] args) throws ParseException {
EmployeeManagementSystem system = new EmployeeManagementSystem();
Scanner scanner = new Scanner(System.in);
// 添加一些员工
system.addEmployee(new Manager("1", "Tom", 30, 5000, "Shanghai", system.dateFormat.parse("2020-01-01")));
system.addEmployee(new Secretary("2", "Jerry", 25, 3000, "Beijing", system.dateFormat.parse("2020-02-01")));
system.addEmployee(new Programmer("3", "Jack", 28, 4000, "Guangzhou", system.dateFormat.parse("2020-03-01")));
system.addEmployee(new Executive("4", "Rose", 35, 8000, "Shenzhen", system.dateFormat.parse("2020-04-01")));
while (true) {
System.out.println("1. Add employee");
System.out.println("2. Remove employee");
System.out.println("3. Find employee by ID");
System.out.println("4. Find employees by name");
System.out.println("5. Promote manager");
System.out.println("6. Demote manager");
System.out.println("7. Print employee information");
System.out.println("8. Exit");
System.out.print("Please enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // 读取换行符
switch (choice) {
case 1:
System.out.print("Please enter ID: ");
String id = scanner.nextLine();
System.out.print("Please enter name: ");
String name = scanner.nextLine();
System.out.print("Please enter age: ");
int age = scanner.nextInt();
scanner.nextLine(); // 读取换行符
System.out.print("Please enter salary: ");
double salary = scanner.nextDouble();
scanner.nextLine(); // 读取换行符
System.out.print("Please enter address: ");
String address = scanner.nextLine();
System.out.print("Please enter hire date (yyyy-MM-dd): ");
Date hireDate = system.dateFormat.parse(scanner.nextLine());
System.out.println("Please select employee type:");
System.out.println("1. Manager");
System.out.println("2. Secretary");
System.out.println("3. Programmer");
int typeChoice = scanner.nextInt();
scanner.nextLine(); // 读取换行符
Employee employee = null;
switch (typeChoice) {
case 1:
employee = new Manager(id, name, age, salary, address, hireDate);
break;
case 2:
employee = new Secretary(id, name, age, salary, address, hireDate);
break;
case 3:
employee = new Programmer(id, name, age, salary, address, hireDate);
break;
default:
System.out.println("Invalid employee type!");
break;
}
if (employee != null) {
system.addEmployee(employee);
System.out.println("Employee added!");
}
break;
case 2:
System.out.print("Please enter ID of the employee to remove: ");
String removeId = scanner.nextLine();
Employee removeEmployee = system.findEmployeeById(removeId);
if (removeEmployee != null) {
system.removeEmployee(removeEmployee);
System.out.println("Employee removed!");
} else {
System.out.println("Employee not found!");
}
break;
case 3:
System.out.print("Please enter ID of the employee to find: ");
String findId = scanner.nextLine();
Employee findEmployee = system.findEmployeeById(findId);
if (findEmployee != null) {
system.printEmployee(findEmployee);
} else {
System.out.println("Employee not found!");
}
break;
case 4:
System.out.print("Please enter name of the employee to find: ");
String findName = scanner.nextLine();
ArrayList<Employee> foundEmployees = system.findEmployeesByName(findName);
if (foundEmployees.size() > 0) {
for (Employee employee : foundEmployees) {
system.printEmployee(employee);
}
} else {
System.out.println("Employee not found!");
}
break;
case 5:
System.out.print("Please enter ID of the manager to promote: ");
String promoteId = scanner.nextLine();
Manager promoteManager = (Manager) system.findEmployeeById(promoteId);
if (promoteManager != null) {
System.out.print("Please enter raise amount: ");
double raise = scanner.nextDouble();
scanner.nextLine(); // 读取换行符
system.promoteManager(promoteManager, raise);
System.out.println("Manager promoted!");
} else {
System.out.println("Manager not found!");
}
break;
case 6:
System.out.print("Please enter ID of the manager to demote: ");
String demoteId = scanner.nextLine();
Manager demoteManager = (Manager) system.findEmployeeById(demoteId);
if (demoteManager != null) {
System.out.print("Please enter decrease amount: ");
double decrease = scanner.nextDouble();
scanner.nextLine(); // 读取换行符
system.demoteManager(demoteManager, decrease);
System.out.println("Manager demoted!");
} else {
System.out.println("Manager not found!");
}
break;
case 7:
System.out.print("Please enter ID of the employee to print: ");
String printId = scanner.nextLine();
Employee printEmployee = system.findEmployeeById(printId);
if (printEmployee != null) {
system.printEmployee(printEmployee);
} else {
System.out.println("Employee not found!");
}
break;
case 8:
System.out.println("Goodbye!");
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
break;
}
System.out.println();
}
}
}
```
这个程序实现了一个简单的企业职工管理系统,可以添加、删除、查找、升职、降职、打印员工信息等操作。用户可以通过输入数字选择操作。程序使用了 ArrayList 来存储员工信息。
阅读全文