Merge pull request #530 from Valdar-Soft/master

Sonar quality bugfix and minor refactor
This commit is contained in:
Ilkka Seppälä 2016-12-24 12:36:11 +02:00 committed by GitHub
commit ad93184a79
5 changed files with 113 additions and 103 deletions

View File

@ -51,27 +51,28 @@ public class ConsoleAdministration {
LotteryAdministration administartion = injector.getInstance(LotteryAdministration.class); LotteryAdministration administartion = injector.getInstance(LotteryAdministration.class);
LotteryService service = injector.getInstance(LotteryService.class); LotteryService service = injector.getInstance(LotteryService.class);
SampleData.submitTickets(service, 20); SampleData.submitTickets(service, 20);
Scanner scanner = new Scanner(System.in); try (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);
if (cmd.equals("1")) { if ("1".equals(cmd)) {
administartion.getAllSubmittedTickets().forEach((k,v)->LOGGER.info("Key: {}, Value: {}", k, v)); administartion.getAllSubmittedTickets().forEach((k, v) -> LOGGER.info("Key: {}, Value: {}", k, v));
} else if (cmd.equals("2")) { } else if ("2".equals(cmd)) {
LotteryNumbers numbers = administartion.performLottery(); LotteryNumbers numbers = administartion.performLottery();
LOGGER.info("The winning numbers: {}", numbers.getNumbersAsString()); LOGGER.info("The winning numbers: {}", numbers.getNumbersAsString());
LOGGER.info("Time to reset the database for next round, eh?"); LOGGER.info("Time to reset the database for next round, eh?");
} else if (cmd.equals("3")) { } else if ("3".equals(cmd)) {
administartion.resetLottery(); administartion.resetLottery();
LOGGER.info("The lottery ticket database was cleared."); LOGGER.info("The lottery ticket database was cleared.");
} else if (cmd.equals("4")) { } else if ("4".equals(cmd)) {
exit = true; exit = true;
} else { } else {
LOGGER.info("Unknown command: {}", cmd); LOGGER.info("Unknown command: {}", cmd);
} }
} }
} }
}
private static void printMainMenu() { private static void printMainMenu() {
LOGGER.info(""); LOGGER.info("");
@ -84,7 +85,6 @@ public class ConsoleAdministration {
private static String readString(Scanner scanner) { private static String readString(Scanner scanner) {
System.out.print("> "); System.out.print("> ");
String cmd = scanner.next(); return scanner.next();
return cmd;
} }
} }

View File

@ -57,7 +57,7 @@ public class LotteryService {
public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) { public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
boolean result = wireTransfers.transferFunds(LotteryConstants.TICKET_PRIZE, boolean result = wireTransfers.transferFunds(LotteryConstants.TICKET_PRIZE,
ticket.getPlayerDetails().getBankAccount(), LotteryConstants.SERVICE_BANK_ACCOUNT); ticket.getPlayerDetails().getBankAccount(), LotteryConstants.SERVICE_BANK_ACCOUNT);
if (result == false) { if (!result) {
notifications.ticketSubmitError(ticket.getPlayerDetails()); notifications.ticketSubmitError(ticket.getPlayerDetails());
return Optional.empty(); return Optional.empty();
} }

View File

@ -29,7 +29,7 @@ package com.iluwatar.hexagonal.domain;
*/ */
public class LotteryTicketCheckResult { public class LotteryTicketCheckResult {
public enum CheckResult { WIN_PRIZE, NO_PRIZE, TICKET_NOT_SUBMITTED }; public enum CheckResult { WIN_PRIZE, NO_PRIZE, TICKET_NOT_SUBMITTED }
private final CheckResult checkResult; private final CheckResult checkResult;
private final int prizeAmount; private final int prizeAmount;
@ -85,12 +85,6 @@ public class LotteryTicketCheckResult {
return false; return false;
} }
LotteryTicketCheckResult other = (LotteryTicketCheckResult) obj; LotteryTicketCheckResult other = (LotteryTicketCheckResult) obj;
if (checkResult != other.checkResult) { return checkResult == other.checkResult && prizeAmount == other.prizeAmount;
return false;
}
if (prizeAmount != other.prizeAmount) {
return false;
}
return true;
} }
} }

View File

