Enum example
“LNSL”
is a construction company. It pays daily wages to its workers. If a worker
works (on weekdays) for 8hours then payment will be made per the
agreed/standard rate. If the worker works for more than 8 hours then the extra
hours worked will be paid at 1.5 times the agreed rate. During weekends, the
rate will be 1.5 times the standard rate.
Note
: to do this example use “enum”.
Solution:
package com.spaneos.core;
enum PayrollDay{
MONDAY(PayType.WEEKDAY),TUESDAY(PayType.WEEKDAY),
WEDNESDAY(PayType.WEEKDAY),THURSDAY(PayType.WEEKDAY),
FRIDAY(PayType.WEEKDAY),SATURDAY(PayType.WEEKEND),
SUNDAY(PayType.WEEKEND);
private final PayType payType;
PayrollDay(PayType payType){
this.payType=payType;
}
double pay(double hoursWorked,double payRate){
return payType.pay(hoursWorked,payRate);
}
private enum PayType{
WEEKDAY{
double overtimePay(double hrs,double payRate) {
return hrs<=HOURS_PER_SHIFT?0:hrs- HOURS_PER_SHIFT)*payRate/2;
}
},
WEEKEND{
double overtimePay(double hrs,double payRate){
return hrs*payRate/2;
}
};
private static final int HOURS_PER_SHIFT=8;
abstract double overtimePay(double hrs,double payRate);
double pay(double hrsworked,double payRate){
double basePay=hrsworked*payRate;
return basePay+overtimePay(hrsworked,
payRate);
}
}
}
public class PaymentManager {
public static void main(String[] args) {
double amount=PayrollDay.SUNDAY.pay(5, 20);
System.out.println(amount);
}
}
No comments:
Post a Comment