某公司的雇员分为以下若干类:
Employee:这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。
SalariedEmployee:Employee的子类,拿固定工资的员工。属性:月薪
HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。属性:每小时的工资、每月工作的小时数
SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率
BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。属性:底薪。
效果如下
父类为Employee类
//父工资类(员工的继承对象)
public class Employee {
private String name; // 员工姓名
private int birthday; // 员工生日的月数
public int getBirthday() {
return birthday;
}
public void setBirthday(int birthday) {
this.birthday = birthday;
}
public void setName(String name) {
this.name = name;
}
public Employee(String name, int birthday) { // 构造方法
this.name = name;
this.birthday = birthday;
}
public Employee() {
} // 无参
public String getName() { // 获取姓名
return name;
}
// 当前月份
Calendar c = Calendar.getInstance();// 可以对每个时间域单独修改
int months = c.get(Calendar.MONTH); // 不晓得为什么,时间需要+1
public double getSalary(int month) {
if (month == (months + 1)) {
return 100;
}
return 0;
}
@Override
public String toString() {
return "Employee [name=" + name + ", birthday=" + birthday + "]";
}
}
子类SalariedEmployee类(拿基本固定工资)
public class SalariedEmployee extends Employee {
private double salary;
public SalariedEmployee() {
} // 无参
public SalariedEmployee(String name, int birthday, int salary) {
super(name, birthday); // 初始化父类公有属性
this.salary = salary; // 初始化子类属性
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public double getSalary(int month) {
if ((months + 1) == month) {
return 100 + salary;
} else
return salary;
}
public static void print() { // 输出内容
SalariedEmployee saedy = new SalariedEmployee();
System.out.println(" 公司内部工资查询页面");
System.out.print("输入你的姓名:");
Scanner scanner = new Scanner(System.in);
String sc = scanner.next();
saedy.setName(sc);
System.out.print("输入你生日的月份:");
int sca = scanner.nextInt();
double salary = 2000; // 固定工资
salary = saedy.getSalary(sca) + salary;
System.out.println(" " + saedy.getName() + " 您好!");
System.out.println("工资: " + salary);
}
}
子类salesEmployee(销售类 工资由月销售额和提成率决定。 属性:月销售额、提成率)
public class salesEmployee extends Employee {
public salesEmployee() {
}
public salesEmployee(String name, int birthday, int salary) {
super(name, birthday);
this.salary = salary;
}
private double salary;
private double lilv = 0.3;
public void setSalary(int salary) {
this.salary = salary;
}
public double getSalary(int month, int xiaol) {
if (month == (months + 1)) {
salary = 100 + xiaol * lilv;
} else
return salary = xiaol * lilv;
return salary;
}
public static void print() { // 输出内容
salesEmployee saedy = new salesEmployee();
System.out.println(" 公司内部工资查询页面");
System.out.print("输入你的姓名:");
Scanner scanner = new Scanner(System.in);
String sc = scanner.next();
saedy.setName(sc);
System.out.print("输入你生日的月份:");
int sca = scanner.nextInt();
System.out.print("输入你的业绩数量:");
int scb = scanner.nextInt();
System.out.println(" " + saedy.getName() + " 您好!");
System.out.println("工资: " + saedy.getSalary(sca, scb));
}
}
子类HourEmployee(小时工资类 按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。 属性:每小时的工资、每月工作的小时数)
public class HourlyEmployee extends Employee {
private double salary;
public HourlyEmployee(String name, int birthday, int salary) {
super(name, birthday); // 初始化父类公有属性
this.salary = salary; // 初始化子类属性
}
public HourlyEmployee() {
}
private int shic = 10;
public double getSalary(int month, int hour) {
if ((months + 1) == month) {
if (hour > 160) { // 如果大于160,
return salary = 100 + 1.5 * shic * (hour - 160) + 160 * shic;
} else
// 否则的话正常计算
return salary = 100 + shic * hour;
} else if (hour > 160) {
return salary = 1.5 * shic * (hour - 160) + 160 * shic;
} else
return salary = shic * hour;
}
public void setSalary(double salary) {
this.salary = salary;
}
public static void print() { // 输出内容
HourlyEmployee saedy = new HourlyEmployee();
System.out.println(" 公司内部工资查询页面");
System.out.print("输入你的姓名:");
Scanner scanner = new Scanner(System.in);
String sc = scanner.next();
saedy.setName(sc);
System.out.print("输入你生日的月份:");
int sca = scanner.nextInt();
System.out.print("输入你的工作时长:");
int scb = scanner.nextInt();
System.out.println(" " + saedy.getName() + " 您好!");
System.out.println("工资: " + saedy.getSalary(sca, scb));
}
}
子类BasePlusSalesEmployee,继承销售类(基本工资+提成)
public class BasePlusSalesEmployee extends salesEmployee {
public BasePlusSalesEmployee() {
}
public BasePlusSalesEmployee(String name, int birthday) {
super();
}
private double salary;
private double lilv = 0.2;
@Override
public double getSalary(int month, int xiaol) {
if (month == (months + 1)) {
return 100 + salary + xiaol * lilv;
} else
return salary + xiaol * lilv;
}
public static void print() { // 输出内容
BasePlusSalesEmployee saedy = new BasePlusSalesEmployee();
System.out.println(" 公司内部工资查询页面");
System.out.print("输入你的姓名:");
Scanner scanner = new Scanner(System.in);
String sc = scanner.next();
saedy.setName(sc);
System.out.print("输入你生日的月份:");
int sca = scanner.nextInt();
System.out.print("输入你的业绩数量:");
int scb = scanner.nextInt();
double salary = 2000; // 固定工资
salary = saedy.getSalary(sca, scb) + salary;
System.out.println(" " + saedy.getName() + " 您好!");
System.out.println("工资: " + salary);
}
}
下面是主方法
//主类
public class MainActy {
public static void main(String[] args) {
while (true) {
System.out.println("进入系统");
System.out.println("1.小时工");
System.out.println("2.文员");
System.out.println("3.销售");
System.out.println("4.我都会");
Scanner ss = new Scanner(System.in);
int scr = ss.nextInt();
switch (scr) {
case 1:
HourlyEmployee s1 = new HourlyEmployee();
s1.print();
break;
case 2:
SalariedEmployee s2 = new SalariedEmployee();
s2.print();
break;
case 3:
salesEmployee s3 = new salesEmployee();
s3.print();
break;
case 4:
BasePlusSalesEmployee s4 = new BasePlusSalesEmployee();
s4.print();
break;
default:
System.out.println("请确认后再输入");
break;
}
}
}
}
OK