Question

So what this program is suppose to do is grab input from the keyboard for the id, hours and wage. It is then going to calculate the overtime pay, regular pay and gross pay. After the calculations, id, hours, wage, overtime pay, regular pay and gross pay are supposed to be calculated have to be printed to the output.

Also the properties and the behaviours of the employee are suppose to be in the employee class.

But when I want to print the toString method, Regular Pay, Overtime pay and gross pay comes out as zeros. Any idea why?

main method:

public class FinalExam
{ // begin class

public static void main(String[] args) 
    { // begin main

    // ********** DECLARATION OF CONSTANTS **********

    // ********** DECLARATION OF VARIABLES **********

        String strout;          // output string (toString)

        int ID;                 // employee's id number
        int Hours;              // employee's hours worked
        double Wage;            // employee's wage per hour

    // ********** CREATE OBJECTS **********

        ArrayList<Employee> employeeInfo = new ArrayList();     // list of all employee's properties/objects    

    // ********** CREATE INPUT STREAMS **********

        Scanner keyboard = new Scanner(System.in);              // create a Scanner object for keyboard input.

    // ********** GET INPUT **********

            // get the employee's ID
        System.out.println("\nEnter your employee ID.");
        ID = keyboard.nextInt();                //get the input and set it to the local varaible ID
        Employee employee = new Employee(ID);   // pass your id
        //System.out.println("Employee ID: " + ID);

            // get the employee's hours worked
        System.out.println("\nEnter the amount of hours you worked this week.");
        Hours = keyboard.nextInt();             //get the input and set it to the local varaible HoursWorked
        employee.setHours(Hours);               // pass your hours
        //System.out.println("Hours worked: " + Hours);

            // get the employee's wage
        System.out.println("\nEnter your wage.");
        Wage = keyboard.nextDouble();           //get the input and set it to the local varaible Wage
        employee.setWage(Wage);                 // pass your wage
        //System.out.println("Employee wage: " + Wage);

        employeeInfo.add(employee);             // add it to the list of course

    // ********** OUTPUT **********

        System.out.println("\n\n" + employeeInfo.toString()); 

} // end main
 } // end class

then the Employee class:

public class Employee
{  // begin class

// *********** CLASS VARIABLES **********

// *********** CLASS CONSTANTS **********

    private static int MAXHOURS = 40;   // maximum hours before overime
    private static double OTRATE = 1.5; // overtime is one and a half

// ********** INSTANCE VARIABLES **********

    private int ID;                     // employee's id
    private int Hours;                  // number of hours worked
    private double Wage;                // pay per hour

    private double RegularPay;          // regular pay
    private int  OverHours;         // number of overtime hours worked
    private double OverPay;             // overtime pay
    private double GrossPay;                // gross pay

// ********** CREATE INPUT STREAMS **********

    DecimalFormat df1 = new DecimalFormat ("#####.00");     // to get two decimal places at the end of the numbers

// ********** CONSTRUCTORS ***********

    public Employee(int IDnumber)
    { // begin initialized constructor
        ID = IDnumber;          // set ID to ID number
    } // end initialized constructor

// ********** ACCESSORS **********

    public int getID()
    { // begin getID
        return ID;
    } // end getID

    public void setWage(double HourlyWage)
    { // begin setWage
        Wage = HourlyWage;
    } // end setWage

    public double getWage()
    { // begin getWage
        return Wage;
    } // end getWage

    public void setHours(int hoursWorked)
    { // begin setHours
        Hours = hoursWorked;
    } // end setHours

    public double getHours()
    { // begin getHours
        return Hours;
    } // end getHours

// ********** MUTATORS **********

    public double getOverPay()
    { // begin getOverPay
        if (Hours > MAXHOURS)
        { // begin if hours worked is bigger than MAXHOURS
            OverHours = Hours - MAXHOURS;
            OverPay = OverHours * Wage * OTRATE;
        } // end if hours worked is bigger than MAXHOURS
        else
            OverPay = 0;

        return OverPay;
    } // end getOverPay

    public double getRegularPay()
    { // begin getRegularPay
        return MAXHOURS * Wage;
    } // end getRegularPay

    public double getGrossPay()
    { // begin getGrossPay
        return RegularPay + OverPay;
    } // end getGrossPay


    public String toString()    // overrides the toString method inherited from object
    { // begin toString
        String strout = "\nId \t\t Hours \t\t Rate \t\t Regular Pay \t Overtime Pay \t Gross Pay\n";
        strout += ID + "\t " + Hours + "\t\t\t $" + (df1.format(Wage)) + "\t\t $" + (df1.format(RegularPay)) + "\t\t\t $" + (df1.format(OverPay)) + "\t\t\t $" + (df1.format(GrossPay));    
            // df1.format(double) allows me two decimal places  

        return strout;
    } // end toString

}  // end class
Was it helpful?

Solution

You are using OvertimePay, GrossPay and RegularPay without using their getters, and these properties haven't been initilizated. You should call the getters.

