Skip to content

Commit 121ed3c

Browse files
committed
Hexagonal pattern: Move lottery administration and service to the core. Introduce console interfaces for players and administartors.
1 parent adc6019 commit 121ed3c

File tree

11 files changed

+103
-137
lines changed

11 files changed

+103
-137
lines changed

hexagonal/etc/hexagonal.ucls

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
<operations public="true" package="true" protected="true" private="true" static="true"/>
6363
</display>
6464
</interface>
65-
<class id="7" language="java" name="com.iluwatar.hexagonal.service.LotteryService" project="hexagonal"
65+
<class id="7" language="java" name="com.iluwatar.hexagonal.domain.LotteryService" project="hexagonal"
6666
file="/hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleService.java" binary="false"
6767
corner="BOTTOM_RIGHT">
6868
<position height="113" width="421" x="2029" y="122"/>
@@ -102,7 +102,7 @@
102102
<operations public="true" package="true" protected="true" private="true" static="true"/>
103103
</display>
104104
</class>
105-
<interface id="11" language="java" name="com.iluwatar.hexagonal.administration.LotteryAdministration"
105+
<interface id="11" language="java" name="com.iluwatar.hexagonal.domain.LotteryAdministration"
106106
project="hexagonal" file="/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/LotteryAdministration.java"
107107
binary="false" corner="BOTTOM_RIGHT">
108108
<position height="113" width="320" x="2808" y="347"/>
@@ -122,7 +122,7 @@
122122
<operations public="true" package="true" protected="true" private="true" static="true"/>
123123
</display>
124124
</class>
125-
<class id="13" language="java" name="com.iluwatar.hexagonal.administration.LotteryAdministration" project="hexagonal"
125+
<class id="13" language="java" name="com.iluwatar.hexagonal.domain.LotteryAdministration" project="hexagonal"
126126
file="/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java" binary="false"
127127
corner="BOTTOM_RIGHT">
128128
<position height="131" width="320" x="2490" y="122"/>
@@ -142,7 +142,7 @@
142142
<operations public="true" package="true" protected="true" private="true" static="true"/>
143143
</display>
144144
</class>
145-
<interface id="15" language="java" name="com.iluwatar.hexagonal.service.LotteryService" project="hexagonal"
145+
<interface id="15" language="java" name="com.iluwatar.hexagonal.domain.LotteryService" project="hexagonal"
146146
file="/hexagonal/src/main/java/com/iluwatar/hexagonal/service/LotteryService.java" binary="false"
147147
corner="BOTTOM_RIGHT">
148148
<position height="95" width="421" x="2347" y="347"/>

hexagonal/src/main/java/com/iluwatar/hexagonal/App.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828

2929
import com.google.inject.Guice;
3030
import com.google.inject.Injector;
31-
import com.iluwatar.hexagonal.administration.LotteryAdministration;
31+
import com.iluwatar.hexagonal.domain.LotteryAdministration;
3232
import com.iluwatar.hexagonal.banking.InMemoryBank;
3333
import com.iluwatar.hexagonal.domain.LotteryConstants;
3434
import com.iluwatar.hexagonal.domain.LotteryNumbers;
3535
import com.iluwatar.hexagonal.domain.LotteryTicket;
3636
import com.iluwatar.hexagonal.domain.PlayerDetails;
37-
import com.iluwatar.hexagonal.service.LotteryService;
37+
import com.iluwatar.hexagonal.domain.LotteryService;
3838

3939
/**
4040
*

hexagonal/src/main/java/com/iluwatar/hexagonal/LotteryModule.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
3030
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
3131
import com.iluwatar.hexagonal.notifications.StdOutNotifications;
32-
import com.iluwatar.hexagonal.service.LotteryService;
3332

3433
/**
3534
* Guice module for binding production dependencies
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.iluwatar.hexagonal.administration;
2+
3+
/**
4+
* Console interface for lottery administration
5+
*/
6+
public class ConsoleAdministration {
7+
}

hexagonal/src/main/java/com/iluwatar/hexagonal/administration/LotteryAdministration.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotterySystem.java renamed to hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryAdministration.java

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,26 @@
2828
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
2929

3030
import java.util.Map;
31-
import java.util.Optional;
3231

