我如何编写toString()方法

时间:2013-07-14 08:31:02

标签: java tostring

我如何编写一个toString()方法,为三名员工打印名称和带有2个小数位的computePay?在添加StringBuilder之前,程序正在工作(打印名称和weeksPay到命令行)。任何帮助表示赞赏。

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;
                }      

}

4 个答案:

答案 0 :(得分:1)

要设置十进制数字的格式,您可以使用java.text.DecimalFormat,在类级别将其定义为class member,以便它可以为{{}的所有对象共享它的行为 1}}正如@Multithreader在评论中提到的那样,这将是你的Payroll方法:

toString()

调用这样的方法:

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

我更喜欢使用java.util.List,因为它减少了很多代码:

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

答案 1 :(得分: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();
   }
 }

答案 2 :(得分:0)

这可以帮助

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

当然,您需要导入java.util.Formatter

答案 3 :(得分: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);
        }                       
}