程序有效,但有点偏。 Java简单工资计划

时间:2015-05-28 21:22:34

标签: java

我正在尝试计算所有员工的总支出,但是在工作时间增加10之后,它仍然按照employee1的原始工资而不是工资。请告知我做错了什么。

package payroll;

public class Payroll 
{
    private static double totalSalary ;

    private String id ;
    private double hours ;
    private double rate ;
    private double salary ;

    public Payroll(String id, double hours, double rate)
    {
        this.id = id ;
        this.hours = hours ;
        this.rate = rate ;
        double salaryCalc = hours * rate ;
        totalSalary = totalSalary + salaryCalc ;
    }

    public void calculateSalary()
    {
        salary = hours * rate ;
    }

    public double getSalary()
    {
        return salary ;
    }

    public String getEmployeeID()
    {
        return id ;
    }

    public double getHours()
    {
        return hours ;
    }

    public double getRate()
    {
        return rate ;
    }

    public void increaseHours(double hourIncrease) 
    {
        hours = hours + hourIncrease ;
    }

    public static double getTotalPayout()
    {
        return totalSalary ;
    }
}

TEST CLASS

package payroll;

import java.util.Date ;         // Used for creating a Date object
import java.text.DateFormat ;   // Used for specifying the format of the date
import java.text.NumberFormat ; // Used for specifying the type of currency



public class TestPayroll 
    {       
        public static void main(String [] arg)
        {
        // Set up the formatters
        Date d = new Date(2010-1900, 8-1, 25) ; // Date Class requires these adjustments to get proper date
        //Date d = new Date() ; // if current date was required
        DateFormat df = DateFormat.getDateInstance(DateFormat.LONG) ;
        NumberFormat nf = NumberFormat.getCurrencyInstance() ;

        System.out.println("\nPayroll For Week Ending " + df.format(d)) ;
        System.out.println("-------------------------------------");
        Payroll employee1 = new Payroll("444-4444", 30, 25) ;
        employee1.calculateSalary() ;
        displaySalary(employee1, nf) ;

        Payroll employee2 = new Payroll("555-5555", 20, 50) ;
        employee2.calculateSalary() ;
        displaySalary(employee2, nf) ;

        System.out.println("\tIncrease " + employee1.getEmployeeID() + " by 10 hours") ;
        employee1.increaseHours(10) ; 
        employee1.calculateSalary() ;
        displaySalary(employee1, nf) ;

        System.out.println("Total payout amount.. " +  nf.format(Payroll.getTotalPayout())) ;
        System.out.println("------------ End of report ----------") ;
        }


        public static void displaySalary(Payroll e, NumberFormat nf)    
        {
        System.out.println("\tEmployee # ......" + e.getEmployeeID() ) ;
        System.out.println("\tHours Worked ...." + e.getHours() + " hours" ) ;
        System.out.println("\tHourly Rate ....." + nf.format(e.getRate()) + "/hour" ) ;
        System.out.println("\tYour Salary is .." + nf.format(e.getSalary()) ) ;
        System.out.println("\t---------------------------------\n") ;
        }   

    }

1 个答案:

答案 0 :(得分:1)

您不应该在类的构造函数中计算总支付的加法,因为对其状态的更改不会反映在此字段中(正如您所发现的那样)。如果你必须在构造函数中执行它,你需要让你的类实现一个观察者类型设计来通知外部类的变化 - 你不想要或不需要的东西。相反,我会创建一个Employee对象的集合(这就是我所说的类),并在需要时迭代遍历集合以使用for循环计算totalPayout。

如,

public class Employee {
    private double hours;
    private double rate;

    public Employee(double hours, double rate) {
        this.hours = hours;
        this.rate = rate;
    }

    public double getSalary() {
        return hours * rate;
    }

    // method to change hours, get rate, get hours, etc...
}

public class Payroll {
    private List<Employee> employees = new ArrayList<>();

    public double getTotalPayout() {
        double totalSalary = 0;
        for (Employee employee : employees) {
            totalSalary += employee.getSalary();
        }
        return totalSalary;
    }

    // methods for adding Employee objects to list, for getting Employee...
    // etc...
}

编辑:根据您的作业要求,您必须在

中更改totalSalary和totalHours

1)构造函数和 2)increaseHours方法。

我会有一个calculateSalary方法,并在increaseHours中调用它来逐步增加totalHours和totalSalary的值。