我正在尝试计算向所有员工支付的总金额,但仍按员工1的原始工资而不是将小时数增加10后的工资。请告知我我做错了什么。

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


考试班

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

    }

最佳答案

您不应该在类的构造函数中计算总支出的加法,因为对其状态的更改不会反映在此字段中(如您所发现的)。如果必须在构造函数中执行此操作,则需要让您的类实现观察者类型设计,以将外部类的更改通知给您-某些您不需要或不需要的更改。相反,我将创建一个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方法,然后从incrementHours内调用它以递增地增加totalHours和totalSalary的值。

关于java - 程序可以运行,但是有点麻烦。 Java简单工资计划,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30517320/

10-10 12:46