Skip to content

Commit 9e91e8a

Browse files
committed
Modified PayrollDay example from Item 34 (p. 166) to eliminate parameterless
constructore, which ran counter to the purpose of the strategy enum pattern. This change corresponds to the forthcoming fourth printing.
1 parent 745394e commit 9e91e8a

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/effectivejava/chapter6/item34/PayrollDay.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package effectivejava.chapter6.item34;
22

3+
import static effectivejava.chapter6.item34.PayrollDay.PayType.*;
4+
35
// The strategy enum pattern (Page 166)
46
enum PayrollDay {
5-
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
6-
SATURDAY(PayType.WEEKEND), SUNDAY(PayType.WEEKEND);
7+
MONDAY(WEEKDAY), TUESDAY(WEEKDAY), WEDNESDAY(WEEKDAY),
8+
THURSDAY(WEEKDAY), FRIDAY(WEEKDAY),
9+
SATURDAY(WEEKEND), SUNDAY(WEEKEND);
710

811
private final PayType payType;
912

1013
PayrollDay(PayType payType) { this.payType = payType; }
11-
PayrollDay() { this(PayType.WEEKDAY); } // Default
1214

1315
int pay(int minutesWorked, int payRate) {
1416
return payType.pay(minutesWorked, payRate);
1517
}
1618

1719
// The strategy enum type
18-
private enum PayType {
20+
enum PayType {
1921
WEEKDAY {
2022
int overtimePay(int minsWorked, int payRate) {
2123
return minsWorked <= MINS_PER_SHIFT ? 0 :
@@ -36,4 +38,9 @@ int pay(int minsWorked, int payRate) {
3638
return basePay + overtimePay(minsWorked, payRate);
3739
}
3840
}
41+
42+
public static void main(String[] args) {
43+
for (PayrollDay day : values())
44+
System.out.printf("%-10s%d%n", day, day.pay(8 * 60, 1));
45+
}
3946
}

0 commit comments

Comments
 (0)