使用 Java 实现枚举工资单
抱歉,只是尝试做一道过去的考试题来准备 Java 考试,希望有人能告诉我我的解决方案是否正确。问题:
使用 Java 枚举,实现一个用于工资计算的类。类中的常量对应于一周中的正常日子:星期一、星期二...星期五,周末日:星期六和星期日以及银行假日,这是一周中的特殊节日。
该类应该提供一个实例方法double pay (double time, double payrate)
,它返回当天工作的员工的总工资。计算规则如下: 给
定日期的工资由下式给出
base pay = pay rate * hours worked.
这里,基本工资由下式给出:
base pay = pay rate * hours worked
当天的加班工资由下式给出
overtime pay = pay rate * overtime hours/2
加班时间取决于日期的种类。
正常工作日:对于正常工作日,加班时间是指当天工作时间超过 8 小时的时间。
周末:周末的加班时间为当天的工作时间。
银行假日:银行假日加班时间为当天工作时间的 1.5 倍。
确保你的类是可维护的。应该可以在不破坏现有代码的情况下添加和删除。提示:使用策略枚举模式实现您的类。
public class Payroll {
public enum Day {
MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY, SUNDAY, BANK HOLIDAY;
}
public double pay( double time, double payrate ) {
if ( day
}
}
我真的不知道接下来该去哪里,有人可以帮助我吗?
Sorry, just attempting to do a past exam question in preparation for a Java exam and was hoping someone could tell me whether or not my solution is correct. The question :
Using Java enums, implement a class for payroll computation. The constants in the class correspond to the normal days of the week : Monday, Tuesday...Friday, weekend days : Saturday and Sunday and a Bank Holiday which is a special fay of the week.
The class should provide an instance method double pay ( double time, double payrate )
which returns the total pay of an employee who has worked on the current day. The rules for computation are as follows :
The pay for the given day given by
base pay = pay rate * hours worked.
Here, base pay is given by :
base pay = pay rate * hours worked
The overtime pay of that day is given by
overtime pay = pay rate * overtime hours/2
The overtime hours depend on the kind of day.
Normal weekday: For a normal weekday, overtime hours are the hours worked on that day in excess of 8 hours.
Weekend: For weekend days, the overtime hours are the hours worked on that day.
Bank holiday: For a bank holiday, the over time hours are 1.5 times the hours worked on that day.
Make sure your class is maintainable. It should be possible to add and remove without breaking existing code. Hint: implement your class with the strategy enum pattern.
public class Payroll {
public enum Day {
MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY, SUNDAY, BANK HOLIDAY;
}
public double pay( double time, double payrate ) {
if ( day
}
}
I can't really figure out where to go with this next, would anyone be able to help me with this please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个提示要求策略枚举模式。这是一个起点:
所以现在,如果你想计算工资,应该可以使用
或使用一个方便的(尚未实现的方法):
There is a hint asking for the strategy enum pattern. Here's a starting point:
So now, if you want to calculate the pay, it should be possible with
or with a convenient (yet to be implemented method):
正如 Andreas_D 的建议,您应该查看 策略枚举模式;例如,请参阅这篇文章。
这是一些快速破解的代码:
有了这些代码,您可以执行以下操作:
As suggest by Andreas_D, you should look at strategy enum pattern; see for example this article.
Here's some quickly hacked code:
With that in place, you can do things like: