2

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

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

4 回答 4

1

要格式化十进制数字,您可以使用java.text.DecimalFormat,在类级别将其定义为类成员,以便它可以共享它的行为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");
    }
}
于 2013-07-14T08:48:12.463 回答
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();
   }
 }
于 2013-07-14T09:12:53.413 回答
0

这可以帮助

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

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

于 2013-07-14T08:49:21.980 回答
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);
        }                       
}
于 2013-07-14T08:49:27.917 回答