3332
/**
34-
* Lottery system
33+
*
34+
* Lottery administration implementation
35+
*
3536
*/
36-
public class LotterySystem {
37+
public class LotteryAdministration {
3738

3839
private final LotteryTicketRepository repository;
3940
private final LotteryNotifications notifications;
4041
private final WireTransfers wireTransfers;
42+
private final LotteryTicketChecker checker;
4143

42-
/**
43-
* Constructor
44-
*/
4544
@Inject
46-
public LotterySystem(LotteryTicketRepository repository, LotteryNotifications notifications,
47-
WireTransfers wireTransfers) {
45+
public LotteryAdministration(LotteryTicketRepository repository, LotteryNotifications notifications,
46+
WireTransfers wireTransfers) {
4847
this.repository = repository;
4948
this.notifications = notifications;
5049
this.wireTransfers = wireTransfers;
50+
this.checker = new LotteryTicketChecker(this.repository);
5151
}
5252

5353
/**
@@ -64,7 +64,7 @@ public LotteryNumbers performLottery() {
6464
LotteryNumbers numbers = LotteryNumbers.createRandom();
6565
Map<LotteryTicketId, LotteryTicket> tickets = getAllSubmittedTickets();
6666
for (LotteryTicketId id : tickets.keySet()) {
67-
LotteryTicketCheckResult result = checkTicketForPrize(id, numbers);
67+
LotteryTicketCheckResult result = checker.checkTicketForPrize(id, numbers);
6868
if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) {
6969
boolean transferred = wireTransfers.transferFunds(LotteryConstants.PRIZE_AMOUNT,
7070
LotteryConstants.SERVICE_BANK_ACCOUNT, tickets.get(id).getPlayerDetails().getBankAccount());
@@ -86,37 +86,4 @@ public LotteryNumbers performLottery() {
8686
public void resetLottery() {
8787
repository.deleteAll();
8888
}
89-
90-
/**
91-
* Submit lottery ticket to participate in the lottery
92-
*/
93-
public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
94-
boolean result = wireTransfers.transferFunds(LotteryConstants.TICKET_PRIZE,
95-
ticket.getPlayerDetails().getBankAccount(), LotteryConstants.SERVICE_BANK_ACCOUNT);
96-
if (result == false) {
97-
notifications.notifyTicketSubmitError(ticket.getPlayerDetails());
98-
return Optional.empty();
99-
}
100-
Optional<LotteryTicketId> optional = repository.save(ticket);
101-
if (optional.isPresent()) {
102-
notifications.notifyTicketSubmitted(ticket.getPlayerDetails());
103-
}
104-
return optional;
105-
}
106-
107-
/**
108-
* Check if lottery ticket has won
109-
*/
110-
public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) {
111-
Optional<LotteryTicket> optional = repository.findById(id);
112-
if (optional.isPresent()) {
113-
if (optional.get().getNumbers().equals(winningNumbers)) {
114-
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.WIN_PRIZE, 1000);
115-
} else {
116-
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.NO_PRIZE);
117-
}
118-
} else {
119-
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.TICKET_NOT_SUBMITTED);
120-
}
121-
}
12289
}

hexagonal/src/main/java/com/iluwatar/hexagonal/service/LotteryService.java renamed to hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryService.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
* THE SOFTWARE.
2222
*/
23-
package com.iluwatar.hexagonal.service;
23+
package com.iluwatar.hexagonal.domain;
2424

2525
import com.google.inject.Inject;
26-
import com.iluwatar.hexagonal.domain.*;
26+
import com.iluwatar.hexagonal.banking.WireTransfers;
27+
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
28+
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
2729

2830
import java.util.Optional;
2931

@@ -34,27 +36,44 @@
3436
*/
3537
public class LotteryService {
3638

37-
private final LotterySystem lotterySystem;
39+
private final LotteryTicketRepository repository;
40+
private final LotteryNotifications notifications;
41+
private final WireTransfers wireTransfers;
42+
private final LotteryTicketChecker checker;
3843

3944
/**
4045
* Constructor
4146
*/
4247
@Inject
43-
public LotteryService(LotterySystem lotterySystem) {
44-
this.lotterySystem = lotterySystem;
48+
public LotteryService(LotteryTicketRepository repository, LotteryNotifications notifications,
49+
WireTransfers wireTransfers) {
50+
this.repository = repository;
51+
this.notifications = notifications;
52+
this.wireTransfers = wireTransfers;
53+
this.checker = new LotteryTicketChecker(this.repository);
4554
}
4655

4756
/**
4857
* Submit lottery ticket to participate in the lottery
4958
*/
5059
public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
51-
return lotterySystem.submitTicket(ticket);
60+
boolean result = wireTransfers.transferFunds(LotteryConstants.TICKET_PRIZE,
61+
ticket.getPlayerDetails().getBankAccount(), LotteryConstants.SERVICE_BANK_ACCOUNT);
62+
if (result == false) {
63+
notifications.notifyTicketSubmitError(ticket.getPlayerDetails());
64+
return Optional.empty();
65+
}
66+
Optional<LotteryTicketId> optional = repository.save(ticket);
67+
if (optional.isPresent()) {
68+
notifications.notifyTicketSubmitted(ticket.getPlayerDetails());
69+
}
70+
return optional;
5271
}
5372

5473
/**
5574
* Check if lottery ticket has won
5675
*/
5776
public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) {
58-
return lotterySystem.checkTicketForPrize(id, winningNumbers);
77+
return checker.checkTicketForPrize(id, winningNumbers);
5978
}
6079
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.iluwatar.hexagonal.domain;
2+
3+
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
4+
5+
import java.util.Optional;
6+
7+
/**
8+
* Lottery ticket checker
9+
*/
10+
public class LotteryTicketChecker {
11+
12+
private final LotteryTicketRepository repository;
13+
14+
public LotteryTicketChecker(LotteryTicketRepository repository) {
15+
this.repository = repository;
16+
}
17+
18+
/**
19+
* Check if lottery ticket has won
20+
*/
21+
public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) {
22+
Optional<LotteryTicket> optional = repository.findById(id);
23+
if (optional.isPresent()) {
24+
if (optional.get().getNumbers().equals(winningNumbers)) {
25+
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.WIN_PRIZE, 1000);
26+
} else {
27+
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.NO_PRIZE);
28+
}
29+
} else {
30+
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.TICKET_NOT_SUBMITTED);
31+
}
32+
}
33+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.iluwatar.hexagonal.service;
2+
3+
/**
4+
* Console interface for lottery players
5+
*/
6+
public class ConsoleLottery {
7+
}

hexagonal/src/test/java/com/iluwatar/hexagonal/LotteryTestingModule.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
3030
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
3131
import com.iluwatar.hexagonal.notifications.StdOutNotifications;
32-
import com.iluwatar.hexagonal.service.LotteryService;
3332

3433
/**
3534
* Guice module for testing dependencies

0 commit comments

Comments
 (0)