I'm asked to modify the class Staff by replacing the displaySalary()
method with the toString()
method that returns all required information to be displayed and print S.O.P in the main
method but I don't even know where to start because as I have declared, salary
is a double
, how are we going to use a String
? I'm really lost with this. It will be a gratitude if I'm advised where to start.
This is the given code:
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;
}
}
This is the code for the main method:
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);
}
}