抽象和继承类

时间:2017-11-24 19:47:30

标签: java inheritance abstract-class

[电流输出] [1] 需要知道为什么薪水是正确的打印输出120000/12,但执行和每小时类打印完全相同的双值它从员工文本文件[staff] [2]读取。问题UML看起来像这样[问题UML] [3] 想知道我是否在Hourly和Executive课程中正确使用Super关键字     ************************************************** *********

  //Driver class reads from staff.txt file    
        import java.io.File;
            import java.util.Scanner;
            public class StaffDemo{
              private static int size;
              private static String[][] employeeInfo;
              private static StaffMember[] staffMember;
              public static void main(String[] args){
                createStaff();
                //System.out.println(staffMember[3].getPaid());
                payAll();
              }

                  public static void createStaff(){
                    try{
                      File file = new File("staff.txt");
                      Scanner s = new Scanner(file);
                      size = Integer.parseInt(s.next());
                      //System.out.println(size);
                      employeeInfo = new String[size][5];
                      for(int i =0;i<size;i++){
                        for(int j=0;j<5;j++){
                          employeeInfo[i][j]=s.next();
                        }
                      }
                      int i=0;
                      staffMember = new StaffMember[size];
                      while(i<size){
                        if(employeeInfo[i][0].equals("Salaried")){
                          StaffMember staffSalaried = new Salaried(Integer.parseInt(employeeInfo[i][1]),employeeInfo[i][2],employeeInfo[i][3],Double.valueOf(employeeInfo[i][4]));
                          staffSalaried.setEmployeeType(employeeInfo[i][0]);
                          staffMember[i] = staffSalaried;
                        }else if(employeeInfo[i][0].equals("Volunteer")){
                          StaffMember staffVolunteer = new Volunteer(Integer.parseInt(employeeInfo[i][1]),employeeInfo[i][2],employeeInfo[i][3],Double.valueOf(employeeInfo[i][4]));        
                          staffVolunteer.setEmployeeType(employeeInfo[i][0]);
                          staffMember[i] = staffVolunteer;
                        }else if(employeeInfo[i][0].equals("Hourly")){
                          StaffMember staffHourly = new Volunteer(Integer.parseInt(employeeInfo[i][1]),employeeInfo[i][2],employeeInfo[i][3],Double.valueOf(employeeInfo[i][4]));        
                          staffHourly.setEmployeeType(employeeInfo[i][0]);
                          staffMember[i] = staffHourly;
                        }else if(employeeInfo[i][0].equals("Executive")){
                          StaffMember staffExecutive = new Volunteer(Integer.parseInt(employeeInfo[i][1]),employeeInfo[i][2],employeeInfo[i][3],Double.valueOf(employeeInfo[i][4]));        
                          staffExecutive.setEmployeeType(employeeInfo[i][0]);          
                          staffMember[i] = staffExecutive;        
                        }
                        i++;
                      }
                    }
                    catch(Exception e){
                      System.out.println("no file found");
                    }
                  }
                  public static void payAll(){
                    for(int k=0;k<size;k++)
                      System.out.println(staffMember[k]);
                  }
                }

    *******************************************************************************   


        //Executive class extends Salaried, suppose to add 0.1*payRate*bonus
            public class Executive extends Salaried{
              private double bonus=0;
              private double monthlySalary;
              public Executive(int idNumber, String firstName, String lastName, double payRate){
                super(idNumber,firstName,lastName, payRate);
                this.setMonthlyRate();
              }
              public void setMonthlyRate(){
                this.monthlySalary=super.payRate*0.1*bonus;
              }
              public double getMonthlyRate(){
                return this.monthlySalary;
              }
              public void addBonus(double bonus){
                this.bonus+=bonus;
              }
              public double getBonus(){
                return this.bonus;
              }
              @Override
              public double getPaid(){
                return this.getMonthlyRate();
              }
              @Override
              public String toString(){
                return super.toString()+this.getPaid();
              }
            }

    ********************************************************************************

        // volunteer class prints out payrate equals zero
        public class Volunteer extends StaffMember{
          private double payRate;
          public Volunteer(int idNumber, String firstName, String lastName, double payRate){
            super(idNumber,firstName,lastName);
            this.setMonthlyRate(payRate);
          }
          public void setMonthlyRate(double payRate){
            this.payRate=payRate;
          }
          public double getMonthlyRate(){
            return this.payRate;
          }
          @Override
          public double getPaid(){
            return getMonthlyRate();
          }
          public String toString(){
            return super.toString()+getPaid();
          }
        }

    ********************************************************************************
        //Hourly class extends StaffMember, it suppose to do payRate*80.00   
        public class Hourly extends StaffMember{
          private double payRate;
          public Hourly(int idNumber, String firstName, String lastName, double payRate){
            super(idNumber,firstName,lastName);
            this.setMonthlyRate(payRate);
          }
          public void setMonthlyRate(double payRate){
            this.payRate=payRate*80.00;
          }
          public double getMonthlyRate(){
            return this.payRate;
          }
          @Override
          public double getPaid(){
            return getMonthlyRate();
          }
          @Override
          public String toString(){
            return super.toString()+getMonthlyRate();
          }
        }

    ********************************************************************************
        //salaried
            public class Salaried extends StaffMember{
              public double payRate;
              public Salaried(int idNumber, String firstName, String lastName, double payRate){
                super(idNumber, firstName, lastName);
                this.setMonthlyRate(payRate);
              }
              public double getMonthlyRate(){
                return this.payRate;
              }
              public void setMonthlyRate(double payRate){
                this.payRate=payRate/12.00;
              }
              @Override
              public double getPaid(){
                return getMonthlyRate();
              }
              public String toString(){
                return super.toString()+getMonthlyRate();
              }
            }

    ********************************************************************************
        //StaffMember is abstract class
        public abstract class StaffMember{
          private String firstName;
          private String lastName;
          private int idNumber;
          private String employeeType;

          public StaffMember(int idNumber, String firstName, String lastName){
            this.idNumber=idNumber;
            this.firstName=firstName;
            this.lastName=lastName;
          }
          public int getIdNumber(){
            return this.idNumber;
          }
          public void setIdNumber(int idNumber){
            this.idNumber=idNumber;
          }
          public String getFirstName(){
            return this.firstName;
          }
          public void setFirstName(String firstName){
            this.firstName=firstName;
          } 
          public String getLastName(){
            return this.lastName;
          }
          public void setLastName(String lastName){
            this.lastName=lastName;
          } 
          public String getEmployeeType(){
            return this.employeeType;
          }
          public void setEmployeeType(String employeeType){
            this.employeeType=employeeType;
          }
          @Override
          public String toString(){
            return getFirstName()+" "+getLastName()+" ("+getEmployeeType()+"): $";
          }

          public abstract double getPaid();
          public abstract double getMonthlyRate();
          public abstract void setMonthlyRate(double i);
        }



  [1]: https://i.stack.imgur.com/rF54p.png
  [2]: https://i.stack.imgur.com/ohblq.png
  [3]: https://i.stack.imgur.com/UezAe.png

1 个答案:

答案 0 :(得分:0)

你确定“每小时”和“执行”都是志愿者吗?

否则if(employeeInfo [i] [0] .equals(“Hourly”)){

StaffMember staffHourly = new 志愿者(...)
...

否则if(employeeInfo [i] [0] .equals(“Executive”)){

StaffMember staffExecutive = new 志愿者(...)