我要求修改类Staff,方法是使用displaySalary()
方法替换toString()
方法,该方法返回要显示的所有必需信息,并在main
方法中打印SOP但我甚至不知道从哪里开始,因为正如我所声明的,salary
是double
,我们如何使用String
?我真的迷失了。如果我建议从哪里开始,那将是一种感激之情。
这是给定的代码:
class Staff {
private String name, staffID;
private double salary;
private double commission;
private int workingHours;
private double Sales;
public void setStaffInfo(String nm, String id, int wHour, double sale)
{
name=nm;
staffID=id;
workingHours=wHour;
Sales=sale;
}
public void calculateSalary(){
salary = (workingHours*8);
}
public double displaySalary(){
return salary;
}
public String displayName(){
return name;
}
public String displayStaffID(){
return staffID;
}
public double displayworkingHours(){
return workingHours;
}
public double displaySales(){
return Sales;
}
public void calculateCommission(){
if(Sales >= 150 && Sales <= 300)
commission= (0.05*Sales);
else if(Sales >=301 && Sales <= 500)
commission= (0.1*Sales);
else if (Sales > 500)
commission=(0.15*Sales);
else
commission= 0;
}
public double displayCommission(){
return commission;
}
}
这是主要方法的代码:
import java.util.Scanner;
class TestStaff {
static Scanner scan = new Scanner(System.in);
public static void main(String arg[]){
Staff staff1= new Staff();
System.out.print("Enter your name: ");
String nm=scan.nextLine();
System.out.print("Enter your Staff ID: ");
String id=scan.nextLine();
System.out.print("Enter your working hours: ");
int wHour=scan.nextInt();
System.out.print("Enter your total sales: ");
double sale=scan.nextDouble();
staff1.setStaffInfo(nm, id, wHour, sale);
staff1.calculateCommission();
staff1.calculateSalary();
System.out.println("\nStaff Name: " + staff1.displayName());
System.out.println("Staff ID: " + staff1.displayStaffID());
System.out.println("Hours worked: " + staff1.displayworkingHours());
System.out.println("Total sales: RM" +staff1.displaySales());
double com= staff1.displayCommission();
double sala= staff1.displaySalary();
double total= sala+com;
System.out.println("Salary: RM" + total);
}
}
答案 0 :(得分:1)
将对象放到System.out.println
时,会调用该对象的toString
方法。
如果你没有提供一个被覆盖的版本,那么它将打印像Staff@123f23
这样的对象指针。
所以你要做的就是用你的显示逻辑重载类toString
内的Staff
方法。例如:
@Override
public String toString() {
double com= staff1.displayCommission();
double sala= staff1.displaySalary();
double total= sala+com;
return "Staff Name: " + staff1.displayName())
+ "\nStaff ID: " + staff1.displayStaffID())
+ "\nHours worked: " + staff1.displayworkingHours())
+ "\nTotal sales: RM" +staff1.displaySales())
+ "\nSalary: RM" + total );
}
答案 1 :(得分:0)
只需覆盖像这样的
public String toString(){
//write which field you need to show
return Double.toString(d);
// Here you append other column as per your need.
}
答案 2 :(得分:0)
替换此
System.out.println("Hours worked: " + staff1.displayworkingHours());
与
System.out.println("Hours worked: " + Double.toString(staff1.displayworkingHours()));
和这个
System.out.println("Total sales: RM" +staff1.displaySales());
带
System.out.println("Total sales: RM" + Double.toString(staff1.displaySales()));
逻辑是,首先使用toString(yourDoubleColumn)将双列转换为字符串,并使用字符串连接字符串
问我你是否仍然面临问题