Java面向
接上一天,继续升入面向
作业:还是在上一家公司,该公司将职员分为三类:部门经理、技术员和销售员。在发工资的时候,部门经理拿固定月薪8000元,技术人员按每小时100元领取月薪,销售人员按照500元底薪加当月销售额的4%进行提成,设计并实现一个工资结算系统。
创建父类(员工)
public abstract class Employee {
private String name;
public String getName() {
return name;
}
public Employee(String name) {
this.name = name;
}
public abstract double getSalary();
@Override
public String toString(){
return name;
}
子类程序员
public class Programmer extends Employee {
private double Hour;
public Programmer(String name) {
super(name);
}
@Override
public double getSalary() {
return 100*Hour;
}
public double getHour() {
return Hour;
}
public void setHour(double d) {
this.Hour = d;
}
public String toString(){
return "程序员"+super.toString();
}
}
子类经理
public class Manager extends Employee {
public Manager(String name) {
super(name);
}
public double getSalary() {
return 8000.0;
}
public String toString() {
return "[部门经理]" + super.toString();
}
}
子类销售员
public class Salesman extends Employee {
private double sales;
public double getSales() {
return sales;
}
public void setSales(double sales) {
this.sales = sales;
}
public Salesman(String name) {
super(name);
}
@Override
public double getSalary() {
return 1200 + sales * 0.05;
}
}
实现类
import java.util.Scanner;
public class SalaryCalculate {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] names = {"李琪","黄南桥","赖彬","何坤","谷振东"};
Employee[] minzi = new Employee[names.length] ;
for (int i = 0; i < minzi.length; i++) {
int empType = (int) (Math.random() * 3);
switch (empType) {
case 0:
minzi[i] = new Manager(names[i]);
break;
case 1:
minzi[i] = new Programmer(names[i]);
break;
case 2:
minzi[i] = new Salesman(names[i]);
break;
}
}
for(Employee e : minzi) {
if(e instanceof Programmer) {
System.out.print("请输入" + e.getName() + "的本月工作时间: ");
((Programmer) e).setHour(sc.nextInt());
}
else if(e instanceof Salesman) {
System.out.print("请输入" + e.getName() + "的本月销售额: ");
((Salesman) e).setSales(sc.nextDouble());
}
System.out.println(e.getName() + "本月工资为: " + e.getSalary());
}
sc.close();
}
}