@ -105,7 +105,7 @@ public class MongoEventLog implements LotteryEventLog {
Document document = new Document("email", details.getEmail()); Document document = new Document("email", details.getEmail());
document.put("phone", details.getPhoneNumber()); document.put("phone", details.getPhoneNumber());
document.put("bank", details.getBankAccount()); document.put("bank", details.getBankAccount());
document.put("message", String.format("Lottery ticket was submitted and bank account was charged for 3 credits.")); document.put("message", "Lottery ticket was submitted and bank account was charged for 3 credits.");
eventsCollection.insertOne(document); eventsCollection.insertOne(document);
stdOutEventLog.ticketSubmitted(details); stdOutEventLog.ticketSubmitted(details);
} }
@ -115,7 +115,7 @@ public class MongoEventLog implements LotteryEventLog {
Document document = new Document("email", details.getEmail()); Document document = new Document("email", details.getEmail());
document.put("phone", details.getPhoneNumber()); document.put("phone", details.getPhoneNumber());
document.put("bank", details.getBankAccount()); document.put("bank", details.getBankAccount());
document.put("message", String.format("Lottery ticket could not be submitted because lack of funds.")); document.put("message", "Lottery ticket could not be submitted because lack of funds.");
eventsCollection.insertOne(document); eventsCollection.insertOne(document);
stdOutEventLog.ticketSubmitError(details); stdOutEventLog.ticketSubmitError(details);
} }
@ -125,7 +125,7 @@ public class MongoEventLog implements LotteryEventLog {
Document document = new Document("email", details.getEmail()); Document document = new Document("email", details.getEmail());
document.put("phone", details.getPhoneNumber()); document.put("phone", details.getPhoneNumber());
document.put("bank", details.getBankAccount()); document.put("bank", details.getBankAccount());
document.put("message", String.format("Lottery ticket was checked and unfortunately did not win this time.")); document.put("message", "Lottery ticket was checked and unfortunately did not win this time.");
eventsCollection.insertOne(document); eventsCollection.insertOne(document);
stdOutEventLog.ticketDidNotWin(details); stdOutEventLog.ticketDidNotWin(details);
} }

View File

@ -56,23 +56,54 @@ public class ConsoleLottery {
Injector injector = Guice.createInjector(new LotteryModule()); Injector injector = Guice.createInjector(new LotteryModule());
LotteryService service = injector.getInstance(LotteryService.class); LotteryService service = injector.getInstance(LotteryService.class);
WireTransfers bank = injector.getInstance(WireTransfers.class); WireTransfers bank = injector.getInstance(WireTransfers.class);
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);
if (cmd.equals("1")) { if ("1".equals(cmd)) {
LOGGER.info("What is the account number?"); queryLotteryAccountFunds(bank, scanner);
String account = readString(scanner); } else if ("2".equals(cmd)) {
LOGGER.info("The account {} has {} credits.", account, bank.getFunds(account)); addFundsToLotteryAccount(bank, scanner);
} else if (cmd.equals("2")) { } else if ("3".equals(cmd)) {
LOGGER.info("What is the account number?"); submitTicket(service, scanner);
String account = readString(scanner); } else if ("4".equals(cmd)) {
LOGGER.info("How many credits do you want to deposit?"); checkTicket(service, scanner);
String amount = readString(scanner); } else if ("5".equals(cmd)) {
bank.setFunds(account, Integer.parseInt(amount)); exit = true;
LOGGER.info("The account {} now has {} credits.", account, bank.getFunds(account)); } else {
} else if (cmd.equals("3")) { LOGGER.info("Unknown command");
}
}
}
}
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?"); LOGGER.info("What is your email address?");
String email = readString(scanner); String email = readString(scanner);
LOGGER.info("What is your bank account number?"); LOGGER.info("What is your bank account number?");
@ -99,35 +130,21 @@ public class ConsoleLottery {
} catch (Exception e) { } catch (Exception e) {
LOGGER.info("Failed submitting lottery ticket - please try again."); LOGGER.info("Failed submitting lottery ticket - please try again.");
} }
} else if (cmd.equals("4")) {
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.");
}
} else if (cmd.equals("5")) {
exit = true;
} else {
LOGGER.info("Unknown command");
} }
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() {
@ -142,7 +159,6 @@ public class ConsoleLottery {
private static String readString(Scanner scanner) { private static String readString(Scanner scanner) {
System.out.print("> "); System.out.print("> ");
String cmd = scanner.next(); return scanner.next();
return cmd;
} }
} }