码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象代码作业

时间:2017-11-25 19:19:21      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:作业   父类   abstract   .net   str   new   director   修改   basic   

/*
 * * 在前一章作业的基础上,进行修改。在Employee类中添加一个抽象方法以计算薪资总额。
 * 定义一个方法基于休假天数leave计算要从基本薪资中扣除的部分。
 * 计算薪资扣除部分的公式为:lessPay=(leave<=5)?(0.25*basic):(0.5*basic)
 * Manager和Director类重写父类的抽象方法。 计算Manager的薪资总额totalAmount的公式为:
 * totalAmount=basic+houseRentAllowance+dearnessAllowance+medicalAllowance;
 * 其中:houseRentAllowance为basic的8% dearnessAllowance为basic的30% medicalAllowance为1500 
 * 计算Director的薪资总额的公式为:
 * totalAmount=basic+houseRentAllowance+dearnessAllowance+medicalAllowance
 * +entertainmentAllowance+transportAllowance
 * 其中:houseRentAllowance为basic的20% dearnessAllowance为basic的50% medicalAllowance为4500 entertainmentAllowance为5000
 * 该程序还应该计算应付薪资,其公式为:NetPay=totalAmount-lessPay
 * 请在show方法中显示name,address,basic,totalAmount和netPay属性中储存的值。
 * 
 * 
 * */

abstract class Employee {
	double basic;
	String name;
	String address;
	int leave;
	double lessPay;
	double NetPay;
	public Employee() {}
	public Employee(double basic,String name,String address,int leave) {
		this();
		this.basic = basic;
		this.name = name;
		this.address = address;
		this.leave = leave;
		
	}
	public abstract double TotalAmount();
	public double lessPay(){
		 return this.lessPay = (leave <= 5)?(0.25*basic):(0.5*basic);
	}
	public double NetPay(){
		 return this.NetPay = this.TotalAmount() - this.lessPay();
	}
	public void show(){
		System.out.println("name:"+ name+"\t" + "address:"+ address+"\t"+
	"TotalAmount:"+ this.TotalAmount() +"\t" + "NetPay:"+ this.NetPay());
	}
	
	
}

class Manager extends Employee{
	private String department;
	public Manager(){
		super();
	}
	public Manager(String name, double basic, String address, int leave,String department) {
		super(basic, name, address, leave);
		this.department = department;
	}
	public double TotalAmount(){
		double houseRentAllowance = basic * 0.08;
		double dearnessAllowance = basic * 0.3;
		int medicalAllowance = 1500;
		return basic + houseRentAllowance + dearnessAllowance + medicalAllowance;
	}
	
}
class Director extends Employee{
	private double transportAllowance;
	public Director(){
		super();
	}
	public Director(String name, double basic, String address, int leave, double transportAllowance) {
		super(basic, name, address, leave);
		this.transportAllowance = transportAllowance;
	}
	public double TotalAmount(){
		double houseRentAllowance = basic * 0.2;
		double dearnessAllowance = basic * 0.5;
		int medicalAllowance = 4500;
		int entertainmentAllowance = 5000;
		return basic + houseRentAllowance + dearnessAllowance + medicalAllowance
				  + entertainmentAllowance + transportAllowance;
	}
}

public class Pay{
	public static void main(String[] args) {
		Employee a = new Manager("张三",3000,"中国",6,"总部");
		Employee b = new Director("李四",5000,"美国",3,2000);
		a.show();
		b.show();
				
	}
}

  

面向对象代码作业

标签:作业   父类   abstract   .net   str   new   director   修改   basic   

原文地址:http://www.cnblogs.com/finsky/p/7896055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!