java面向对象设计工资管理系统
时间: 2023-09-28 07:04:13 浏览: 134
1. 定义员工类
- 成员变量:员工编号、姓名、基本工资
- 成员方法:计算薪水(根据基本工资和工龄等因素计算总薪水)、获取员工信息
2. 定义经理类(继承自员工类)
- 成员变量:绩效工资、奖金
- 成员方法:计算薪水(包括基本工资、绩效工资和奖金)、获取经理信息
3. 定义销售员类(继承自员工类)
- 成员变量:销售额、提成比例
- 成员方法:计算薪水(包括基本工资和销售提成)、获取销售员信息
4. 定义工资管理系统类
- 成员变量:员工列表
- 成员方法:添加员工、删除员工、修改员工信息、查询员工信息、计算所有员工总薪水
5. 编写测试代码,测试工资管理系统的各项功能
示例代码:
员工类:
```
public class Employee {
private String id;
private String name;
private double baseSalary;
public Employee(String id, String name, double baseSalary) {
this.id = id;
this.name = name;
this.baseSalary = baseSalary;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getBaseSalary() {
return baseSalary;
}
public double calculateSalary() {
// 根据基本工资和工龄等因素计算总薪水
return baseSalary;
}
public String getInfo() {
// 获取员工信息
return "员工编号:" + id + ",姓名:" + name + ",基本工资:" + baseSalary;
}
}
```
经理类:
```
public class Manager extends Employee {
private double performanceSalary;
private double bonus;
public Manager(String id, String name, double baseSalary, double performanceSalary, double bonus) {
super(id, name, baseSalary);
this.performanceSalary = performanceSalary;
this.bonus = bonus;
}
public double getPerformanceSalary() {
return performanceSalary;
}
public double getBonus() {
return bonus;
}
@Override
public double calculateSalary() {
// 包括基本工资、绩效工资和奖金
return super.calculateSalary() + performanceSalary + bonus;
}
@Override
public String getInfo() {
// 获取经理信息
return "员工编号:" + getId() + ",姓名:" + getName() + ",基本工资:" + getBaseSalary() + ",绩效工资:" + performanceSalary + ",奖金:" + bonus;
}
}
```
销售员类:
```
public class Salesman extends Employee {
private double salesAmount;
private double commissionRate;
public Salesman(String id, String name, double baseSalary, double salesAmount, double commissionRate) {
super(id, name, baseSalary);
this.salesAmount = salesAmount;
this.commissionRate = commissionRate;
}
public double getSalesAmount() {
return salesAmount;
}
public double getCommissionRate() {
return commissionRate;
}
@Override
public double calculateSalary() {
// 包括基本工资和销售提成
return super.calculateSalary() + salesAmount * commissionRate;
}
@Override
public String getInfo() {
// 获取销售员信息
return "员工编号:" + getId() + ",姓名:" + getName() + ",基本工资:" + getBaseSalary() + ",销售额:" + salesAmount + ",提成比例:" + commissionRate;
}
}
```
工资管理系统类:
```
import java.util.ArrayList;
import java.util.List;
public class SalaryManagementSystem {
private List<Employee> employeeList;
public SalaryManagementSystem() {
employeeList = new ArrayList<>();
}
public void addEmployee(Employee employee) {
employeeList.add(employee);
}
public void deleteEmployee(String id) {
for (Employee employee : employeeList) {
if (employee.getId().equals(id)) {
employeeList.remove(employee);
break;
}
}
}
public void modifyEmployee(String id, String name, double baseSalary) {
for (Employee employee : employeeList) {
if (employee.getId().equals(id)) {
employeeList.remove(employee);
employeeList.add(new Employee(id, name, baseSalary));
break;
}
}
}
public String queryEmployee(String id) {
for (Employee employee : employeeList) {
if (employee.getId().equals(id)) {
return employee.getInfo();
}
}
return "未找到员工";
}
public double calculateTotalSalary() {
double totalSalary = 0;
for (Employee employee : employeeList) {
totalSalary += employee.calculateSalary();
}
return totalSalary;
}
}
```
测试代码:
```
public class TestSalaryManagementSystem {
public static void main(String[] args) {
SalaryManagementSystem salaryManagementSystem = new SalaryManagementSystem();
// 添加员工
salaryManagementSystem.addEmployee(new Employee("001", "张三", 5000));
salaryManagementSystem.addEmployee(new Manager("002", "李四", 8000, 2000, 3000));
salaryManagementSystem.addEmployee(new Salesman("003", "王五", 4000, 10000, 0.1));
// 查询员工信息
System.out.println(salaryManagementSystem.queryEmployee("001"));
System.out.println(salaryManagementSystem.queryEmployee("002"));
System.out.println(salaryManagementSystem.queryEmployee("003"));
System.out.println(salaryManagementSystem.queryEmployee("004"));
// 修改员工信息
salaryManagementSystem.modifyEmployee("001", "张三丰", 6000);
System.out.println(salaryManagementSystem.queryEmployee("001"));
// 删除员工
salaryManagementSystem.deleteEmployee("002");
System.out.println(salaryManagementSystem.queryEmployee("002"));
// 计算所有员工总薪水
System.out.println("总薪水:" + salaryManagementSystem.calculateTotalSalary());
}
}
```
阅读全文