Skip to content

Commit c34004b

Browse files
authored
Merge pull request iluwatar#763 from 7agustibm/master
First proposal for iluwatar#586 Hexagonal Architecture primary ports should have interfaces
2 parents facb9e5 + b453753 commit c34004b

File tree

6 files changed

+210
-90
lines changed

6 files changed

+210
-90
lines changed

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.inject.Guice;
2626
import com.google.inject.Injector;
2727
import com.iluwatar.hexagonal.domain.LotteryAdministration;
28-
import com.iluwatar.hexagonal.domain.LotteryNumbers;
2928
import com.iluwatar.hexagonal.domain.LotteryService;
3029
import com.iluwatar.hexagonal.module.LotteryModule;
3130
import com.iluwatar.hexagonal.mongo.MongoConnectionPropertiesLoader;
@@ -48,23 +47,21 @@ public class ConsoleAdministration {
4847
public static void main(String[] args) {
4948
MongoConnectionPropertiesLoader.load();
5049
Injector injector = Guice.createInjector(new LotteryModule());
51-
LotteryAdministration administartion = injector.getInstance(LotteryAdministration.class);
50+
LotteryAdministration administration = injector.getInstance(LotteryAdministration.class);
5251
LotteryService service = injector.getInstance(LotteryService.class);
5352
SampleData.submitTickets(service, 20);
53+
ConsoleAdministrationSrv consoleAdministration = new ConsoleAdministrationSrvImpl(administration, LOGGER);
5454
try (Scanner scanner = new Scanner(System.in)) {
5555
boolean exit = false;
5656
while (!exit) {
5757
printMainMenu();
5858
String cmd = readString(scanner);
5959
if ("1".equals(cmd)) {
60-
administartion.getAllSubmittedTickets().forEach((k, v) -> LOGGER.info("Key: {}, Value: {}", k, v));
60+
consoleAdministration.getAllSubmittedTickets();
6161
} else if ("2".equals(cmd)) {
62-
LotteryNumbers numbers = administartion.performLottery();
63-
LOGGER.info("The winning numbers: {}", numbers.getNumbersAsString());
64-
LOGGER.info("Time to reset the database for next round, eh?");
62+
consoleAdministration.performLottery();
6563
} else if ("3".equals(cmd)) {
66-
administartion.resetLottery();
67-
LOGGER.info("The lottery ticket database was cleared.");
64+
consoleAdministration.resetLottery();
6865
} else if ("4".equals(cmd)) {
6966
exit = true;
7067
} else {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.iluwatar.hexagonal.administration;
2+
3+
/**
4+
* Console interface for lottery administration
5+
*/
6+
public interface ConsoleAdministrationSrv {
7+
8+
/**
9+
* Get all submitted tickets
10+
*/
11+
void getAllSubmittedTickets();
12+
13+
/**
14+
* Draw lottery numbers
15+
*/
16+
void performLottery();
17+
18+
/**
19+
* Begin new lottery round
20+
*/
21+
void resetLottery();
22+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.iluwatar.hexagonal.administration;
2+
3+
import com.iluwatar.hexagonal.domain.LotteryAdministration;
4+
import com.iluwatar.hexagonal.domain.LotteryNumbers;
5+
import org.slf4j.Logger;
6+
7+
/**
8+
* Console implementation for lottery administration
9+
*/
10+
public class ConsoleAdministrationSrvImpl implements ConsoleAdministrationSrv {
11+
private final LotteryAdministration administration;
12+
private final Logger logger;
13+
14+
/**
15+
* Constructor
16+
*/
17+
public ConsoleAdministrationSrvImpl(LotteryAdministration administration, Logger logger) {
18+
this.administration = administration;
19+
this.logger = logger;
20+
}
21+
22+
@Override
23+
public void getAllSubmittedTickets() {
24+
administration.getAllSubmittedTickets().forEach((k, v) -> logger.info("Key: {}, Value: {}", k, v));
25+
}
26+
27+
@Override
28+
public void performLottery() {
29+
LotteryNumbers numbers = administration.performLottery();
30+
logger.info("The winning numbers: {}", numbers.getNumbersAsString());
31+
logger.info("Time to reset the database for next round, eh?");
32+
}
33+
34+
@Override
35+
public void resetLottery() {
36+
administration.resetLottery();
37+
logger.info("The lottery ticket database was cleared.");
38+
}
39+
}

hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleLottery.java

Lines changed: 6 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,13 @@
2525
import com.google.inject.Guice;
2626
import com.google.inject.Injector;
2727
import com.iluwatar.hexagonal.banking.WireTransfers;
28-
import com.iluwatar.hexagonal.domain.LotteryNumbers;
2928
import com.iluwatar.hexagonal.domain.LotteryService;
30-
import com.iluwatar.hexagonal.domain.LotteryTicket;
31-
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult;
32-
import com.iluwatar.hexagonal.domain.LotteryTicketId;
33-
import com.iluwatar.hexagonal.domain.PlayerDetails;
3429
import com.iluwatar.hexagonal.module.LotteryModule;
3530
import com.iluwatar.hexagonal.mongo.MongoConnectionPropertiesLoader;
3631
import org.slf4j.Logger;
3732
import org.slf4j.LoggerFactory;
3833

39-
import java.util.HashSet;
40-
import java.util.Optional;
4134
import java.util.Scanner;
42-
import java.util.Set;
4335

4436
/**
4537
* Console interface for lottery players
@@ -54,21 +46,22 @@ public class ConsoleLottery {
5446
public static void main(String[] args) {
5547
MongoConnectionPropertiesLoader.load();
5648
Injector injector = Guice.createInjector(new LotteryModule());
57-
LotteryService service = injector.getInstance(LotteryService.class);
49+
LotteryService service = injector.getInstance( LotteryService.class);
5850
WireTransfers bank = injector.getInstance(WireTransfers.class);
5951
try (final Scanner scanner = new Scanner(System.in)) {
6052
boolean exit = false;
6153
while (!exit) {
6254
printMainMenu();
6355
String cmd = readString(scanner);
56+
LotteryConsoleService lotteryConsoleService = new LotteryConsoleServiceImpl(LOGGER);
6457
if ("1".equals(cmd)) {
65-
queryLotteryAccountFunds(bank, scanner);
58+
lotteryConsoleService.queryLotteryAccountFunds(bank, scanner);
6659
} else if ("2".equals(cmd)) {
67-
addFundsToLotteryAccount(bank, scanner);
60+
lotteryConsoleService.addFundsToLotteryAccount(bank, scanner);
6861
} else if ("3".equals(cmd)) {
69-
submitTicket(service, scanner);
62+
lotteryConsoleService.submitTicket(service, scanner);
7063
} else if ("4".equals(cmd)) {
71-
checkTicket(service, scanner);
64+
lotteryConsoleService.checkTicket(service, scanner);
7265
} else if ("5".equals(cmd)) {
7366
exit = true;
7467
} else {
@@ -78,75 +71,6 @@ public static void main(String[] args) {
7871
}
7972
}
8073

81-
private static void checkTicket(LotteryService service, Scanner scanner) {
82-
LOGGER.info("What is the ID of the lottery ticket?");
83-
String id = readString(scanner);
84-
LOGGER.info("Give the 4 comma separated winning numbers?");
85-
String numbers = readString(scanner);
86-
try {
87-
String[] parts = numbers.split(",");
88-
Set<Integer> winningNumbers = new HashSet<>();
89-
for (int i = 0; i < 4; i++) {
90-
winningNumbers.add(Integer.parseInt(parts[i]));
91-
}
92-
LotteryTicketCheckResult result = service.checkTicketForPrize(
93-
new LotteryTicketId(Integer.parseInt(id)), LotteryNumbers.create(winningNumbers));
94-
if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) {
95-
LOGGER.info("Congratulations! The lottery ticket has won!");
96-
} else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) {
97-
LOGGER.info("Unfortunately the lottery ticket did not win.");
98-
} else {
99-
LOGGER.info("Such lottery ticket has not been submitted.");
100-
}
101-
} catch (Exception e) {
102-
LOGGER.info("Failed checking the lottery ticket - please try again.");
103-
}
104-
}
105-
106-
private static void submitTicket(LotteryService service, Scanner scanner) {
107-
LOGGER.info("What is your email address?");
108-
String email = readString(scanner);
109-
LOGGER.info("What is your bank account number?");
110-
String account = readString(scanner);
111-
LOGGER.info("What is your phone number?");
112-
String phone = readString(scanner);
113-
PlayerDetails details = new PlayerDetails(email, account, phone);
114-
LOGGER.info("Give 4 comma separated lottery numbers?");
115-
String numbers = readString(scanner);
116-
try {
117-
String[] parts = numbers.split(",");
118-
Set<Integer> chosen = new HashSet<>();
119-
for (int i = 0; i < 4; i++) {
120-
chosen.add(Integer.parseInt(parts[i]));
121-
}
122-
LotteryNumbers lotteryNumbers = LotteryNumbers.create(chosen);
123-
LotteryTicket lotteryTicket = new LotteryTicket(new LotteryTicketId(), details, lotteryNumbers);
124-
Optional<LotteryTicketId> id = service.submitTicket(lotteryTicket);
125-
if (id.isPresent()) {
126-
LOGGER.info("Submitted lottery ticket with id: {}", id.get());
127-
} else {
128-
LOGGER.info("Failed submitting lottery ticket - please try again.");
129-
}
130-
} catch (Exception e) {
131-
LOGGER.info("Failed submitting lottery ticket - please try again.");
132-
}
133-
}
134-
135-
private static void addFundsToLotteryAccount(WireTransfers bank, Scanner scanner) {
136-
LOGGER.info("What is the account number?");
137-
String account = readString(scanner);
138-
LOGGER.info("How many credits do you want to deposit?");
139-
String amount = readString(scanner);
140-
bank.setFunds(account, Integer.parseInt(amount));
141-
LOGGER.info("The account {} now has {} credits.", account, bank.getFunds(account));
142-
}
143-
144-
private static void queryLotteryAccountFunds(WireTransfers bank, Scanner scanner) {
145-
LOGGER.info("What is the account number?");
146-
String account = readString(scanner);
147-
LOGGER.info("The account {} has {} credits.", account, bank.getFunds(account));
148-
}
149-
15074
private static void printMainMenu() {
15175
LOGGER.info("");
15276
LOGGER.info("### Lottery Service Console ###");
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.iluwatar.hexagonal.service;
2+
3+
import com.iluwatar.hexagonal.banking.WireTransfers;
4+
import com.iluwatar.hexagonal.domain.LotteryService;
5+
6+
import java.util.Scanner;
7+
8+
9+
/**
10+
* Console interface for lottery service
11+
*/
12+
public interface LotteryConsoleService {
13+
14+
void checkTicket(LotteryService service, Scanner scanner);
15+
16+
/**
17+
* Submit lottery ticket to participate in the lottery
18+
*/
19+
void submitTicket(LotteryService service, Scanner scanner);
20+
21+
/**
22+
* Add funds to lottery account
23+
*/
24+
void addFundsToLotteryAccount(WireTransfers bank, Scanner scanner);
25+
26+
27+
/**
28+
* Recovery funds from lottery account
29+
*/
30+
void queryLotteryAccountFunds(WireTransfers bank, Scanner scanner);
31+
32+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.iluwatar.hexagonal.service;
2+
3+
import com.iluwatar.hexagonal.banking.WireTransfers;
4+
import com.iluwatar.hexagonal.domain.*;
5+
import org.slf4j.Logger;
6+
7+
import java.util.HashSet;
8+
import java.util.Optional;
9+
import java.util.Scanner;
10+
import java.util.Set;
11+
12+
/**
13+
* Console implementation for lottery console service
14+
*/
15+
public class LotteryConsoleServiceImpl implements LotteryConsoleService {
16+
17+
private final Logger logger;
18+
19+
/**
20+
* Constructor
21+
*/
22+
public LotteryConsoleServiceImpl(Logger logger) {
23+
this.logger = logger;
24+
}
25+
26+
@Override
27+
public void checkTicket(LotteryService service, Scanner scanner) {
28+
logger.info( "What is the ID of the lottery ticket?" );
29+
String id = readString( scanner );
30+
logger.info( "Give the 4 comma separated winning numbers?" );
31+
String numbers = readString( scanner );
32+
try {
33+
String[] parts = numbers.split( "," );
34+
Set<Integer> winningNumbers = new HashSet<>();
35+
for (int i = 0; i < 4; i++) {
36+
winningNumbers.add( Integer.parseInt( parts[i] ) );
37+
}
38+
39+
final LotteryTicketId lotteryTicketId = new LotteryTicketId( Integer.parseInt( id ) );
40+
final LotteryNumbers lotteryNumbers = LotteryNumbers.create( winningNumbers );
41+
LotteryTicketCheckResult result = service.checkTicketForPrize( lotteryTicketId, lotteryNumbers );
42+
43+
if (result.getResult().equals( LotteryTicketCheckResult.CheckResult.WIN_PRIZE )) {
44+
logger.info( "Congratulations! The lottery ticket has won!" );
45+
} else if (result.getResult().equals( LotteryTicketCheckResult.CheckResult.NO_PRIZE )) {
46+
logger.info( "Unfortunately the lottery ticket did not win." );
47+
} else {
48+
logger.info( "Such lottery ticket has not been submitted." );
49+
}
50+
} catch (Exception e) {
51+
logger.info( "Failed checking the lottery ticket - please try again." );
52+
}
53+
}
54+
55+
@Override
56+
public void submitTicket(LotteryService service, Scanner scanner) {
57+
logger.info( "What is your email address?" );
58+
String email = readString( scanner );
59+
logger.info( "What is your bank account number?" );
60+
String account = readString( scanner );
61+
logger.info( "What is your phone number?" );
62+
String phone = readString( scanner );
63+
PlayerDetails details = new PlayerDetails( email, account, phone );
64+
logger.info( "Give 4 comma separated lottery numbers?" );
65+
String numbers = readString( scanner );
66+
try {
67+
String[] parts = numbers.split( "," );
68+
Set<Integer> chosen = new HashSet<>();
69+
for (int i = 0; i < 4; i++) {
70+
chosen.add( Integer.parseInt( parts[i] ) );
71+
}
72+
LotteryNumbers lotteryNumbers = LotteryNumbers.create( chosen );
73+
LotteryTicket lotteryTicket = new LotteryTicket( new LotteryTicketId(), details, lotteryNumbers );
74+
Optional<LotteryTicketId> id = service.submitTicket( lotteryTicket );
75+
if (id.isPresent()) {
76+
logger.info( "Submitted lottery ticket with id: {}", id.get() );
77+
} else {
78+
logger.info( "Failed submitting lottery ticket - please try again." );
79+
}
80+
} catch (Exception e) {
81+
logger.info( "Failed submitting lottery ticket - please try again." );
82+
}
83+
}
84+
85+
@Override
86+
public void addFundsToLotteryAccount(WireTransfers bank, Scanner scanner) {
87+
logger.info( "What is the account number?" );
88+
String account = readString( scanner );
89+
logger.info( "How many credits do you want to deposit?" );
90+
String amount = readString( scanner );
91+
bank.setFunds( account, Integer.parseInt( amount ) );
92+
logger.info( "The account {} now has {} credits.", account, bank.getFunds( account ) );
93+
}
94+
95+
@Override
96+
public void queryLotteryAccountFunds(WireTransfers bank, Scanner scanner) {
97+
logger.info( "What is the account number?" );
98+
String account = readString( scanner );
99+
logger.info( "The account {} has {} credits.", account, bank.getFunds( account ) );
100+
}
101+
102+
private String readString(Scanner scanner) {
103+
System.out.print( "> " );
104+
return scanner.next();
105+
}
106+
}

0 commit comments

Comments
 (0)