北邮JAVA高级语言程序设计(选修课)第四次OJ作业
题目描述:
定义下述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方法。
读取对象类型和相应的数据,每行表示一个对象实例,数据格式见样例。其中0表示SalaridEmployee,1表示HourlyEmployee,2表示CommissionEmployee,3表示BasePlusCommisionEmployee。读取所有对象实例,对象引用保存到数组中。然后依次调用对象的toString方法输出对象的信息,调用earning方法来输出对象的月工资。
输入样例:
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
输出样例:
firstName:Ai; lastName:Meng; socialSecurityNumber:2012673901; earning:17248.00
firstName:NanXiong; lastName:Qimu; socialSecurityNumber:2016782340; earning:3040.00
firstName:Guo; lastName:Yang; socialSecurityNumber:2017672347; earning:4678.13
firstName:Rong; lastName:Huang; socialSecurityNumber:2018768901; earning:9297.23
源代码:
import java.util.Scanner;
class SalaridEmployee extends Employee
{
double weeklySalary;
public SalaridEmployee(int type, String firstName, String lastName, String socialSecurityNumber, double weeklySalary)
{
super(type, firstName, lastName, socialSecurityNumber);
this.weeklySalary = weeklySalary;
}
public void earning()
{
System.out.println(super.toString() + String.format("%.2f",weeklySalary*4.00));
}
}
class HourlyEmployee extends Employee
{
double wage, hours;
public HourlyEmployee(int type, String firstName, String lastName, String socialSecurityNumber, double wage, double hours)
{
super(type, firstName, lastName, socialSecurityNumber);
this.wage = wage;
this.hours = hours;
}
public void earning()
{
System.out.println(super.toString() + String.format("%.2f",wage*hours));
}
}
class CommisionEmployee extends Employee
{
double grossSales, commissionRate;
public CommisionEmployee(int type, String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate)
{
super(type, firstName, lastName, socialSecurityNumber);
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}
public void earning()
{
System.out.println(super.toString() + String.format("%.2f",grossSales*commissionRate));
}
public double Money()
{
return grossSales*commissionRate;
}
}
class BasePlusCommisionEmployee extends CommisionEmployee
{
double baseSalary;
public BasePlusCommisionEmployee(int type, String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate, double baseSalary)
{
super(type, firstName, lastName, socialSecurityNumber, grossSales, commissionRate);
this.baseSalary = baseSalary;
}
public void earning()
{
double GZH = Money() + baseSalary;
System.out.println(super.toString() + String.format("%.2f",GZH));
}
}
public class EmployeeTest {
public static void main(String args[])
{
String line;
Scanner in = new Scanner(System.in);
while(in.hasNext())
{
line = in.nextLine();
String[] list = line.split(" ");
String s1 = list[0];
int command = Integer.parseInt(s1);
double weekmoney = Double.parseDouble(list[4]);
if(command == 0)
{
SalaridEmployee earning1 = new SalaridEmployee(command, list[1], list[2], list[3], weekmoney);
earning1.earning();
}
if(command == 1)
{
double a1 = Double.parseDouble(list[5]);
HourlyEmployee earning2 = new HourlyEmployee(command,list[1],list[2],list[3],weekmoney,a1);
earning2.earning();
}
if(command == 2)
{
double a2 = Double.parseDouble(list[5]);
CommisionEmployee earning3 = new CommisionEmployee(command,list[1],list[2],list[3],weekmoney,a2);
earning3.earning();
}
if(command == 3)
{
double a3 = Double.parseDouble(list[5]);
double b = Double.parseDouble(list[6]);
BasePlusCommisionEmployee earning4 = new BasePlusCommisionEmployee(command,list[1],list[2],list[3],weekmoney,a3,b);
earning4.earning();
}
}
}
}
abstract class Employee
{
int type;
String firstName;
String lastName;
String socialSecurityNumber;
double weeklySalary;
double wage,hours;
double grossSales,commissionRate;
double baseSalary;
public Employee(int type, String firstName, String lastName, String socialSecurityNumber)
{
this.type = type;
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 void earning(); //抽象方法在父类中不能实现,所以没有函数体。但在后续在继承时,要具体实现此方法。
public String toString()
{
return "firstName:" +
firstName +
"; lastName:" +
lastName +
"; socialSecurityNumber:" +
socialSecurityNumber +
"; earning:" ;
}
}