import java.text.DecimalFormat;

public class Employee
{  // begin class

// *********** CLASS VARIABLES **********

// *********** CLASS CONSTANTS **********

    private static int MAXHOURS = 40;   // maximum hours before overime
    private static double OTRATE = 1.5; // overtime is one and a half

// ********** INSTANCE VARIABLES **********

    private int ID;                     // employee's id
    private int Hours;                  // number of hours worked
    private double Wage;                // pay per hour

    private double RegularPay;          // regular pay
    private int  OverHours;         // number of overtime hours worked
    private double OverPay;             // overtime pay
    private double GrossPay;                // gross pay

// ********** CREATE INPUT STREAMS **********

    DecimalFormat df1 = new DecimalFormat ("#####.00");     // to get two decimal places at the end of the numbers

// ********** CONSTRUCTORS ***********

    public Employee(int IDnumber)
    { // begin initialized constructor
        ID = IDnumber;          // set ID to ID number
    } // end initialized constructor

// ********** ACCESSORS **********

    public int getID()
    { // begin getID
        return ID;
    } // end getID

    public void setWage(double HourlyWage)
    { // begin setWage
        Wage = HourlyWage;
    } // end setWage

    public double getWage()
    { // begin getWage
        return Wage;
    } // end getWage

    public void setHours(int hoursWorked)
    { // begin setHours
        Hours = hoursWorked;
    } // end setHours

    public double getHours()
    { // begin getHours
        return Hours;
    } // end getHours

// ********** MUTATORS **********

    public double getOverPay()
    { // begin getOverPay
        if (Hours > MAXHOURS)
{ // begin if hours worked is bigger than MAXHOURS
            OverHours = Hours - MAXHOURS;
            OverPay = OverHours * Wage * OTRATE;
        } // end if hours worked is bigger than MAXHOURS
        else
            OverPay = 0;

        return OverPay;
    } // end getOverPay

    public double getRegularPay()
    { // begin getRegularPay

        return MAXHOURS * Wage;
    } // end getRegularPay

    public double getGrossPay()
    { // begin getGrossPay
        return getRegularPay() + OverPay;
    } // end getGrossPay


    public String toString()    // overrides the toString method inherited from object
    { // begin toString
        String strout = "\nId \t\t Hours \t\t Rate \t\t Regular Pay \t Overtime Pay \t Gross Pay\n";
        strout += ID + "\t " + Hours + "\t\t\t $" + (df1.format(Wage)) + "\t\t $" + (df1.format(getRegularPay())) + "\t\t\t $" + (df1.format(getOverPay())) + "\t\t\t $" + (df1.format(getGrossPay()));    
            // df1.format(double) allows me two decimal places  

        return strout;
    } // end toString

}  // end class

OTHER TIPS

You are not printing the state of the employee object but rather the list employeeinfo. You need to iterate over the list and print each employee:

for(Employee e : employeeInfo) {
    System.out.println(e.toString());
}

Atleast give me a 1-up for the killer toString method:

import java.util.ArrayList;
import java.util.Scanner;

public class FinalExam
{
    public static void main(String[] args)
    {
        FinalExam finalExam = new FinalExam();
        finalExam.run();
    }

    void run()
    {
        Employee emp = new Employee();
        ArrayList<Employee> list = new ArrayList<>();
        Scanner scan = new Scanner(System.in);

        System.out.print('\n' + "your employee id:  ");
        emp.setId(scan.nextInt());

        System.out.print('\n' + "hours you worked:  ");
        emp.setHours(scan.nextInt());

        System.out.print('\n' + "your wage:");
        emp.setWage(scan.nextDouble());

        list.add(emp);

        scan.close();

        System.out.println(emp.toString());
    }

    class Employee
    {
        final private int    MAXHOURS =     40;
        final private double OTRATE =       1.5;

        private int id;
        private int hours;
        private double wage;

        void setId(int id)          { this.id = id; }
        int getId()                 { return id; }

        void setHours(int hours)    { this.hours = hours; }
        int getHours()              { return hours; }

        void setWage(double wage)   { this.wage = wage; }
        double getWage()            { return wage; }

        @Override
        public String toString()
        {
            double payRegular, payOvertime, 
                payGross =      
                    (payRegular =  hours > MAXHOURS ? (MAXHOURS * wage)                    : (hours * wage))  + 
                    (payOvertime = hours > MAXHOURS ? (hours - MAXHOURS) * (wage * OTRATE) :              0);

            StringBuilder string = new StringBuilder(500);

            string.append("\n");

            string.append(
                String.format("%10s %10s %10s %15s %15s %15s", 
                    "Id", "Hours", "Rate", "Regular Pay", "Overtime Pay", "Gross Pay"));

            string.append("\n");

            string.append(
                String.format("%10s %10s %10s %15s %15s %15s", 
                    id, hours, wage, payRegular, payOvertime, payGross));

            string.trimToSize();

            return string.toString();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top