Skip to content

Commit e17d72b

Browse files
committed
Hexagonal pattern: Added console interfaces for players and administration.
1 parent 121ed3c commit e17d72b

File tree

13 files changed

+374
-80
lines changed

13 files changed

+374
-80
lines changed

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

Lines changed: 5 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,12 @@
2222
*/
2323
package com.iluwatar.hexagonal;
2424

25-
import java.util.ArrayList;
26-
import java.util.List;
27-
import java.util.Random;
28-
2925
import com.google.inject.Guice;
3026
import com.google.inject.Injector;
3127
import com.iluwatar.hexagonal.domain.LotteryAdministration;
32-
import com.iluwatar.hexagonal.banking.InMemoryBank;
33-
import com.iluwatar.hexagonal.domain.LotteryConstants;
34-
import com.iluwatar.hexagonal.domain.LotteryNumbers;
35-
import com.iluwatar.hexagonal.domain.LotteryTicket;
36-
import com.iluwatar.hexagonal.domain.PlayerDetails;
3728
import com.iluwatar.hexagonal.domain.LotteryService;
29+
import com.iluwatar.hexagonal.module.LotteryTestingModule;
30+
import com.iluwatar.hexagonal.sampledata.SampleData;
3831

3932
/**
4033
*
@@ -68,87 +61,23 @@
6861
*
6962
*/
7063
public class App {
71-
72-
private static final List<PlayerDetails> PLAYERS;
73-
74-
static {
75-
PLAYERS = new ArrayList<>();
76-
PLAYERS.add(PlayerDetails.create("john@google.com", "312-342", "+3242434242"));
77-
PLAYERS.add(PlayerDetails.create("mary@google.com", "234-987", "+23452346"));
78-
PLAYERS.add(PlayerDetails.create("steve@google.com", "833-836", "+63457543"));
79-
PLAYERS.add(PlayerDetails.create("wayne@google.com", "319-826", "+24626"));
80-
PLAYERS.add(PlayerDetails.create("johnie@google.com", "983-322", "+3635635"));
81-
PLAYERS.add(PlayerDetails.create("andy@google.com", "934-734", "+0898245"));
82-
PLAYERS.add(PlayerDetails.create("richard@google.com", "536-738", "+09845325"));
83-
PLAYERS.add(PlayerDetails.create("kevin@google.com", "453-936", "+2423532"));
84-
PLAYERS.add(PlayerDetails.create("arnold@google.com", "114-988", "+5646346524"));
85-
PLAYERS.add(PlayerDetails.create("ian@google.com", "663-765", "+928394235"));
86-
PLAYERS.add(PlayerDetails.create("robin@google.com", "334-763", "+35448"));
87-
PLAYERS.add(PlayerDetails.create("ted@google.com", "735-964", "+98752345"));
88-
PLAYERS.add(PlayerDetails.create("larry@google.com", "734-853", "+043842423"));
89-
PLAYERS.add(PlayerDetails.create("calvin@google.com", "334-746", "+73294135"));
90-
PLAYERS.add(PlayerDetails.create("jacob@google.com", "444-766", "+358042354"));
91-
PLAYERS.add(PlayerDetails.create("edwin@google.com", "895-345", "+9752435"));
92-
PLAYERS.add(PlayerDetails.create("mary@google.com", "760-009", "+34203542"));
93-
PLAYERS.add(PlayerDetails.create("lolita@google.com", "425-907", "+9872342"));
94-
PLAYERS.add(PlayerDetails.create("bruno@google.com", "023-638", "+673824122"));
95-
PLAYERS.add(PlayerDetails.create("peter@google.com", "335-886", "+5432503945"));
96-
PLAYERS.add(PlayerDetails.create("warren@google.com", "225-946", "+9872341324"));
97-
PLAYERS.add(PlayerDetails.create("monica@google.com", "265-748", "+134124"));
98-
PLAYERS.add(PlayerDetails.create("ollie@google.com", "190-045", "+34453452"));
99-
PLAYERS.add(PlayerDetails.create("yngwie@google.com", "241-465", "+9897641231"));
100-
PLAYERS.add(PlayerDetails.create("lars@google.com", "746-936", "+42345298345"));
101-
PLAYERS.add(PlayerDetails.create("bobbie@google.com", "946-384", "+79831742"));
102-
PLAYERS.add(PlayerDetails.create("tyron@google.com", "310-992", "+0498837412"));
103-
PLAYERS.add(PlayerDetails.create("tyrell@google.com", "032-045", "+67834134"));
104-
PLAYERS.add(PlayerDetails.create("nadja@google.com", "000-346", "+498723"));
105-
PLAYERS.add(PlayerDetails.create("wendy@google.com", "994-989", "+987324454"));
106-
PLAYERS.add(PlayerDetails.create("luke@google.com", "546-634", "+987642435"));
107-
PLAYERS.add(PlayerDetails.create("bjorn@google.com", "342-874", "+7834325"));
108-
PLAYERS.add(PlayerDetails.create("lisa@google.com", "024-653", "+980742154"));
109-
PLAYERS.add(PlayerDetails.create("anton@google.com", "834-935", "+876423145"));
110-
PLAYERS.add(PlayerDetails.create("bruce@google.com", "284-936", "+09843212345"));
111-
PLAYERS.add(PlayerDetails.create("ray@google.com", "843-073", "+678324123"));
112-
PLAYERS.add(PlayerDetails.create("ron@google.com", "637-738", "+09842354"));
113-
PLAYERS.add(PlayerDetails.create("xavier@google.com", "143-947", "+375245"));
114-
PLAYERS.add(PlayerDetails.create("harriet@google.com", "842-404", "+131243252"));
115-
InMemoryBank wireTransfers = new InMemoryBank();
116-
Random random = new Random();
117-
for (int i = 0; i < PLAYERS.size(); i++) {
118-
wireTransfers.setFunds(PLAYERS.get(i).getBankAccount(),
119-
random.nextInt(LotteryConstants.PLAYER_MAX_SALDO));
120-
}
121-
}
122-
64+
12365
/**
12466
* Program entry point
12567
*/
12668
public static void main(String[] args) {
12769

128-
Injector injector = Guice.createInjector(new LotteryModule());
70+
Injector injector = Guice.createInjector(new LotteryTestingModule());
12971

13072
// start new lottery round
13173
LotteryAdministration administartion = injector.getInstance(LotteryAdministration.class);
13274
administartion.resetLottery();
13375

13476
// submit some lottery tickets
13577
LotteryService service = injector.getInstance(LotteryService.class);
136-
submitTickets(service, 20);
78+
SampleData.submitTickets(service, 20);
13779

13880
// perform lottery
13981
administartion.performLottery();
14082
}
141-
142-
private static void submitTickets(LotteryService lotteryService, int numTickets) {
143-
for (int i = 0; i < numTickets; i++) {
144-
LotteryTicket ticket = LotteryTicket.create(getRandomPlayerDetails(), LotteryNumbers.createRandom());
145-
lotteryService.submitTicket(ticket);
146-
}
147-
}
148-
149-
private static PlayerDetails getRandomPlayerDetails() {
150-
Random random = new Random();
151-
int idx = random.nextInt(PLAYERS.size());
152-
return PLAYERS.get(idx);
153-
}
15483
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,84 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
123
package com.iluwatar.hexagonal.administration;
224

25+
import com.google.inject.Guice;
26+
import com.google.inject.Injector;
27+
import com.iluwatar.hexagonal.domain.LotteryAdministration;
28+
import com.iluwatar.hexagonal.domain.LotteryNumbers;
29+
import com.iluwatar.hexagonal.domain.LotteryService;
30+
import com.iluwatar.hexagonal.module.LotteryModule;
31+
import com.iluwatar.hexagonal.sampledata.SampleData;
32+
33+
import java.util.Scanner;
34+
335
/**
436
* Console interface for lottery administration
537
*/
638
public class ConsoleAdministration {
39+
40+
/**
41+
* Program entry point
42+
*/
43+
public static void main(String[] args) {
44+
Injector injector = Guice.createInjector(new LotteryModule());
45+
LotteryAdministration administartion = injector.getInstance(LotteryAdministration.class);
46+
LotteryService service = injector.getInstance(LotteryService.class);
47+
SampleData.submitTickets(service, 20);
48+
Scanner scanner = new Scanner(System.in);
49+
boolean exit = false;
50+
while (!exit) {
51+
printMainMenu();
52+
String cmd = readString(scanner);
53+
if (cmd.equals("1")) {
54+
administartion.getAllSubmittedTickets().forEach((k,v)->System.out.println("Key: " + k + " Value: " + v));
55+
} else if (cmd.equals("2")) {
56+
LotteryNumbers numbers = administartion.performLottery();
57+
System.out.println("The winning numbers: " + numbers);
58+
System.out.println("Time to reset the database for next round, eh?");
59+
} else if (cmd.equals("3")) {
60+
administartion.resetLottery();
61+
System.out.println("The lottery ticket database was cleared.");
62+
} else if (cmd.equals("4")) {
63+
exit = true;
64+
} else {
65+
System.out.println("Unknown command: " + cmd);
66+
}
67+
}
68+
}
69+
70+
private static void printMainMenu() {
71+
System.out.println("");
72+
System.out.println("### Lottery Administration Console ###");
73+
System.out.println("(1) Show all submitted tickets");
74+
System.out.println("(2) Perform lottery draw");
75+
System.out.println("(3) Reset lottery ticket database");
76+
System.out.println("(4) Exit");
77+
}
78+
79+
private static String readString(Scanner scanner) {
80+
System.out.print("> ");
81+
String cmd = scanner.next();
82+
return cmd;
83+
}
784
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class LotteryAdministration {
4141
private final WireTransfers wireTransfers;
4242
private final LotteryTicketChecker checker;
4343

44+
/**
45+
* Constructor
46+
*/
4447
@Inject
4548
public LotteryAdministration(LotteryTicketRepository repository, LotteryNotifications notifications,
4649
WireTransfers wireTransfers) {

hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryNumbers.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ private void generateRandomNumbers() {
9292
}
9393
}
9494
}
95-
95+
96+
@Override
97+
public String toString() {
98+
return "LotteryNumbers{" + "numbers=" + numbers + '}';
99+
}
100+
96101
/**
97102
*
98103
* Helper class for generating random numbers.

hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicket.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public LotteryNumbers getNumbers() {
6161
return lotteryNumbers;
6262
}
6363

64+
@Override
65+
public String toString() {
66+
return playerDetails.toString() + " " + lotteryNumbers.toString();
67+
}
68+
6469
@Override
6570
public int hashCode() {
6671
final int prime = 31;

hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicketChecker.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
123
package com.iluwatar.hexagonal.domain;
224

325
import com.iluwatar.hexagonal.database.LotteryTicketRepository;

hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicketId.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,17 @@ public class LotteryTicketId {
3434
public LotteryTicketId() {
3535
id = UUID.randomUUID();
3636
}
37+
38+
public LotteryTicketId(String str) {
39+
id = UUID.fromString(str);
40+
}
3741

3842
public UUID getId() {
3943
return id;
4044
}
45+
46+
@Override
47+
public String toString() {
48+
return id.toString();
49+
}
4150
}

hexagonal/src/main/java/com/iluwatar/hexagonal/domain/PlayerDetails.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ public String getPhoneNumber() {
7070
return phoneNumber;
7171
}
7272

73+
@Override
74+
public String toString() {
75+
return "PlayerDetails{" + "emailAddress='" + emailAddress + '\''
76+
+ ", bankAccountNumber='" + bankAccountNumber + '\''
77+
+ ", phoneNumber='" + phoneNumber + '\'' + '}';
78+
}
79+
7380
@Override
7481
public int hashCode() {
7582
final int prime = 31;

hexagonal/src/main/java/com/iluwatar/hexagonal/LotteryModule.java renamed to hexagonal/src/main/java/com/iluwatar/hexagonal/module/LotteryModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
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;
23+
package com.iluwatar.hexagonal.module;
2424

2525
import com.google.inject.AbstractModule;
2626
import com.iluwatar.hexagonal.banking.InMemoryBank;

hexagonal/src/test/java/com/iluwatar/hexagonal/LotteryTestingModule.java renamed to hexagonal/src/main/java/com/iluwatar/hexagonal/module/LotteryTestingModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
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;
23+
package com.iluwatar.hexagonal.module;
2424

2525
import com.google.inject.AbstractModule;
2626
import com.iluwatar.hexagonal.banking.InMemoryBank;

0 commit comments

Comments
 (0)