前言
大二开始学JAVA了,想把自己写的程序记录一下,见证自己的进步
任务
设计一个发工资系统。
1、当员工在该月生日时,会多100元工资。
2、一类员工拿固定工资,一类员工按小时计算工资,工作160小时以上1.5倍工资。
3、要求使用多态技术。
代码
代码如下:
父类Employee
public class Employee {
protected String employee_name;
protected int employee_birth_date;
public int getSalary(int month) {
int salary = 0;
return salary;
}
public void setName(String name) {
employee_name = name;
}
public void setBirth(int month) {
employee_birth_date = month;
}
public String getName() {
return employee_name;
}
public int getBirth() {
return employee_birth_date;
}
}
子类SalariedEmployee
public class SalariedEmployee extends Employee {
private int month_salary;
public int getSalary(int month) {
int salary = month_salary;
if (employee_birth_date == month) {
salary += 100;
}
return salary;
}
public void setMonthSalary(int salary) {
month_salary = salary;
}
public int getMonthSalary() {
return month_salary;
}
}
子类HourlyEmployee
public class HourlyEmployee extends Employee {
private int hour_salary;
private int work_hours_monthly;
public int getSalary(int month) {
double salary = 0;
if(work_hours_monthly>160) {
salary=(160*hour_salary)+(work_hours_monthly*hour_salary*1.5);
}
else {
salary=work_hours_monthly*hour_salary;
}
if(employee_birth_date==month) {
salary+=100;
}
return (int)salary;
}
public void setHourSalary(int salary) {
hour_salary = salary;
}
public void setWorkHoursMonthly(int month) {
work_hours_monthly = month;
}
public int getHourSalary() {
return hour_salary;
}
public int getWorkHoursMonthly() {
return work_hours_monthly;
}
}
测试类
public class EmployeeTest {
public static void main(String[] args) {
SalariedEmployee[] emps = new SalariedEmployee[2];
emps[0] = new SalariedEmployee();
emps[1] = new SalariedEmployee();
HourlyEmployee[] emph = new HourlyEmployee[2];
emph[0] = new HourlyEmployee();
emph[1] = new HourlyEmployee();
emps[0].setBirth(3);
emps[0].setMonthSalary(3000);
emps[0].setName("张三");
emps[1].setBirth(4);
emps[1].setMonthSalary(4000);
emps[1].setName("张四");
emph[0].setBirth(3);
emph[0].setHourSalary(30);
emph[0].setName("王三");
emph[0].setWorkHoursMonthly(130);
emph[1].setBirth(4);
emph[1].setHourSalary(40);
emph[1].setName("王四");
emph[1].setWorkHoursMonthly(170);
System.out.println(emps[0].getSalary(4));
System.out.println(emps[1].getSalary(4));
System.out.println(emph[0].getSalary(4));
System.out.println(emph[1].getSalary(4));
}
}