用JAVA1.实现程序模拟简单的企业职工管理系统。编写程序实现以下要求: (1)请按继承层次构建企业的各种职工类型,包括超类Employee,以及相应的子类Manager、Secretary和Programmer,从Manager继承又可得到Executive。超类Employee有编号,姓名,年龄,薪金、地址和雇佣日期等信息。 (2)用户可以根据职工编号或职工姓名查找所需的信息。管理人员可以增加,删除数据;可以写入一个数据,查找一个数据,或删除一个数据;此外,管理人员可以增加或降低每个职工的薪金。程序通过ArrayList来实现增加,查找,删除数据。
时间: 2024-03-19 21:44:41 浏览: 62
好的,这是一个很好的编程问题。首先,我们可以创建一个Employee类来表示职工,然后创建Manager、Secretary和Programmer类作为其子类。Executive类可以继承自Manager类。
以下是一个简单的实现:
Employee.java
```java
public class Employee {
private String id;
private String name;
private int age;
private double salary;
private String address;
private String hireDate;
public Employee(String id, String name, int age, double salary, String address, String 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 String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public String getAddress() {
return address;
}
public String getHireDate() {
return hireDate;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Age: " + age + ", Salary: " + salary + ", Address: " + address + ", Hire Date: " + hireDate;
}
}
```
Manager.java
```java
public class Manager extends Employee {
private String department;
public Manager(String id, String name, int age, double salary, String address, String hireDate, String department) {
super(id, name, age, salary, address, hireDate);
this.department = department;
}
public String getDepartment() {
return department;
}
@Override
public String toString() {
return super.toString() + ", Department: " + department;
}
}
```
Secretary.java
```java
public class Secretary extends Employee {
public Secretary(String id, String name, int age, double salary, String address, String 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, String 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, String hireDate, String department) {
super(id, name, age, salary, address, hireDate, department);
}
}
```
现在我们可以创建一个管理系统来管理这些职工。
EmployeeManagementSystem.java
```java
import java.util.ArrayList;
import java.util.Scanner;
public class EmployeeManagementSystem {
private ArrayList<Employee> employees;
public EmployeeManagementSystem() {
employees = new ArrayList<>();
}
public void addEmployee(Employee employee) {
employees.add(employee);
}
public void removeEmployee(Employee employee) {
employees.remove(employee);
}
public Employee findEmployeeById(String id) {
for (Employee employee : employees) {
if (employee.getId().equals(id)) {
return employee;
}
}
return null;
}
public Employee findEmployeeByName(String name) {
for (Employee employee : employees) {
if (employee.getName().equals(name)) {
return employee;
}
}
return null;
}
public void printAllEmployees() {
for (Employee employee : employees) {
System.out.println(employee.toString());
}
}
public void increaseSalary(Employee employee, double amount) {
employee.setSalary(employee.getSalary() + amount);
}
public void decreaseSalary(Employee employee, double amount) {
employee.setSalary(employee.getSalary() - amount);
}
public static void main(String[] args) {
EmployeeManagementSystem system = new EmployeeManagementSystem();
Scanner scanner = new Scanner(System.in);
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 Employee by Name");
System.out.println("5. Print All Employees");
System.out.println("6. Increase Employee Salary");
System.out.println("7. Decrease Employee Salary");
System.out.println("8. Exit");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("Enter employee ID: ");
String id = scanner.nextLine();
System.out.print("Enter employee name: ");
String name = scanner.nextLine();
System.out.print("Enter employee age: ");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter employee salary: ");
double salary = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter employee address: ");
String address = scanner.nextLine();
System.out.print("Enter employee hire date: ");
String hireDate = scanner.nextLine();
System.out.print("Enter employee type (Manager, Secretary, Programmer, Executive): ");
String type = scanner.nextLine();
if (type.equals("Manager")) {
System.out.print("Enter manager department: ");
String department = scanner.nextLine();
Manager manager = new Manager(id, name, age, salary, address, hireDate, department);
system.addEmployee(manager);
} else if (type.equals("Secretary")) {
Secretary secretary = new Secretary(id, name, age, salary, address, hireDate);
system.addEmployee(secretary);
} else if (type.equals("Programmer")) {
Programmer programmer = new Programmer(id, name, age, salary, address, hireDate);
system.addEmployee(programmer);
} else if (type.equals("Executive")) {
System.out.print("Enter executive department: ");
String department = scanner.nextLine();
Executive executive = new Executive(id, name, age, salary, address, hireDate, department);
system.addEmployee(executive);
} else {
System.out.println("Invalid employee type.");
}
break;
case 2:
System.out.print("Enter employee ID: ");
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("Enter employee ID: ");
String findId = scanner.nextLine();
Employee findEmployeeById = system.findEmployeeById(findId);
if (findEmployeeById != null) {
System.out.println(findEmployeeById.toString());
} else {
System.out.println("Employee not found.");
}
break;
case 4:
System.out.print("Enter employee name: ");
String findName = scanner.nextLine();
Employee findEmployeeByName = system.findEmployeeByName(findName);
if (findEmployeeByName != null) {
System.out.println(findEmployeeByName.toString());
} else {
System.out.println("Employee not found.");
}
break;
case 5:
system.printAllEmployees();
break;
case 6:
System.out.print("Enter employee ID: ");
String increaseId = scanner.nextLine();
Employee increaseEmployee = system.findEmployeeById(increaseId);
if (increaseEmployee != null) {
System.out.print("Enter salary increase amount: ");
double increaseAmount = scanner.nextDouble();
scanner.nextLine();
system.increaseSalary(increaseEmployee, increaseAmount);
System.out.println("Salary increased.");
} else {
System.out.println("Employee not found.");
}
break;
case 7:
System.out.print("Enter employee ID: ");
String decreaseId = scanner.nextLine();
Employee decreaseEmployee = system.findEmployeeById(decreaseId);
if (decreaseEmployee != null) {
System.out.print("Enter salary decrease amount: ");
double decreaseAmount = scanner.nextDouble();
scanner.nextLine();
system.decreaseSalary(decreaseEmployee, decreaseAmount);
System.out.println("Salary decreased.");
} else {
System.out.println("Employee not found.");
}
break;
case 8:
System.exit(0);
break;
default:
System.out.println("Invalid choice.");
break;
}
System.out.println();
}
}
}
```
在这个系统中,我们使用了ArrayList来存储职工信息。用户可以通过输入选项来执行不同的操作,如添加职工、删除职工、查找职工、打印所有职工、增加/降低职工薪水等。
阅读全文