持续更新,敬请关注!
定义下述5个类, 其中SalaridEmployee, HourlyEmployee, CommisionEmployee 继承自Employee,basePlusCommisionEmployee继承自 CommisionEmployee。
类属性如下:
Employee: firstName,lastName,socialSecurityNumber
SalaridEmployee: weeklySalary(周薪)
HourlyEmployee: wage(每小时的工钱),hours(月工作小时数)
CommisionEmployee: grossSales(销售额),commissionRate(提成比率)
basePlusCommisionEmployee: baseSalary(月基本工资)
Employee类中定义了抽象方法earning,用于计算员工的月工资。
SalaridEmployee月工资计算为:weeklySalary4
HourlyEmployee月工资计算为:wagehours
CommisionEmployee月工资计算为:grossSalescommissionRate
basePlusCommisionEmployee月工资计算为:grossSalescommissionRate+baseSalary
类还应该包括构造方法,toString方法,属性的get/set方法。
firstName,lastName,socialSecurityNumber的初始化在构造方法中完成。其中对firstName,lastName也要提供get/set方法,对socialSecurityNumber只提供get方法。
其他属性要提供get和set方法。
类中用到的数值建议用double存储。
在Main类中利用这5个类完成相应查询要求。
第一行为一个整数n(0<n<=100),代表共n个雇员。后边是n行,每行一个雇员的数据,数据格式见样例。其中0表示SalaridEmployee,1表示HourlyEmployee,2表示CommissionEmployee,3表示basePlusCommisionEmployee。紧跟着的三个字符串依次代表firstName,lastName,socialSecurityNumber。后边的数字,如果是SalaridEmployee则代表周薪,如果是HourlyEmployee,则依次代表wage,hours,如果是CommissionEmployee则依次代表grossSales,commissionRate,如果是basePlusCommisionEmployee则依次代表grossSales,commissionRate,baseSalary。
这n行后的第一行为一个整数,m(0<m<100),代表测试用例条数。后边为m行,每行一条测试用例,数据格式见样例。其中0代表根据firstName(其后边跟的即为firstName)),1代表根据socialSecurityNumber查询(其后边跟的即为firstName)。
样例:
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
若干行,每行表示一个雇员的信息。具体格式见样例(注意各类数据之间都有一个空格)。建议依次调用对象的toString方法输出对象的信息,调用earning方法来输出对象的月工资。
如果一条查询有多条结果(firstName有可能相同),则按月工资从低到高的顺序输出。
java.lang包有一个接口叫Comparable,该接口只有一个方法,即int compareTo(T o)方法;如果我们在自己的类(比如Employee)中实现该接口,那么就可以使用使用Arrays.sort()方法对Employee类的数组进行排序。
int compareTo(T o)方法:
如: obj1.compareTo(obj2):obj1小于、等于、大于obj2时,分别返回负整数、零、正整数。
样例:
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.13
import java.util.Arrays;
import java.util.Scanner;
abstract class Employee
{
String firstName,lastName,socialSecurityNumber;
double weeklySalary,wage,hours,grossSales,commissionRate,baseSalary;
int n;
public Employee(int n, String firstName, String lastName, String socialSecurityNumber)
{
this.n = n;
this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;
}
public String getfirstName()
{
return firstName;
}
public void setfirstName(String firstName)
{
this.firstName = firstName;
}
public String getlastName()
{
return lastName;
}
public void setlastName(String lastName)
{
this.lastName = lastName;
}
public String getsocialSecurityNumber()
{
return socialSecurityNumber;
}
public abstract double earning();
public String toString()
{
return "firstName:"+firstName+"; lastName:"+lastName+"; socialSecurityNumber:"+socialSecurityNumber+"; earning:";
}
}
class SalaridEmployee extends Employee
{
double weeklySarlary;
public SalaridEmployee(int n,String firstName,String lastName,String socialSecurityNumber, double weeklySarlary)
{
super(n, firstName, lastName, socialSecurityNumber);
this.weeklySarlary=weeklySarlary;
}
public double earning()
{
return weeklySarlary*4;
//System.out.println(super.toString()+String.format("%.2f", weeklySarlary));
}
}
class HourlyEmployee extends Employee
{
double wage, hours;
public HourlyEmployee(int n, String firstName, String lastName, String socialSecurityNumber, double wage, double hours)
{
super(n, firstName, lastName, socialSecurityNumber);
this.wage = wage;
this.hours = hours;
}
public double earning()
{
return wage*hours;
//System.out.println(super.toString() + String.format("%.2f",wage*hours));
}
}
class CommisionEmployee extends Employee
{
double grossSales, commissionRate;
public CommisionEmployee(int n, String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate)
{
super(n, firstName, lastName, socialSecurityNumber);
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}
public double earning()
{
return grossSales*commissionRate;
//System.out.println(super.toString() + String.format("%.2f",grossSales*commissionRate));
}
}
class basePlusCommisionEmployee extends CommisionEmployee
{
double baseSalary;
public basePlusCommisionEmployee(int n, String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate, double baseSalary)
{
super(n, firstName, lastName, socialSecurityNumber, grossSales, commissionRate);
this.baseSalary = baseSalary;
}
public double earning()
{
double bCE_money=(grossSales*commissionRate+baseSalary);
return bCE_money;
//System.out.println(super.toString() + String.format("%.2f",bCE_money));
}
}
public class Main{
public static void main(String args[])
{
int m;
Scanner in = new Scanner(System.in);
String str,sss,mm;
int i=0;
mm=in.nextLine();
m=Integer.parseInt(mm);
Employee [] emp=new Employee[m];
for(int j=m;j>=1;j--)
{
str = in.nextLine();
String[] s = str.split(" ");
String str1 = s[0];
int num = Integer.parseInt(str1);
double money=Double.parseDouble(s[4]);
if(num==0)
{
emp[i] = new SalaridEmployee(m, s[1], s[2], s[3], money);
//p[i].earning();
i=i+1;
}
if(num==1)
{
double a=Double.parseDouble(s[5]);
emp[i] = new HourlyEmployee(m, s[1], s[2], s[3], money,a);
//people1.earning();
i=i+1;
}
if(num==2)
{
double b=Double.parseDouble(s[5]);
emp[i] = new CommisionEmployee(m, s[1], s[2], s[3], money,b);
//people2.earning();
i=i+1;
}
if(num==3)
{
double c=Double.parseDouble(s[5]);
double d=Double.parseDouble(s[6]);
emp[i] = new basePlusCommisionEmployee(m, s[1], s[2], s[3], money,c,d);
//people3.earning();
i=i+1;
}
}
Arrays.sort(emp,(first,second)->(int)(first.earning()-second.earning()));
String pp=in.nextLine();
for(int p=Integer.parseInt(pp);p>=1;p--)
{
sss=in.nextLine();
String []s1=sss.split(" ");
String sss1=s1[0];
int num1=Integer.parseInt(sss1);
if(num1==1)
{
for(int j=0;j<=i-1;j++)
{
if(s1[1].equals(emp[j].socialSecurityNumber))
{
emp[j].earning();
System.out.println(emp[j].toString() + String.format("%.2f",emp[j].earning()));
break;
}
}
}
else
{
for(int k=0;k<=i-1;k++)
{
if(s1[1].equals(emp[k].firstName))
{
emp[k].earning();
System.out.println(emp[k].toString() + String.format("%.2f",emp[k].earning()));
}
}
}
}
in.close();
}
}
冲!