First proposal
This commit is contained in:
parent
c7f9266768
commit
ae07423470
@ -25,21 +25,12 @@ package com.iluwatar.hexagonal.service;
|
|||||||
import com.google.inject.Guice;
|
import com.google.inject.Guice;
|
||||||
import com.google.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
import com.iluwatar.hexagonal.banking.WireTransfers;
|
import com.iluwatar.hexagonal.banking.WireTransfers;
|
||||||
import com.iluwatar.hexagonal.domain.LotteryNumbers;
|
|
||||||
import com.iluwatar.hexagonal.domain.LotteryService;
|
|
||||||
import com.iluwatar.hexagonal.domain.LotteryTicket;
|
|
||||||
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult;
|
|
||||||
import com.iluwatar.hexagonal.domain.LotteryTicketId;
|
|
||||||
import com.iluwatar.hexagonal.domain.PlayerDetails;
|
|
||||||
import com.iluwatar.hexagonal.module.LotteryModule;
|
import com.iluwatar.hexagonal.module.LotteryModule;
|
||||||
import com.iluwatar.hexagonal.mongo.MongoConnectionPropertiesLoader;
|
import com.iluwatar.hexagonal.mongo.MongoConnectionPropertiesLoader;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Console interface for lottery players
|
* Console interface for lottery players
|
||||||
@ -54,21 +45,22 @@ public class ConsoleLottery {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
MongoConnectionPropertiesLoader.load();
|
MongoConnectionPropertiesLoader.load();
|
||||||
Injector injector = Guice.createInjector(new LotteryModule());
|
Injector injector = Guice.createInjector(new LotteryModule());
|
||||||
LotteryService service = injector.getInstance(LotteryService.class);
|
com.iluwatar.hexagonal.domain.LotteryService service = injector.getInstance( com.iluwatar.hexagonal.domain.LotteryService.class);
|
||||||
WireTransfers bank = injector.getInstance(WireTransfers.class);
|
WireTransfers bank = injector.getInstance(WireTransfers.class);
|
||||||
try (final Scanner scanner = new Scanner(System.in)) {
|
try (final Scanner scanner = new Scanner(System.in)) {
|
||||||
boolean exit = false;
|
boolean exit = false;
|
||||||
while (!exit) {
|
while (!exit) {
|
||||||
printMainMenu();
|
printMainMenu();
|
||||||
String cmd = readString(scanner);
|
String cmd = readString(scanner);
|
||||||
|
LotteryService lotteryService = new LotteryServiceImpl(LOGGER);
|
||||||
if ("1".equals(cmd)) {
|
if ("1".equals(cmd)) {
|
||||||
queryLotteryAccountFunds(bank, scanner);
|
lotteryService.queryLotteryAccountFunds(bank, scanner);
|
||||||
} else if ("2".equals(cmd)) {
|
} else if ("2".equals(cmd)) {
|
||||||
addFundsToLotteryAccount(bank, scanner);
|
lotteryService.addFundsToLotteryAccount(bank, scanner);
|
||||||
} else if ("3".equals(cmd)) {
|
} else if ("3".equals(cmd)) {
|
||||||
submitTicket(service, scanner);
|
lotteryService.submitTicket(service, scanner);
|
||||||
} else if ("4".equals(cmd)) {
|
} else if ("4".equals(cmd)) {
|
||||||
checkTicket(service, scanner);
|
lotteryService.checkTicket(service, scanner);
|
||||||
} else if ("5".equals(cmd)) {
|
} else if ("5".equals(cmd)) {
|
||||||
exit = true;
|
exit = true;
|
||||||
} else {
|
} else {
|
||||||
@ -78,75 +70,6 @@ public class ConsoleLottery {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void checkTicket(LotteryService service, Scanner scanner) {
|
|
||||||
LOGGER.info("What is the ID of the lottery ticket?");
|
|
||||||
String id = readString(scanner);
|
|
||||||
LOGGER.info("Give the 4 comma separated winning numbers?");
|
|
||||||
String numbers = readString(scanner);
|
|
||||||
try {
|
|
||||||
String[] parts = numbers.split(",");
|
|
||||||
Set<Integer> winningNumbers = new HashSet<>();
|
|
||||||
for (int i = 0; i < 4; i++) {
|
|
||||||
winningNumbers.add(Integer.parseInt(parts[i]));
|
|
||||||
}
|
|
||||||
LotteryTicketCheckResult result = service.checkTicketForPrize(
|
|
||||||
new LotteryTicketId(Integer.parseInt(id)), LotteryNumbers.create(winningNumbers));
|
|
||||||
if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) {
|
|
||||||
LOGGER.info("Congratulations! The lottery ticket has won!");
|
|
||||||
} else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) {
|
|
||||||
LOGGER.info("Unfortunately the lottery ticket did not win.");
|
|
||||||
} else {
|
|
||||||
LOGGER.info("Such lottery ticket has not been submitted.");
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOGGER.info("Failed checking the lottery ticket - please try again.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void submitTicket(LotteryService service, Scanner scanner) {
|
|
||||||
LOGGER.info("What is your email address?");
|
|
||||||
String email = readString(scanner);
|
|
||||||
LOGGER.info("What is your bank account number?");
|
|
||||||
String account = readString(scanner);
|
|
||||||
LOGGER.info("What is your phone number?");
|
|
||||||
String phone = readString(scanner);
|
|
||||||
PlayerDetails details = new PlayerDetails(email, account, phone);
|
|
||||||
LOGGER.info("Give 4 comma separated lottery numbers?");
|
|
||||||
String numbers = readString(scanner);
|
|
||||||
try {
|
|
||||||
String[] parts = numbers.split(",");
|
|
||||||
Set<Integer> chosen = new HashSet<>();
|
|
||||||
for (int i = 0; i < 4; i++) {
|
|
||||||
chosen.add(Integer.parseInt(parts[i]));
|
|
||||||
}
|
|
||||||
LotteryNumbers lotteryNumbers = LotteryNumbers.create(chosen);
|
|
||||||
LotteryTicket lotteryTicket = new LotteryTicket(new LotteryTicketId(), details, lotteryNumbers);
|
|
||||||
Optional<LotteryTicketId> id = service.submitTicket(lotteryTicket);
|
|
||||||
if (id.isPresent()) {
|
|
||||||
LOGGER.info("Submitted lottery ticket with id: {}", id.get());
|
|
||||||
} else {
|
|
||||||
LOGGER.info("Failed submitting lottery ticket - please try again.");
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOGGER.info("Failed submitting lottery ticket - please try again.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void addFundsToLotteryAccount(WireTransfers bank, Scanner scanner) {
|
|
||||||
LOGGER.info("What is the account number?");
|
|
||||||
String account = readString(scanner);
|
|
||||||
LOGGER.info("How many credits do you want to deposit?");
|
|
||||||
String amount = readString(scanner);
|
|
||||||
bank.setFunds(account, Integer.parseInt(amount));
|
|
||||||
LOGGER.info("The account {} now has {} credits.", account, bank.getFunds(account));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void queryLotteryAccountFunds(WireTransfers bank, Scanner scanner) {
|
|
||||||
LOGGER.info("What is the account number?");
|
|
||||||
String account = readString(scanner);
|
|
||||||
LOGGER.info("The account {} has {} credits.", account, bank.getFunds(account));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void printMainMenu() {
|
private static void printMainMenu() {
|
||||||
LOGGER.info("");
|
LOGGER.info("");
|
||||||
LOGGER.info("### Lottery Service Console ###");
|
LOGGER.info("### Lottery Service Console ###");
|
||||||
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.iluwatar.hexagonal.service;
|
||||||
|
|
||||||
|
import com.iluwatar.hexagonal.banking.WireTransfers;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public interface LotteryService {
|
||||||
|
void checkTicket(com.iluwatar.hexagonal.domain.LotteryService service, Scanner scanner);
|
||||||
|
|
||||||
|
void submitTicket(com.iluwatar.hexagonal.domain.LotteryService service, Scanner scanner);
|
||||||
|
|
||||||
|
void addFundsToLotteryAccount(WireTransfers bank, Scanner scanner);
|
||||||
|
|
||||||
|
|
||||||
|
void queryLotteryAccountFunds(WireTransfers bank, Scanner scanner);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,99 @@
|
|||||||
|
package com.iluwatar.hexagonal.service;
|
||||||
|
|
||||||
|
import com.iluwatar.hexagonal.banking.WireTransfers;
|
||||||
|
import com.iluwatar.hexagonal.domain.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class LotteryServiceImpl implements LotteryService {
|
||||||
|
|
||||||
|
private final Logger logger;
|
||||||
|
|
||||||
|
public LotteryServiceImpl(Logger logger) {
|
||||||
|
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkTicket(com.iluwatar.hexagonal.domain.LotteryService service, Scanner scanner) {
|
||||||
|
logger.info("What is the ID of the lottery ticket?");
|
||||||
|
String id = readString(scanner);
|
||||||
|
logger.info("Give the 4 comma separated winning numbers?");
|
||||||
|
String numbers = readString(scanner);
|
||||||
|
try {
|
||||||
|
String[] parts = numbers.split(",");
|
||||||
|
Set<Integer> winningNumbers = new HashSet<>();
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
winningNumbers.add(Integer.parseInt(parts[i]));
|
||||||
|
}
|
||||||
|
LotteryTicketCheckResult result = service.checkTicketForPrize(
|
||||||
|
new LotteryTicketId(Integer.parseInt(id)), LotteryNumbers.create(winningNumbers));
|
||||||
|
if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) {
|
||||||
|
logger.info("Congratulations! The lottery ticket has won!");
|
||||||
|
} else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) {
|
||||||
|
logger.info("Unfortunately the lottery ticket did not win.");
|
||||||
|
} else {
|
||||||
|
logger.info("Such lottery ticket has not been submitted.");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.info("Failed checking the lottery ticket - please try again.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void submitTicket(com.iluwatar.hexagonal.domain.LotteryService service, Scanner scanner) {
|
||||||
|
logger.info("What is your email address?");
|
||||||
|
String email = readString(scanner);
|
||||||
|
logger.info("What is your bank account number?");
|
||||||
|
String account = readString(scanner);
|
||||||
|
logger.info("What is your phone number?");
|
||||||
|
String phone = readString(scanner);
|
||||||
|
PlayerDetails details = new PlayerDetails(email, account, phone);
|
||||||
|
logger.info("Give 4 comma separated lottery numbers?");
|
||||||
|
String numbers = readString(scanner);
|
||||||
|
try {
|
||||||
|
String[] parts = numbers.split(",");
|
||||||
|
Set<Integer> chosen = new HashSet<>();
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
chosen.add(Integer.parseInt(parts[i]));
|
||||||
|
}
|
||||||
|
LotteryNumbers lotteryNumbers = LotteryNumbers.create(chosen);
|
||||||
|
LotteryTicket lotteryTicket = new LotteryTicket(new LotteryTicketId(), details, lotteryNumbers);
|
||||||
|
Optional<LotteryTicketId> id = service.submitTicket(lotteryTicket);
|
||||||
|
if (id.isPresent()) {
|
||||||
|
logger.info("Submitted lottery ticket with id: {}", id.get());
|
||||||
|
} else {
|
||||||
|
logger.info("Failed submitting lottery ticket - please try again.");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.info("Failed submitting lottery ticket - please try again.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addFundsToLotteryAccount(WireTransfers bank, Scanner scanner) {
|
||||||
|
logger.info("What is the account number?");
|
||||||
|
String account = readString(scanner);
|
||||||
|
logger.info("How many credits do you want to deposit?");
|
||||||
|
String amount = readString(scanner);
|
||||||
|
bank.setFunds(account, Integer.parseInt(amount));
|
||||||
|
logger.info("The account {} now has {} credits.", account, bank.getFunds(account));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void queryLotteryAccountFunds(WireTransfers bank, Scanner scanner) {
|
||||||
|
logger.info("What is the account number?");
|
||||||
|
String account = readString(scanner);
|
||||||
|
logger.info("The account {} has {} credits.", account, bank.getFunds(account));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String readString(Scanner scanner) {
|
||||||
|
System.out.print("> ");
|
||||||
|
return scanner.next();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user