Skip to content

Commit d2b900b

Browse files
committed
Fix checkstyle & update interface services
1 parent ae07423 commit d2b900b

File tree

8 files changed

+212
-130
lines changed

8 files changed

+212
-130
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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
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.LotteryService;
2829
import com.iluwatar.hexagonal.module.LotteryModule;
2930
import com.iluwatar.hexagonal.mongo.MongoConnectionPropertiesLoader;
3031
import org.slf4j.Logger;
@@ -45,22 +46,22 @@ public class ConsoleLottery {
4546
public static void main(String[] args) {
4647
MongoConnectionPropertiesLoader.load();
4748
Injector injector = Guice.createInjector(new LotteryModule());
48-
com.iluwatar.hexagonal.domain.LotteryService service = injector.getInstance( com.iluwatar.hexagonal.domain.LotteryService.class);
49+
LotteryService service = injector.getInstance( LotteryService.class);
4950
WireTransfers bank = injector.getInstance(WireTransfers.class);
5051
try (final Scanner scanner = new Scanner(System.in)) {
5152
boolean exit = false;
5253
while (!exit) {
5354
printMainMenu();
5455
String cmd = readString(scanner);
55-
LotteryService lotteryService = new LotteryServiceImpl(LOGGER);
56+
LotteryConsoleService lotteryConsoleService = new LotteryConsoleServiceImpl(LOGGER);
5657
if ("1".equals(cmd)) {
57-
lotteryService.queryLotteryAccountFunds(bank, scanner);
58+
lotteryConsoleService.queryLotteryAccountFunds(bank, scanner);
5859
} else if ("2".equals(cmd)) {
59-
lotteryService.addFundsToLotteryAccount(bank, scanner);
60+
lotteryConsoleService.addFundsToLotteryAccount(bank, scanner);
6061
} else if ("3".equals(cmd)) {
61-
lotteryService.submitTicket(service, scanner);
62+
lotteryConsoleService.submitTicket(service, scanner);
6263
} else if ("4".equals(cmd)) {
63-
lotteryService.checkTicket(service, scanner);
64+
lotteryConsoleService.checkTicket(service, scanner);
6465
} else if ("5".equals(cmd)) {
6566
exit = true;
6667
} else {
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: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.iluwatar.hexagonal.service;
2+
3+
import com.google.inject.Inject;
4+
import com.iluwatar.hexagonal.banking.WireTransfers;
5+
import com.iluwatar.hexagonal.domain.*;
6+
import org.slf4j.Logger;
7+
8+
import java.util.HashSet;
9+
import java.util.Optional;
10+
import java.util.Scanner;
11+
import java.util.Set;
12+
13+
/**
14+
* Console implementation for lottery console service
15+
*/
16+
public class LotteryConsoleServiceImpl implements LotteryConsoleService {
17+
18+
private final Logger logger;
19+
20+
/**
21+
* Constructor
22+
*/
23+
public LotteryConsoleServiceImpl(Logger logger) {
24+
this.logger = logger;
25+
}
26+
27+
@Override
28+
public void checkTicket(LotteryService service, Scanner scanner) {
29+
logger.info( "What is the ID of the lottery ticket?" );
30+
String id = readString( scanner );
31+
logger.info( "Give the 4 comma separated winning numbers?" );
32+
String numbers = readString( scanner );
33+
try {
34+
String[] parts = numbers.split( "," );
35+
Set<Integer> winningNumbers = new HashSet<>();
36+
for (int i = 0; i < 4; i++) {
37+
winningNumbers.add( Integer.parseInt( parts[i] ) );
38+
}
39+
40+
final LotteryTicketId lotteryTicketId = new LotteryTicketId( Integer.parseInt( id ) );
41+
final LotteryNumbers lotteryNumbers = LotteryNumbers.create( winningNumbers );
42+
LotteryTicketCheckResult result = service.checkTicketForPrize( lotteryTicketId, lotteryNumbers );
43+
44+
if (result.getResult().equals( LotteryTicketCheckResult.CheckResult.WIN_PRIZE )) {
45+
logger.info( "Congratulations! The lottery ticket has won!" );
46+
} else if (result.getResult().equals( LotteryTicketCheckResult.CheckResult.NO_PRIZE )) {
47+
logger.info( "Unfortunately the lottery ticket did not win." );
48+
} else {
49+
logger.info( "Such lottery ticket has not been submitted." );
50+
}
51+
} catch (Exception e) {
52+
logger.info( "Failed checking the lottery ticket - please try again." );
53+
}
54+
}
55+
56+
@Override
57+
public void submitTicket(LotteryService service, Scanner scanner) {
58+
logger.info( "What is your email address?" );
59+
String email = readString( scanner );
60+
logger.info( "What is your bank account number?" );
61+
String account = readString( scanner );
62+
logger.info( "What is your phone number?" );
63+
String phone = readString( scanner );
64+
PlayerDetails details = new PlayerDetails( email, account, phone );
65+
logger.info( "Give 4 comma separated lottery numbers?" );
66+
String numbers = readString( scanner );
67+
try {
68+
String[] parts = numbers.split( "," );
69+
Set<Integer> chosen = new HashSet<>();
70+
for (int i = 0; i < 4; i++) {
71+
chosen.add( Integer.parseInt( parts[i] ) );
72+
}
73+
LotteryNumbers lotteryNumbers = LotteryNumbers.create( chosen );
74+
LotteryTicket lotteryTicket = new LotteryTicket( new LotteryTicketId(), details, lotteryNumbers );
75+
Optional<LotteryTicketId> id = service.submitTicket( lotteryTicket );
76+
if (id.isPresent()) {
77+
logger.info( "Submitted lottery ticket with id: {}", id.get() );
78+
} else {
79+
logger.info( "Failed submitting lottery ticket - please try again." );
80+
}
81+
} catch (Exception e) {
82+
logger.info( "Failed submitting lottery ticket - please try again." );
83+
}
84+
}
85+
86+
@Override
87+
public void addFundsToLotteryAccount(WireTransfers bank, Scanner scanner) {
88+
logger.info( "What is the account number?" );
89+
String account = readString( scanner );
90+
logger.info( "How many credits do you want to deposit?" );
91+
String amount = readString( scanner );
92+
bank.setFunds( account, Integer.parseInt( amount ) );
93+
logger.info( "The account {} now has {} credits.", account, bank.getFunds( account ) );
94+
}
95+
96+
@Override
97+
public void queryLotteryAccountFunds(WireTransfers bank, Scanner scanner) {
98+
logger.info( "What is the account number?" );
99+
String account = readString( scanner );
100+
logger.info( "The account {} has {} credits.", account, bank.getFunds( account ) );
101+
}
102+
103+
private String readString(Scanner scanner) {
104+
System.out.print( "> " );
105+
return scanner.next();
106+
}
107+
}

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

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

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

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

0 commit comments

Comments
 (0)