Java学习记录(4)

前言

大二开始学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));

	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值