如何获得打印的计算结果 - Java

时间:2014-04-18 22:18:17

标签: java class properties return behavior

所以这个程序假设要做的是从键盘上获取id,hours和wage的输入。然后计算加班费,定期工资和总工资。在计算之后,应该计算的id,小时,工资,加班工资,常规工资和总工资必须打印到输出。

此外,员工的财产和行为也应该在员工类中。

但是当我想打印toString方法时,常规工资,加班工资和总工资都是零。知道为什么吗?

主要方法:

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

然后是Employee类:

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

3 个答案:

答案 0 :(得分:1)

您正在使用OvertimePay,GrossPay和RegularPay而不使用其getter,并且这些属性尚未初始化。你应该打电话给吸气鬼。

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

答案 1 :(得分:1)

您不打印员工对象的状态,而是列出employeeinfo列表。您需要遍历列表并打印每个员工:

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

答案 2 :(得分:1)

Atleast给我一个1-up的杀手toString方法:

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