2013-07-14 92 views
2

我該如何編寫一個toString()方法,用於爲三名員工打印姓名和computePay以及兩位小數位?在我添加StringBuilder之前,該程序正在工作(打印名稱和星期付款到命令行)。任何幫助表示讚賞。如何編寫toString()方法

import javax.swing.JOptionPane; 

public class TestPayroll { 

     public static void main(String[] args) { 

       Payroll employee1 = new Payroll("Tiny Tim", 100.25, 40); 
       Payroll employee2 = new Payroll("Brad Pitt", 150.50, 10); 
       Payroll employee3 = new Payroll("Madonna", 124.24, 20); 

     StringBuilder sb = new StringBuilder(); 

     String toDisplay=sb.toString();   
     sb.append(String.format("\n", employee1.getName(), employee1.getComputePay())); 
     sb.append(String.format("\n", employee2.getName(), employee2.getComputePay())); 


     JOptionPane.showMessageDialog(null, sb.toString(), toDisplay, JOptionPane.INFORMATION_MESSAGE); 
     }      
} 

public class Payroll { 

    public static void main(String[] args) { 
       } 

       private String name; 
       private double payRate; 
       private double hrsWorked; 
       private double computePay; 

       //default constructor 
       public Payroll() { 
         this.name = name; 
         this.payRate = payRate; 
         this.hrsWorked = hrsWorked; 
         this.computePay = computePay; 
       } 

       //Payroll constructor 
       public Payroll(String name, double payRate, double hrsWorked) { 
         this.name = name; 
         this.payRate = payRate; 
         this.hrsWorked = hrsWorked; 
       } 

       //return name 
       public String getName() { 
         return name; 
       } 

       //set name 
       public void setName(String name) { 
         this.name = name; 
       } 

       //return pay rate 
       public double getPayRate() { 
         return payRate; 
       } 

       //set pay rate 
       public void setPayRate(double payRate) { 
         this.payRate = payRate; 
       } 

       //return hours worked for the week 
       public double getHrsWorked() { 
         return hrsWorked; 
       } 

       //set hours worked for the week 
       public void setHrsWorked(double hrsWorked) { 
         this.hrsWorked = hrsWorked; 
       } 

       //find week's pay 
       public double getComputePay() { 
         double computePay = payRate * hrsWorked; 
      return computePay; 
       }  

} 
+1

*爲三位僱員保留2位小數* *我不明白這行可以解釋更多! – Azad

+0

你的TestPayroll類甚至不會按原樣編譯。請修正主方法的'}' – Multithreader

+0

抱歉。有3名員工,我想顯示他們的姓名和週薪。 weeksPay應該有2個小數位。 – Grafica

回答

1

要格式化十進制數字,您可以使用java.text.DecimalFormat,在一流水平的class member,使其可以分享它的行爲Payroll所有對象爲@Multithreader在評論中提到它定義它,這將您的toString()方法:

private static DecimalFormat df = new DecimalFormat("#.##"); 
//...... 
    @Override 
    public String toString(){ 
     return "Name: "+getName()+ 
      "\nCompute Pay: "+df.format(getComputePay())+"\n"; 
    } 

呼叫的方法是這樣的:

System.out.println(employee1.toString()); 

我更喜歡使用java.util.List因爲它減少了很多代碼:

public static void main(String[] args) { 
    List <Payroll> employes = new ArrayList<>(); 

    employes.add(new Payroll("Tiny Tim", 100.2534, 40.87876)); 
    employes.add(new Payroll("Brad Pitt", 150.50, 10)); 
    employes.add(new Payroll("Madonna", 124.24, 20)); 

    for(Payroll p :employes){ 
    System.out.println(p.toString()+"\n"); 
    } 
} 
+0

哇。謝謝。 – Grafica

+0

@Grafica:也請參閱最新的答案。 – Azad

+1

如果將'new java.text.DecimalFormat(「#。##」)'作爲最終靜態private變量存儲在同一個類或其他地方,而不是每次創建一個'new'對象,它會更好你調用'toString()' – Multithreader

0

這可以幫助

@Override 
    public String toString(){ 
     Formatter fmt = new Formatter(); 
     fmt.format("%.2f", computePay); 
     return "Name : "+name+"\n"+"ComputePay : "+fmt.toString(); 
    } 

當然,你需要輸入java.util.Formatter

0

我認爲下面的代碼應該給你編譯時錯誤:

//default constructor 
       public Payroll() { 
         this.name = name; 
         this.payRate = payRate; 
         this.hrsWorked = hrsWorked; 
         this.computePay = computePay; 
       } 

這一段代碼給你的麻煩:

sb.append(String.format("\n", employee1.getName(), employee1.getComputePay())); 
sb.append(String.format("\n", employee2.getName(), employee2.getComputePay())); 

你應該有這樣的代碼,而不是前一個:

sb.append(String.format("%s,%f\n",employee1.getName(), employee1.getComputePay())); 
sb.append(String.format("%s,%f\n",employee2.getName(), employee2.getComputePay())); 

您的工資等級應爲:

public class Payroll { 

    private String name; 
    private double payRate; 
    private double hrsWorked; 
    private double computePay;//need not to be instance variable 

    //default constructor 
    public Payroll() { 
     //should set default value if you want 
    } 

    //Payroll constructor 
    public Payroll(String name, double payRate, double hrsWorked) { 
     this.name = name; 
     this.payRate = payRate; 
     this.hrsWorked = hrsWorked; 
    } 

    //return name 
    public String getName() { 
     return name; 
    } 

    //set name 
    public void setName(String name) { 
     this.name = name; 
    } 

    //return pay rate 
    public double getPayRate() { 
     return payRate; 
    } 

    //set pay rate 
    public void setPayRate(double payRate) { 
     this.payRate = payRate; 
    } 

    //return hours worked for the week 
    public double getHrsWorked() { 
     return hrsWorked; 
    } 

    //set hours worked for the week 
    public void setHrsWorked(double hrsWorked) { 
     this.hrsWorked = hrsWorked; 
    } 

    //find week's pay 
    public double getComputePay() { 
     double computePay = payRate * hrsWorked; 
     return computePay; 
    }  


} 
import javax.swing.JOptionPane; 

public class TestPayroll { 

     public static void main(String[] args) { 

       Payroll employee1 = new Payroll("Tiny Tim", 100.25, 40); 
       Payroll employee2 = new Payroll("Brad Pitt", 150.50, 10); 
       Payroll employee3 = new Payroll("Madonna", 124.24, 20); 

     StringBuilder sb = new StringBuilder(); 

     String toDisplay=sb.toString(); 
     sb.append(String.format("%s,%f\n",employee1.getName(), employee1.getComputePay())); 
     sb.append(String.format("%s,%f\n",employee2.getName(), employee2.getComputePay())); 
     // sb.append(String.format("\n", employee1.getName(), employee1.getComputePay())); 
     //sb.append(String.format("\n", employee2.getName(), employee2.getComputePay())); 


     JOptionPane.showMessageDialog(null, sb.toString(), toDisplay, JOptionPane.INFORMATION_MESSAGE); 
     }      
} 
1

您可以使用Apache ToStringBuilder類。這裏是一個來自文檔的例子:

public class Person { 
    String name; 
    int age; 
    boolean smoker; 

    ... 

    public String toString() { 
    return new ToStringBuilder(this). 
     append("name", name). 
     append("age", age). 
     append("smoker", smoker). 
     toString(); 
    } 
}