创建员工类和emplyoee类演示?

时间:2017-04-15 01:53:48

标签: java class methods

我们必须使用set和get方法,例如

  • Employee()
  • Employee(empId:int)
  • Employee(empId:int, payRate:double, weeklyHrs:double)
  • setId(empId:int):void
  • setPayRate(payRate:double):void
  • setHrs(weeklyHrs:double):void
  • getId():int
  • getPayRate():double
  • getHrs():double
  • getWeeklyPay():double
  • toString():String

创建员工类和员工类演示。我们必须使用两个员工ID并输入他们的每周工作时间并付费以返回他们制作的数量。以下是我为Employee类所做的事情:

      //@param
    Scanner input = new Scanner(System.in); //Scanner object
    public void setID(int empID){
      System.out.println("Enter employee 1's id: ");
      String input2 = input.next();
      int emp1 = Integer.parseInt(input2);
      System.out.println("Enter employee 2's id: ");
      String input3 = input.next();
      int emp2 = Integer.parseInt(input3);     
    }
    //@param
    public void setPayRate(double payRate){
      System.out.println("Enter employee 1's payrate: ");
      String input4 = input.next();
      double pay1 = Double.parseDouble(input4);
      System.out.println("Enter employee 2's payrate: ");
      String input5 = input.next();
      double pay2 = Double.parseDouble(input5);  
    } 
    //@param
    public void setHrs(double weeklyHrs){
      System.out.println("Enter employee 1's hours: ");
      String input6 = input.next();
      double hour1 = Double.parseDouble(input6);
      System.out.println("Enter employee 2's hours: ");
      String input7 = input.next();
      double hour2 = Double.parseDouble(input7); 
    }

//GET METHODS
    public int getID(int empID){
      return empID;  
    }
    public double getHrs(double hours){
      return hours;
    }
    public double getWeeklyPay(double pay){
      return pay;
    }

//DISPLAY ANSWERS


    public String toString(int emp1,int emp2,double hour1,double hour2,double pay1,double pay2){

      String myString= String.format("The first employee's ID is: " + emp1 + 
                                     "The second employee's ID is: " + emp2 +
                                     "The first employee's hours are: " + hour1 +
                                     "The secondemployee's hours are: " + hour2 +
                                     "The first employee's pay: $%,.2f"+ pay1*hour1 +
                                     "The second employee's pay: $%,.2f" + pay2*hour2,
                                     emp1,emp2,hour1,hour2,pay1,pay2);
      return myString;

    }

  }//End of class

我只是想知道我会在Employee Class Demo中添加什么内容以便能够返回toString方法?

1 个答案:

答案 0 :(得分:0)

System.out.println()会自动调用您传递给它的任何对象的toString()方法 - 或者,您可以自己调用它:

Employee e;

// properly initialize e

System.out.println(e); // works
System.out.println(e.toString()); // also works

作为旁注,您的toString()方法不正确。它应该是Employee类的成员函数 - 使它成为接受 Employee对象的方法是错误的方法(假设你想要遵循标准的Java OOP)实践)。

正确的方法看起来像这样:

// Notice no arguments are passed in
public String toString() {

    // Instead, we use the "this" variable
    return String.format("My ID is %s, my hours are %f, and my pay is $%,.2f", this.empID, this.hours, this.pay);
}