以下是我的作业详情:
编写一个Java程序,可用于计算每小时付费员工的周薪。该 程序最初接受员工的身份,工作小时数和每小时工资率。一个 员工可能会额外工作发生这种情况时,必须增加额外的小时数 添加到原始金额,并重新计算工资。该程序必须单独输出 排除员工的身份证号码,工作小时数,小时工资率和工资 每个员工。此外,该计划还必须输出由支付的总金额 公司
这是我到目前为止所拥有的
包工资单;
public class Payroll
{
private static double TotalPayout;
private double Hours;
private double Hourly_Rate;
private String EmployeeId;
public static double Salary;
private double increaseHours = 10;
public Payroll (String getEmployeeId, double getHours, double
getHourly_Rate)
{
EmployeeId = getEmployeeId;
Hours = getHours;
Hourly_Rate = getHourly_Rate;
Salary = Hours * Hourly_Rate;
TotalPayout = TotalPayout + Salary;
}
public static double getTotalPayout()
{
return TotalPayout;
}
public String getEmployeeId ()
{
return EmployeeId;
}
public double getHours ()
{
return Hours;
}
public void increaseHours (double x)
{
Hours = increaseHours + Hours;
}
public double getHourly_Rate()
{
return Hourly_Rate;
}
public double calculateSalary()
{
Salary = Hours * Hourly_Rate;
return Salary;
}
}
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();
DateFormat df = DateFormat.getDateInstance();
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println("ABC Company");
System.out.println ("\nPayroll For Week Ending " + df.format (d));
System.out.println("--------------------------------------");
//Define employee 1
Payroll employee1 = new Payroll ("444-4444", 30, 25);
employee1.calculateSalary();
displaySalary (employee1, nf);
//Define employee 2
Payroll employee2 = new Payroll ("555-55555", 20, 50);
employee2.calculateSalary();
displaySalary(employee2, nf);
System.out.println("\tIncrease " + employee1.getEmployeeId() + " by 10
hours");
employee1.increaseHours(10); // 10 hours increase
System.out.println("\tEmployee # ...... " + employee1.getEmployeeId ());
System.out.println("\tHours Worked:.... " + employee1.getHours() + "
hours");
System.out.println("\tHourly Rate:..... " + nf.format(employee1.getHourly_Rate()) + "/hour");
System.out.println("\tYour Salary is .. " + nf.format(employee1.Salary));
System.out.println("\t------------------------------");
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.getHourly_Rate()) + "/hour");
System.out.println("\tYour Salary is .. " + nf.format(e.calculateSalary()));
System.out.println("\t------------------------------\n");
}
}
答案 0 :(得分:1)
您正在调用calculateSalary()
来更新总数,但之后您正在调用increaseHours()
,因此increaseHours()
还需要能够更新总数的代码。
我建议不要将TotalPayout计算放在构造函数中。为它创建一个单独的方法(就像你对calculateSalary所做的那样),然后你可以在需要的时候调用该方法。
答案 1 :(得分:0)
问题在于这种方法。
public void increaseHours (double x)
{
Hours = increaseHours + Hours;
}
首先,您使用了错误的字段(increaseHours
)而不是x
。所以请删除这一行:
private double increaseHours = 10;
现在更新你的方法:
public void increaseHours (double increaseHours)
{
Hours = increaseHours + Hours;
TotalPayout += increaseHours * Hourly_Rate;
}
现在这将解决您的问题,但我强烈建议 使用static
TotalPayout
字段 - 这将要求您稍微重新设计。
另外,还有一些风格指针。使用camel case以变量/字段的小写字母开头。例如totalPayout
代替TotalPayout
。等
欢迎来到S.O.和java世界,并进一步好运。
答案 2 :(得分:0)
当你在Payroll类中增加工资时,你不会更新totalPayout变量。
public void increaseHours(double x) {
hours = x + hours;
totalPayout += x * hourly_Rate;
}
我建议你检查你的代码,不要使用静态变量和方法来汇总所有Payroll对象(它是一个对象,而不是工资单的实用程序类),你有一些未使用的变量,正如Luud所写,属性应该是小写,等等。