雇员类
题目描述:
定义下述5个类, 其中SalariedEmployee
, HourlyEmployee
, CommissionEmployee
继承自 Employee
, basePlusCommissionEmployee
继承自 CommissionEmployee
.
类属性如下:
1 2 3 4 5 6 7
| Employee: firstName,lastName,socialSecurityNumber SalariedEmployee: weeklySalary HourlyEmployee: wage hours CommissionEmployee: grossSales commissionRate basePlusCommissionEmployee: baseSalary
|
Employee
类中定义了抽象方法 earning
, 用于计算员工的月工资
不同类中工资的计算方式:
类名 |
计算方式 |
SalariedEmployee |
weeklySalary * 4 |
HourlyEmployee |
wage * hours |
CommissionEmployee |
grossSales * commissionRate |
basePlusCommissionEmployee |
grossSales*commissionRate+baseSalary |
类还应该包括构造方法, toString
方法, 属性的 get/set
方法.
firstName
, lastName
, socialSecurityNumber
的初始化在构造方法中完成. 其中对 firstName
, lastName
也要提供 get/set
方法, 对 socialSecurityNumber
只提供 get
方法.
其他属性要提供 get/set
方法
类中用到的数值建议用 double
存储.
在 Main
类中利用这 5 个类完成相应查询要求.
输入:
第一行为一个整数 $n (0 < n \leq 100) $, 代表共 n 个雇员. 后边是 n 行, 每行一个雇员的数据, 数据格式见样例.
输入个数如表所示, 紧跟着的三个字符串依次代表 firstName
, lastName
, socialSecurityNumber
.
Tag |
类名 |
数字意义 (按输入顺序标记的对应的意义) |
0 |
SalariedEmployee |
weeklySalary |
1 |
HourlyEmployee |
wage , hours |
2 |
CommissionEmployee |
grossSales , commissionRate |
3 |
basePlusCommissionEmployee |
grossSales , commissionRate , baseSalary |
这 n 行后的第一行为一个整数, m(0<m<100), 代表测试用例条数. 后边为 m 行, 每行一条测试用例, 数据格式见样例.
其中: 0
代表根据 firstName
(其后边跟的即为 firstName
) 查询, 1
代表根据 socialSecurityNumber
查询
输出:
若干行, 每行表示一个雇员的信息. 具体格式见样例 (注意各类数据之间都有一个空格). 建议依次调用对象的 toString
方法输出对象的信息, 调用 earning
方法来输出对象的月工资 (保留2位小数).
如果一条查询有多条结果 (firstName
有可能相同), 则按月工资从低到高的顺序输出.
提示:
java.lang
包有一个接口叫 Comparable
, 该接口只有一个方法, 即 int compareTo(T o)
方法: 如果我们在自己的类 (比如 Employee
) 中实现该接口, 那么就可以使用使用 Arrays.sort()
方法对 Employee
类的数组进行排序.
int compareTo(T o)
方法:
如: obj1.compareTo(obj2)
: obj1
小于, 等于, 大于 obj2
时, 分别返回负整数, 零, 正整数.
输入样例:
1 2 3 4 5 6 7 8 9 10
| 4 0 Ai Meng 2012673901 4312 1 NanXiong Qimu 2016782340 15.2 200 2 Guo Yang 2017672347 46781.3 0.1 3 Rong Huang 2018768901 7854.4 0.28 7098 4 0 Ai 1 2016782340 1 2018768901 0 Guo
|
输出样例:
1 2 3 4 5
| firstName:Ai; lastName:Meng; socialSecurityNumber:2012673901; earning:17248.00 firstName:NanXiong; lastName:Qimu; socialSecurityNumber:2016782340; earning:3040.00 firstName:Rong; lastName:Huang; socialSecurityNumber:2018768901; earning:9297.23 firstName:Guo; lastName:Yang; socialSecurityNumber:2017672347; earning:4678.138768901 0 Guo
|
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
import java.util.Arrays; import java.util.Scanner;
public class Main { public static void main(String[] args) { var input = new Scanner(System.in); var n = input.nextInt(); var employee = new Employee[n]; String firstName, lastName, socialSecurity; for (int i = 0; i < n; i++) { var tag = input.nextInt(); firstName = input.next(); lastName = input.next(); socialSecurity = input.next(); switch (tag) { case 0 -> { var temp = new SalariedEmployee(firstName, lastName, socialSecurity); var weeklySalary = input.nextDouble(); temp.setWeeklySalary(weeklySalary); employee[i] = temp; } case 1 -> { var temp = new HourlyEmployee(firstName, lastName, socialSecurity); double wage = input.nextDouble(), hours = input.nextDouble(); temp.setWage(wage); temp.setHours(hours); employee[i] = temp; } case 2 -> { var temp = new CommissionEmployee(firstName, lastName, socialSecurity); double grossSales = input.nextDouble(), commissionRate = input.nextDouble(); temp.setGrossSales(grossSales); temp.setCommissionRate(commissionRate); employee[i] = temp; } case 3 -> { var temp = new basePlusCommissionEmployee(firstName, lastName, socialSecurity); double grossSales = input.nextDouble(), commissionRate = input.nextDouble(), baseSalary = input.nextDouble(); temp.setGrossSales(grossSales); temp.setCommissionRate(commissionRate); temp.setBaseSalary(baseSalary); employee[i] = temp; } } } Arrays.sort(employee); var m = input.nextInt(); for (int i = 0; i < m; i++) { var tag = input.nextInt(); var str = input.next(); switch (tag) { case 0 -> { for (Employee value : employee) if (value.getFirstName().equals(str)) System.out.println(value); } case 1 -> { for (Employee value : employee) if (value.getSocialSecurity().equals(str)) System.out.println(value); } } } } }
abstract class Employee implements Comparable<Employee> { private String firstName; private String lastName; private final String socialSecurity;
public String getFirstName() { return firstName; }
public String getLastName() { return lastName; }
public String getSocialSecurity() { return socialSecurity; }
void setFirstName(String firstName) { this.firstName = firstName; }
void setLastName(String lastName) { this.lastName = lastName; }
public Employee(String firstName, String lastName, String socialSecurity) { this.firstName = firstName; this.lastName = lastName; this.socialSecurity = socialSecurity; }
@Override public String toString() { return String.format("firstName:%s; lastName:%s; socialSecurityNumber:%s; earning:%.2f", firstName, lastName, socialSecurity, earning()); }
public int compareTo(Employee other) { return (int) (this.earning() - other.earning()); }
abstract double earning(); }
class SalariedEmployee extends Employee { private double weeklySalary;
public double getWeeklySalary() { return weeklySalary; }
public void setWeeklySalary(double weeklySalary) { this.weeklySalary = weeklySalary; }
public SalariedEmployee(String firstName, String lastName, String socialSecurity) { super(firstName, lastName, socialSecurity); }
public double earning() { return weeklySalary * 4; }
}
class HourlyEmployee extends Employee { public double getWage() { return wage; }
public void setWage(double wage) { this.wage = wage; }
public double getHours() { return hours; }
public void setHours(double hours) { this.hours = hours; }
private double wage; private double hours;
public HourlyEmployee(String firstName, String lastName, String socialSecurity) { super(firstName, lastName, socialSecurity); }
public double earning() { return wage * hours; } }
class CommissionEmployee extends Employee { private double grossSales; private double commissionRate;
public double getGrossSales() { return grossSales; }
public void setGrossSales(double grossSales) { this.grossSales = grossSales; }
public double getCommissionRate() { return commissionRate; }
public void setCommissionRate(double commissionRate) { this.commissionRate = commissionRate; }
public CommissionEmployee(String firstName, String lastName, String socialSecurity) { super(firstName, lastName, socialSecurity); }
public double earning() { return grossSales * commissionRate; } }
class basePlusCommissionEmployee extends CommissionEmployee { private double baseSalary;
public double getBaseSalary() { return baseSalary; }
public void setBaseSalary(double baseSalary) { this.baseSalary = baseSalary; }
public basePlusCommissionEmployee(String firstName, String lastName, String socialSecurity) { super(firstName, lastName, socialSecurity); }
public double earning() { return super.earning() + baseSalary; } }
|
写在最后
根据学校给的题目, 对格式和其中的一些拼写错误进行了修改, 并不影响代码结果的正确.
CommissionEmployee
-> CommissionEmployee
SalaridEmployee
-> SalariedEmployee
2023-04-17
IP属地: 北京