-1

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

    }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Hann
  • 21
  • 1
  • 1
  • 6
  • 1
    You can use `Double.toString(salary)`. – Héctor May 04 '18 at 15:25
  • 1
    [This](https://stackoverflow.com/questions/5766318/converting-double-to-string) may help you. Also, you haven't specified *how* the `toString` method should display the information. Like, all the values one after the other separated by commas? – Federico klez Culloca May 04 '18 at 15:25
  • @FedericoklezCulloca oh no no, the values should start at every next line – Hann May 04 '18 at 16:24

3 Answers3

1

When put object to System.out.println, the toString method of that object is called.

If you don't provide an overridden version then it will print something likes Staff@123f23 which is the object pointer.

So all you have to do is overload the toString method inside class Staff with your display logic. For example:

@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 );
}
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
0

Just override the to String method like this

public String toString(){
      //write which field you need to show
      return Double.toString(d);
      // Here you append other column as per your need. 
}
Naresh Bharadwaj
  • 192
  • 1
  • 12
0

Replace this

System.out.println("Hours worked: " + staff1.displayworkingHours());

with

System.out.println("Hours worked: " + Double.toString(staff1.displayworkingHours()));

and this

System.out.println("Total sales: RM" +staff1.displaySales());

with

System.out.println("Total sales: RM" + Double.toString(staff1.displaySales()));

Logic is, first covert your double column to string by using toString(yourDoubleColumn) and, concatenate string with a string

Ask me if you are still facing issues

AMRESH PANDEY
  • 195
  • 1
  • 10