如何在HourlyWorker类中实现计算加班工资的方法

huangapple go评论76阅读模式
英文:

How do I implement a method for calculating overtime pay in an HourlyWorker class

问题

以下是您提供的代码的翻译部分:

我有一个任务,要求计算下面代码中的内容。所有内容都正常工作 - 我只需要计算超过160小时的每月工时,按照正常小时费率的1.5倍支付。我的数学似乎是正确的,计算得很好:

((hours - 160) * overtime) + (160 * hourlyRate)

但是我不知道我是否应该将这个if语句放在正确的方法中,甚至是否应该是一个if语句。在此之前,我的增加/减少支付方法是有效的,并且它们需要保留。我删除了一些内容,以便阅读更容易。

HourlyWorker类:

public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;
private double overtime = (1.5 * hourlyRate);

public HourlyWorker(String last, String first, String ID, double rate)
{
   super(last, first, ID);
   hourlyRate = rate;
}

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

public int getHours()
{
   return hours;
}

public void setHourlyRate(double rate)
{
   this.hourlyRate = rate;
}

public double getHourlyRate()
{
   return hourlyRate;
}


public double getMonthlyPay()
{
   if (hours > 160)
   {
      monthlyPay = ((hours - 160) * overtime) + (160 * hourlyRate);
   }
   else 
   {
      monthlyPay = hourlyRate * hours;
   }
   return monthlyPay;
}

public void increasePay(double percentage)
{
   hourlyRate *= 1 + percentage / 100;
}

public void decreasePay(double percentage)
{
   hourlyRate *= 1 - percentage / 100;
}

}

测试内容:

public class TestEmployee2
{
   public static void main(String[] args)
   {
   Employee [] staff = new Employee[3];
      HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 10);
       
      hw1.setHours(200);	
      staff[0] = hw1;

   System.out.println(staff[0].getMonthlyPay());
   staff[0].increasePay(10);
   System.out.println(staff[0].getMonthlyPay());
}
}

输出结果:
1600  (初始每月费率,包括40小时的加班和160小时的正常工时)
1760  (月薪增加10%)

应该是:
2006
2206.6
英文:

I have an assignment which asks for everything I have in the code below. That all works fine - I just need to calculate any monthly hours over 160 hours to be paid at 1.5 times the normal hourly rate. My math seems sound and calculates fine:

> ((hours - 160) * overtime) + (160 * hourlyRate)

But I dont know if I'm putting this if statement in the right method or if it even should be an if statement. My increase/decreasePay methods are working prior to this and they need to stay. I removed some things so it's easier to read.

HourlyWorker Class:

public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;
private double overtime = (1.5 * hourlyRate);

public HourlyWorker(String last, String first, String ID, double rate)
{
   super(last, first, ID);
   hourlyRate = rate;
}

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

public int getHours()
{
   return hours;
}

public void setHourlyRate(double rate)
{
   this.hourlyRate = rate;
}

public double getHourlyRate()
{
   return hourlyRate;
}


public double getMonthlyPay()
{
   if (hours > 160)
   {
      monthlyPay = ((hours - 160) * overtime) + (160 * hourlyRate);
   }
   else 
   {
      monthlyPay = hourlyRate * hours;
   }
   return monthlyPay;
}

public void increasePay(double percentage)
{
   hourlyRate *= 1 + percentage / 100;
}

public void decreasePay(double percentage)
{
   hourlyRate *= 1 - percentage / 100;
}

}

What I'm testing with:

public class TestEmployee2
{
   public static void main(String[] args)
   {
   Employee [] staff = new Employee[3];
      HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 10);
       
      hw1.setHours(200);	
      staff[0] = hw1;

   System.out.println(staff[0].getMonthlyPay());
   staff[0].increasePay(10);
   System.out.println(staff[0].getMonthlyPay());
}
}

Output is:
1600 (initial monthly rate, with 40 overtime hours and 160 regular hours)
1760 (10% increase to the monthlyPay)

Should be:
2006
2206.6

答案1

得分: 2

你的代码存在以下问题:

  • 字段初始化程序在构造函数体之前运行,因此 overtime = (1.5 * hourlyRate) 使用 hourlyRate 字段的默认值 0,计算出一个 overtime 值为 0,且永远不会重新计算,因为初始化程序只在初始化期间运行一次。

  • setHourlyRate() 更新了 hourlyRate 字段,但未重新计算 overtime 字段的值。increasePay()decreasePay() 也是同样的情况。

  • monthlyPay 字段没有意义,因为你实际上从未使用过它,考虑到你只将它用作 getMonthlyPay() 方法中的局部变量。

消除 monthlyPayovertime 字段,将它们都作为 getMonthlyPay() 方法中的局部变量。

FYI: 在 Java 中,不建议使用 double 类型表示货币金额。推荐用于货币的数据类型是 BigDecimal

英文:

You code has the following issues:

  • Field initializers run before the body of the constructor, so overtime = (1.5 * hourlyRate) uses the default value of 0 for the hourlyRate field, calculating an overtime value of 0, and is never recalculated, since initializers only run once, during initialization.

  • setHourlyRate() updates the hourlyRate field, but doesn't re-calculate the value for the overtime field. Same for increasePay() and decreasePay().

  • There is no point to the monthlyPay field, since you never really use it, given that you only use it as-if it was a local variable in the getMonthlyPay() method.

Get rid of fields monthlyPay and overtime, and make them both local variables in the getMonthlyPay() method.

FYI: Using double for currency amounts is discouraged. The recommended type for currency in Java is BigDecimal.

huangapple
  • 本文由 发表于 2020年9月14日 01:14:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63873509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定