Skip to content
Permalink
Browse files

Modified PayrollDay example from Item 34 (p. 166) to eliminate parame…

…terless

constructore, which ran counter to the purpose of the strategy enum  pattern.
This change corresponds to the forthcoming fourth printing.
  • Loading branch information...
jbloch committed Aug 13, 2018
1 parent 745394e commit 9e91e8a65c1847975dc81ef653f00c3b5c749c99
Showing with 11 additions and 4 deletions.
  1. +11 −4 src/effectivejava/chapter6/item34/PayrollDay.java
@@ -1,21 +1,23 @@
package effectivejava.chapter6.item34;

import static effectivejava.chapter6.item34.PayrollDay.PayType.*;

// The strategy enum pattern (Page 166)
enum PayrollDay {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
SATURDAY(PayType.WEEKEND), SUNDAY(PayType.WEEKEND);
MONDAY(WEEKDAY), TUESDAY(WEEKDAY), WEDNESDAY(WEEKDAY),
THURSDAY(WEEKDAY), FRIDAY(WEEKDAY),
SATURDAY(WEEKEND), SUNDAY(WEEKEND);

private final PayType payType;

PayrollDay(PayType payType) { this.payType = payType; }
PayrollDay() { this(PayType.WEEKDAY); } // Default

int pay(int minutesWorked, int payRate) {
return payType.pay(minutesWorked, payRate);
}

// The strategy enum type
private enum PayType {
enum PayType {
WEEKDAY {
int overtimePay(int minsWorked, int payRate) {
return minsWorked <= MINS_PER_SHIFT ? 0 :
@@ -36,4 +38,9 @@ int pay(int minsWorked, int payRate) {
return basePay + overtimePay(minsWorked, payRate);
}
}

public static void main(String[] args) {
for (PayrollDay day : values())
System.out.printf("%-10s%d%n", day, day.pay(8 * 60, 1));
}
}

0 comments on commit 9e91e8a

Please sign in to comment.
You can’t perform that action at this time.