公司发工资的代码(面向对象练习)

 

public static void main(String[] args) {	
			
			System.out.println("请输入当前月份");
			Scanner sc = new Scanner(System.in);
			int month =sc.nextInt();
			System.out.println("请输入小时工工作时长");
			int hour = sc.nextInt();		
			System.out.println("请输入销售人员本月销售额");
			int sales = sc.nextInt();
			Emoloyee [] aEmoloyees= {new SalariedEmployee("固定工资", 7),new HourlyEmployee("小时工资", 7, hour),new SalesEmployee("销售工资", 7, sales),new BasePlusSalesEmployee("有底薪销售工资", 7, sales)};
			for (int i = 0; i < aEmoloyees.length; i++) {
				aEmoloyees[i].getSalary(month);
			}
		} 

}
 
abstract class Emoloyee{
	//属性
	protected String name;
	protected int month;
	//抽象方法
	public abstract void getSalary(int month);
	//封装
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	
}


//固定工资
class SalariedEmployee extends Emoloyee{
	//属性
	private double monthlyPay =10000;
	//构造方法
	public SalariedEmployee() {}
	public SalariedEmployee(String name,int month) {
		this.name = name;
		this.month =month;
		
	}
	//实现方法
	@Override
	public void getSalary(int month) {
		if (this.month == month) {
			monthlyPay +=100;
		}
		System.out.println(name + "为"+monthlyPay);
	}
	
	//封装
	public double getMonthlyPay() {
		return monthlyPay;
	}

	public void setMonthlyPay(double monthlyPay) {
		this.monthlyPay = monthlyPay;
	}
	
}

//小时工
class HourlyEmployee extends Emoloyee{
	//属性
	private int hoursMoney;
	private int hour;
	private double wages;
	//实现方法
	@Override
	public void getSalary(int month) {
		if (hour<=160) {
			wages = hour*100;
		}else {
			wages = 160*100+(hour-160)*150;
		}
		if (this.month == month) {
			wages+=100;
		}
		System.out.println(name + "为"+wages);
	}
	//构造方法
	public HourlyEmployee() {}
	public HourlyEmployee(String name,int month,int hour) {
		this.name = name;
		this.month=month;
		this.hour = hour;
	}
	//封装
	public int getHoursMoney() {
		return hoursMoney;
	}
	public void setHoursMoney(int hoursMoney) {
		this.hoursMoney = hoursMoney;
	}
	public int getHour() {
		return hour;
	}
	public void setHour(int hour) {
		this.hour = hour;
	}
	public double getWages() {
		return wages;
	}
	public void setWages(double wages) {
		this.wages = wages;
	}
}

//销售工资
class SalesEmployee extends Emoloyee{
	//属性
	protected int  sales ;
	protected double commission=0.5;
	protected double wages;
	//实现方法 
	@Override
	public void getSalary(int month) {
		wages = this.sales*commission;
		if (this.month ==month) {
			wages+=100;
		}
		
		System.out.println(name+"为"+wages);
	}
	//构造方法
	public SalesEmployee() {}
	public SalesEmployee(String name,int month,int sales) {
		this.name=name;
		this.month =month;
		this.sales =sales;
	}
	
	//封装
	public int getSales() {
		return sales;
	}
	public void setSales(int sales) {
		this.sales = sales;
	}
	public double getCommission() {
		return commission;
	}
	public void setCommission(double commission) {
		this.commission = commission;
	}
	public double getWages() {
		return wages;
	}
	public void setWages(double wages) {
		this.wages = wages;
	}
	
}
//有底薪销售
class BasePlusSalesEmployee extends SalesEmployee{
	//属性
	private double monthlyPay =5000;
	//构造方法
	public BasePlusSalesEmployee() {}
	public BasePlusSalesEmployee(String name,int month,int sales) {
		this.name=name;
		this.month =month;
		this.sales =sales;
	}
	@Override
	public void getSalary(int month) {
		wages = this.monthlyPay+ this.sales*commission;
		if (this.month ==month) {
			wages+=100;
		}
		
		System.out.println(name+"为"+wages);
	}
	//封装
	public double getMonthlyPay() {
		return monthlyPay;
	}
	public void setMonthlyPay(double monthlyPay) {
		this.monthlyPay = monthlyPay;
	}
	
}

        这是刚学习完面向对象知识做的题目,希望以后学习更多知识后可以对程序进行优化。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值