Skip to content

Commit e01c852

Browse files
committed
Merge pull request iluwatar#30 from ruslanpa/master
Makes a few improvements, basically unnecessary declarations, formatting
2 parents c7a4a85 + 33cd28f commit e01c852

File tree

21 files changed

+300
-275
lines changed

21 files changed

+300
-275
lines changed

builder/src/main/java/com/iluwatar/HairType.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
public enum HairType {
44

5-
BALD, SHORT, CURLY, LONG_STRAIGHT, LONG_CURLY;
5+
BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY("long curly");
66

7-
@Override
7+
private String title;
8+
9+
HairType(String title) {
10+
this.title = title;
11+
}
12+
13+
@Override
814
public String toString() {
9-
return name().toLowerCase().replaceAll("_", " ");
15+
return title;
1016
}
11-
1217
}

command/src/main/java/com/iluwatar/Goblin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
public class Goblin extends Target {
44

55
public Goblin() {
6-
this.setSize(Size.NORMAL);
7-
this.setVisibility(Visibility.VISIBLE);
6+
setSize(Size.NORMAL);
7+
setVisibility(Visibility.VISIBLE);
88
}
99

1010
@Override

command/src/main/java/com/iluwatar/InvisibilitySpell.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ public class InvisibilitySpell extends Command {
44

55
private Target target;
66

7-
public InvisibilitySpell() {
8-
target = null;
9-
}
10-
117
@Override
128
public void execute(Target target) {
139
target.setVisibility(Visibility.INVISIBLE);

command/src/main/java/com/iluwatar/ShrinkSpell.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
public class ShrinkSpell extends Command {
44

55
private Size oldSize;
6-
76
private Target target;
87

9-
public ShrinkSpell() {
10-
oldSize = null;
11-
target = null;
12-
}
13-
148
@Override
159
public void execute(Target target) {
1610
oldSize = target.getSize();

command/src/main/java/com/iluwatar/Wizard.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ public class Wizard extends Target {
55
private Command previousSpell;
66

77
public Wizard() {
8-
this.setSize(Size.NORMAL);
9-
this.setVisibility(Visibility.VISIBLE);
10-
previousSpell = null;
8+
setSize(Size.NORMAL);
9+
setVisibility(Visibility.VISIBLE);
1110
}
1211

1312
public void castSpell(Command command, Target target) {
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.iluwatar;
22

33
import java.util.ArrayList;
4+
import java.util.Collection;
45
import java.util.List;
56

67
/**
@@ -15,7 +16,7 @@
1516
*/
1617
public class DwarvenGoldmineFacade {
1718

18-
List<DwarvenMineWorker> workers;
19+
private final List<DwarvenMineWorker> workers;
1920

2021
public DwarvenGoldmineFacade() {
2122
workers = new ArrayList<>();
@@ -25,23 +26,20 @@ public DwarvenGoldmineFacade() {
2526
}
2627

2728
public void startNewDay() {
28-
for (DwarvenMineWorker worker : workers) {
29-
worker.wakeUp();
30-
worker.goToMine();
31-
}
29+
makeActions(workers, DwarvenMineWorker.Action.WAKE_UP, DwarvenMineWorker.Action.GO_TO_MINE);
3230
}
3331

3432
public void digOutGold() {
35-
for (DwarvenMineWorker worker : workers) {
36-
worker.work();
37-
}
33+
makeActions(workers, DwarvenMineWorker.Action.WORK);
3834
}
3935

4036
public void endDay() {
41-
for (DwarvenMineWorker worker : workers) {
42-
worker.goHome();
43-
worker.goToSleep();
44-
}
37+
makeActions(workers, DwarvenMineWorker.Action.GO_HOME, DwarvenMineWorker.Action.GO_TO_SLEEP);
4538
}
4639

40+
private void makeActions(Collection<DwarvenMineWorker> workers, DwarvenMineWorker.Action... actions) {
41+
for (DwarvenMineWorker worker : workers) {
42+
worker.action(actions);
43+
}
44+
}
4745
}

facade/src/main/java/com/iluwatar/DwarvenMineWorker.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,40 @@ public void goToMine() {
2323
System.out.println(name() + " goes to the mine.");
2424
}
2525

26+
private void action(Action action) {
27+
switch (action) {
28+
case GO_TO_SLEEP:
29+
goToSleep();
30+
break;
31+
case WAKE_UP:
32+
wakeUp();
33+
break;
34+
case GO_HOME:
35+
goHome();
36+
break;
37+
case GO_TO_MINE:
38+
goToMine();
39+
break;
40+
case WORK:
41+
work();
42+
break;
43+
default:
44+
System.out.println("Undefined action");
45+
break;
46+
}
47+
}
48+
49+
public void action(Action... actions) {
50+
for (Action action : actions) {
51+
action(action);
52+
}
53+
}
54+
2655
public abstract void work();
2756

2857
public abstract String name();
2958

59+
static enum Action {
60+
GO_TO_SLEEP, WAKE_UP, GO_HOME, GO_TO_MINE, WORK
61+
}
3062
}

mediator/src/main/java/com/iluwatar/Action.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@
77
*/
88
public enum Action {
99

10-
HUNT("hunted a rabbit"), TALE("tells a tale"), GOLD("found gold"), ENEMY("spotted enemies"), NONE("");
10+
HUNT("hunted a rabbit", "arrives for dinner"),
11+
TALE("tells a tale", "comes to listen"),
12+
GOLD("found gold", "takes his share of the gold"),
13+
ENEMY("spotted enemies", "runs for cover"),
14+
NONE("", "");
1115

1216
private String title;
17+
private String description;
1318

14-
Action(String title) {
19+
Action(String title, String description) {
1520
this.title = title;
21+
this.description = description;
22+
}
23+
24+
public String getDescription() {
25+
return description;
1626
}
1727

1828
public String toString() {

mediator/src/main/java/com/iluwatar/PartyImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
public class PartyImpl implements Party {
1212

13-
private List<PartyMember> members;
13+
private final List<PartyMember> members;
1414

1515
public PartyImpl() {
1616
members = new ArrayList<>();

mediator/src/main/java/com/iluwatar/PartyMemberBase.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,7 @@ public void joinedParty(Party party) {
1717

1818
@Override
1919
public void partyAction(Action action) {
20-
String s = this + " ";
21-
switch (action) {
22-
case ENEMY:
23-
s = s + "runs for cover";
24-
break;
25-
case GOLD:
26-
s = s + "takes his share of the gold";
27-
break;
28-
case HUNT:
29-
s = s + "arrives for dinner";
30-
break;
31-
case TALE:
32-
s = s + "comes to listen";
33-
break;
34-
default:
35-
break;
36-
}
37-
System.out.println(s);
20+
System.out.println(this + " " + action.getDescription());
3821
}
3922

4023
@Override

0 commit comments

Comments
 (0)