问题
How would I write a toString() method that prints name and computePay with 2 decimal places for the three employees? The program was working (printing name and weeksPay to command line) before I added the StringBuilder. Any help is appreciated.
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:
To formats decimal numbers, you can use java.text.DecimalFormat, define it in the class level as a class member so that it could share it's behavior for all objects of Payroll
as @Multithreader mentioned it in the comment, this will be your toString()
method:
private static DecimalFormat df = new DecimalFormat("#.##");
//......
@Override
public String toString(){
return "Name: "+getName()+
"\nCompute Pay: "+df.format(getComputePay())+"\n";
}
Call the method like this:
System.out.println(employee1.toString());
I prefer to use java.util.List as it's reduce a lot of codes:
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");
}
}
回答2:
You can use Apache ToStringBuilder class. Here is an example from documentation:
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();
}
}
回答3:
This can help
@Override
public String toString(){
Formatter fmt = new Formatter();
fmt.format("%.2f", computePay);
return "Name : "+name+"\n"+"ComputePay : "+fmt.toString();
}
Of course, you need to import java.util.Formatter
.
回答4:
I think the following code should give you compile time error:
//default constructor
public Payroll() {
this.name = name;
this.payRate = payRate;
this.hrsWorked = hrsWorked;
this.computePay = computePay;
}
and this piece of code is giving you trouble:
sb.append(String.format("\n", employee1.getName(), employee1.getComputePay()));
sb.append(String.format("\n", employee2.getName(), employee2.getComputePay()));
You should have this code instead of the previous one:
sb.append(String.format("%s,%f\n",employee1.getName(), employee1.getComputePay()));
sb.append(String.format("%s,%f\n",employee2.getName(), employee2.getComputePay()));
Your Payroll Class should look like:
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);
}
}
来源:https://stackoverflow.com/questions/17637775/how-do-i-write-a-tostring-method