某公司的雇員分為以下若干類:
Employee:這是所有員工總的父類,屬性:員工的姓名,員工的生日月份。方法:getSalary(int month) 根據參數月份來確定工資,如果該月員工過生日,則公司會額外獎勵100元。
SalariedEmployee:Employee的子類,拿固定工資的員工。屬性:月薪
HourlyEmployee:Employee的子類,按小時拿工資的員工,每月工作超出160小時的部分按照1.5倍工資發放。屬性:每小時的工資、每月工作的小時數
SalesEmployee:Employee的子類,銷售人員,工資由月銷售額和提成率決定。屬性:月銷售額、提成率
BasePlusSalesEmployee:SalesEmployee的子類,有固定底薪的銷售人員,工資由底薪加上銷售提成部分。屬性:底薪。
效果如下
Paste_Image.png
父類為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