1. (程序题)设计具有工作能力接口Workable,声明抽象方法updateName(),通过传入的字符串修改姓名;声明抽象方法calculateSalary(),返回计算的薪资
设计员工实体类Employee,公司下所有员工均包含,姓名,基本工资属性;并实现Workable接口,所有公司职员均具有相同的修改姓名行为
设计经理实体类Manager,经理也是一类员工,拥有经理级别;经理薪资 = 基本工资 * 级别 * 80%
设计销售实体类Seller,销售也是一类员工,拥有销售额,销售提成;销售薪资 = 基本工资 + 销售额 * 销售提成
所有员工一定是2种类型之一,即系统种不存在员工,只存在经理和销售
设计薪资业务逻辑操作类SalaryService,提供getTotalSalaries()方法,统计返回,传入的所有工作员工的薪资总额。
创建Test类,在主函数,输入一个经理的名字,一个经理的基本工资,一个经理的级别;再输入一个销售人员的名字,销售人员的基本工资,销售额,及销售提成。通过调用业务逻辑类计算的薪资总额,并输出,薪资保留两位小数。
输入输出例子:
输入:
zansan 5000 2
lisi 3000 20000 0.15
输出:
薪资总额: 14000.00
提交提醒:
(1)只有Test类是public的,且一定要放在最前面。
(2)所有类提交的时候不能有创建包的语句。
java
package com.nefu.lessons;
// Test类
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class a08_experiment03_t1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 输入经理信息
String[] managerInfo = scanner.nextLine().split(" ");
String managerName = managerInfo[0];
double managerBaseSalary = Double.parseDouble(managerInfo[1]);
int managerLevel = Integer.parseInt(managerInfo[2]);
// 输入销售人员信息
String[] sellerInfo = scanner.nextLine().split(" ");
String sellerName = sellerInfo[0];
double sellerBaseSalary = Double.parseDouble(sellerInfo[1]);
double salesAmount = Double.parseDouble(sellerInfo[2]);
double salesCommission = Double.parseDouble(sellerInfo[3]);
// 创建经理对象和销售人员对象
Manager manager = new Manager(managerName, managerBaseSalary, managerLevel);
Seller seller = new Seller(sellerName, sellerBaseSalary, salesAmount, salesCommission);
// 创建员工列表
List<Employee> employees = new ArrayList<>();
employees.add(manager);
employees.add(seller);
// 计算薪资总额
SalaryService salaryService = new SalaryService();
double totalSalaries = salaryService.getTotalSalaries(employees);
// 输出薪资总额
System.out.printf("薪资总额: %.2f\n", totalSalaries);
scanner.close();
}
}
// 接口Workable
interface Workable {
void updateName(String newName);
double calculateSalary();
}
// 员工实体类Employee
abstract class Employee implements Workable {
protected String name;
protected double baseSalary;
public Employee(String name, double baseSalary) {
this.name = name;
this.baseSalary = baseSalary;
}
@Override
public void updateName(String newName) {
this.name = newName;
}
}
// 经理实体类Manager
class Manager extends Employee {
private int level;
public Manager(String name, double baseSalary, int level) {
super(name, baseSalary);
this.level = level;
}
@Override
public double calculateSalary() {
return baseSalary * level * 80 / 100.0;
}
}
// 销售实体类Seller
class Seller extends Employee {
private double salesAmount;
private double salesCommission;
public Seller(String name, double baseSalary, double salesAmount, double salesCommission) {
super(name, baseSalary);
this.salesAmount = salesAmount;
this.salesCommission = salesCommission;
}
@Override
public double calculateSalary() {
return baseSalary + salesAmount * salesCommission;
}
}
// 薪资业务逻辑操作类SalaryService
class SalaryService {
public double getTotalSalaries(List<Employee> employees) {
double totalSalaries = 0;
for (Employee employee : employees) {
totalSalaries += employee.calculateSalary();
}
return totalSalaries;
}
}