image.png
    image.png

    1. package com.atguigu.exercise2;
    2. abstract public class Employee {
    3. //属性
    4. private String name;
    5. private int number;
    6. private MyDate birthday;
    7. //构造器
    8. public Employee(String name, int number, MyDate birthday) {
    9. super();
    10. this.name = name;
    11. this.number = number;
    12. this.birthday = birthday;
    13. }
    14. //方法
    15. //抽象方法
    16. public abstract int earnings();//收入
    17. @Override
    18. public String toString() {
    19. return "name=" + name + ", number=" +
    20. number + ", birthday=" + birthday.toDateString();
    21. }
    22. public String getName() {
    23. return name;
    24. }
    25. public void setName(String name) {
    26. this.name = name;
    27. }
    28. public int getNumber() {
    29. return number;
    30. }
    31. public void setNumber(int number) {
    32. this.number = number;
    33. }
    34. public MyDate getBirthday() {
    35. return birthday;
    36. }
    37. public void setBirthday(MyDate birthday) {
    38. this.birthday = birthday;
    39. }
    40. }

    1. package com.atguigu.exercise2;
    2. public class MyDate {
    3. //属性
    4. private int year;
    5. private int month;
    6. private int day;
    7. //构造器
    8. public MyDate(){
    9. }
    10. public MyDate(int year,int month,int day){
    11. this.year = year;
    12. this.month = month;
    13. this.day = day;
    14. }
    15. //方法
    16. public String toDateString() {
    17. return year + "年" + month + "月" + day + "日";
    18. }
    19. public int getYear() {
    20. return year;
    21. }
    22. public void setYear(int year) {
    23. this.year = year;
    24. }
    25. public int getMonth() {
    26. return month;
    27. }
    28. public void setMonth(int month) {
    29. this.month = month;
    30. }
    31. public int getDay() {
    32. return day;
    33. }
    34. public void setDay(int day) {
    35. this.day = day;
    36. }
    37. }

    1. package com.atguigu.exercise2;
    2. public class SalariedEmployee extends Employee{
    3. //属性
    4. private int monthlySalary;//月工资
    5. //构造器
    6. public SalariedEmployee(String name, int number, MyDate birthday) {
    7. super(name, number, birthday);
    8. }
    9. public SalariedEmployee(String name, int number, MyDate birthday,int monthlySalary) {
    10. super(name, number, birthday);
    11. this.monthlySalary = monthlySalary;
    12. }
    13. //方法
    14. @Override
    15. public int earnings() {
    16. return monthlySalary;
    17. }
    18. @Override
    19. public String toString() {
    20. return "SalariedEmployee[name=" + getName() + ", number=" +
    21. super.getNumber() + ", birthday=" + getBirthday() + "]";
    22. }
    23. public int getMonthlySalary() {
    24. return monthlySalary;
    25. }
    26. public void setMonthlySalary(int monthlySalary) {
    27. this.monthlySalary = monthlySalary;
    28. }
    29. }

    1. package com.atguigu.exercise2;
    2. public class HourlyEmployee extends Employee{
    3. //属性
    4. private int wage;//每小时的工资
    5. private int hour;//月工作的小时数
    6. //构造器
    7. public HourlyEmployee(String name,int number,MyDate birthday) {
    8. super(name, number, birthday);
    9. }
    10. public HourlyEmployee(String name,int number,MyDate birthday,int wage,int hour) {
    11. super(name, number, birthday);
    12. this.hour = hour;
    13. this.wage = wage;
    14. }
    15. public int getWage() {
    16. return wage;
    17. }
    18. //方法
    19. public void setWage(int wage) {
    20. this.wage = wage;
    21. }
    22. public int getHour() {
    23. return hour;
    24. }
    25. public void setHour(int hour) {
    26. this.hour = hour;
    27. }
    28. @Override
    29. public int earnings() {
    30. return wage*hour;
    31. }
    32. @Override
    33. public String toString() {
    34. return "HourlyEmployee ["+super.toString()+"]";
    35. }
    36. }

    1. package com.atguigu.exercise2;
    2. import java.util.Scanner;
    3. public class PayrollSystem {
    4. public static void main(String[] args) {
    5. Scanner scanner = new Scanner(System.in);
    6. System.out.println("请输入当月的月份:");
    7. int month = scanner.nextInt();
    8. Employee[] emps= new Employee[2];
    9. emps[0] = new SalariedEmployee("马胜",1002,new MyDate(1992,3,12),6500);
    10. emps[1] = new HourlyEmployee("小明",2001,new MyDate(1991,5,4),60,240);
    11. for(int i = 0;i < emps.length;i++){
    12. System.out.println(emps[i]);//遍历员工信息
    13. //结算工资
    14. double salary = emps[i].earnings();
    15. if(month == emps[i].getBirthday().getMonth()){
    16. salary = emps[i].earnings();//有生日时的月工资
    17. System.out.println("生日快乐,奖励100元!");
    18. System.out.println("月工资为:" + (salary + 100));//月工资
    19. }else{
    20. System.out.println("月工资为:" + salary );
    21. }
    22. }
    23. }
    24. }