Merge pull request #530 from Valdar-Soft/master
Sonar quality bugfix and minor refactor
This commit is contained in:
commit
ad93184a79
@ -51,24 +51,25 @@ public class ConsoleAdministration {
|
||||
LotteryAdministration administartion = injector.getInstance(LotteryAdministration.class);
|
||||
LotteryService service = injector.getInstance(LotteryService.class);
|
||||
SampleData.submitTickets(service, 20);
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
boolean exit = false;
|
||||
while (!exit) {
|
||||
printMainMenu();
|
||||
String cmd = readString(scanner);
|
||||
if (cmd.equals("1")) {
|
||||
administartion.getAllSubmittedTickets().forEach((k,v)->LOGGER.info("Key: {}, Value: {}", k, v));
|
||||
} else if (cmd.equals("2")) {
|
||||
LotteryNumbers numbers = administartion.performLottery();
|
||||
LOGGER.info("The winning numbers: {}", numbers.getNumbersAsString());
|
||||
LOGGER.info("Time to reset the database for next round, eh?");
|
||||
} else if (cmd.equals("3")) {
|
||||
administartion.resetLottery();
|
||||
LOGGER.info("The lottery ticket database was cleared.");
|
||||
} else if (cmd.equals("4")) {
|
||||
exit = true;
|
||||
} else {
|
||||
LOGGER.info("Unknown command: {}", cmd);
|
||||
try (Scanner scanner = new Scanner(System.in)) {
|
||||
boolean exit = false;
|
||||
while (!exit) {
|
||||
printMainMenu();
|
||||
String cmd = readString(scanner);
|
||||
if ("1".equals(cmd)) {
|
||||
administartion.getAllSubmittedTickets().forEach((k, v) -> LOGGER.info("Key: {}, Value: {}", k, v));
|
||||
} else if ("2".equals(cmd)) {
|
||||
LotteryNumbers numbers = administartion.performLottery();
|
||||
LOGGER.info("The winning numbers: {}", numbers.getNumbersAsString());
|
||||
LOGGER.info("Time to reset the database for next round, eh?");
|
||||
} else if ("3".equals(cmd)) {
|
||||
administartion.resetLottery();
|
||||
LOGGER.info("The lottery ticket database was cleared.");
|
||||
} else if ("4".equals(cmd)) {
|
||||
exit = true;
|
||||
} else {
|
||||
LOGGER.info("Unknown command: {}", cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -84,7 +85,6 @@ public class ConsoleAdministration {
|
||||
|
||||
private static String readString(Scanner scanner) {
|
||||
System.out.print("> ");
|
||||
String cmd = scanner.next();
|
||||
return cmd;
|
||||
return scanner.next();
|
||||
}
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ public class LotteryService {
|
||||
public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
|
||||
boolean result = wireTransfers.transferFunds(LotteryConstants.TICKET_PRIZE,
|
||||
ticket.getPlayerDetails().getBankAccount(), LotteryConstants.SERVICE_BANK_ACCOUNT);
|
||||
if (result == false) {
|
||||
if (!result) {
|
||||
notifications.ticketSubmitError(ticket.getPlayerDetails());
|
||||
return Optional.empty();
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ package com.iluwatar.hexagonal.domain;
|
||||
*/
|
||||
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 int prizeAmount;
|
||||
@ -85,12 +85,6 @@ public class LotteryTicketCheckResult {
|
||||
return false;
|
||||
}
|
||||
LotteryTicketCheckResult other = (LotteryTicketCheckResult) obj;
|
||||
if (checkResult != other.checkResult) {
|
||||
return false;
|
||||
}
|
||||
if (prizeAmount != other.prizeAmount) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return checkResult == other.checkResult && prizeAmount == other.prizeAmount;
|
||||
}
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ public class MongoEventLog implements LotteryEventLog {
|
||||
Document document = new Document("email", details.getEmail());
|
||||
document.put("phone", details.getPhoneNumber());
|
||||
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);
|
||||
stdOutEventLog.ticketSubmitted(details);
|
||||
}
|
||||
@ -115,7 +115,7 @@ public class MongoEventLog implements LotteryEventLog {
|
||||
Document document = new Document("email", details.getEmail());
|
||||
document.put("phone", details.getPhoneNumber());
|
||||
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);
|
||||
stdOutEventLog.ticketSubmitError(details);
|
||||
}
|
||||
@ -125,7 +125,7 @@ public class MongoEventLog implements LotteryEventLog {
|
||||
Document document = new Document("email", details.getEmail());
|
||||
document.put("phone", details.getPhoneNumber());
|
||||
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);
|
||||
stdOutEventLog.ticketDidNotWin(details);
|
||||
}
|
||||
|
@ -56,80 +56,97 @@ public class ConsoleLottery {
|
||||
Injector injector = Guice.createInjector(new LotteryModule());
|
||||
LotteryService service = injector.getInstance(LotteryService.class);
|
||||
WireTransfers bank = injector.getInstance(WireTransfers.class);
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
boolean exit = false;
|
||||
while (!exit) {
|
||||
printMainMenu();
|
||||
String cmd = readString(scanner);
|
||||
if (cmd.equals("1")) {
|
||||
LOGGER.info("What is the account number?");
|
||||
String account = readString(scanner);
|
||||
LOGGER.info("The account {} has {} credits.", account, bank.getFunds(account));
|
||||
} else if (cmd.equals("2")) {
|
||||
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));
|
||||
} else if (cmd.equals("3")) {
|
||||
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.");
|
||||
try (final Scanner scanner = new Scanner(System.in)) {
|
||||
boolean exit = false;
|
||||
while (!exit) {
|
||||
printMainMenu();
|
||||
String cmd = readString(scanner);
|
||||
if ("1".equals(cmd)) {
|
||||
queryLotteryAccountFunds(bank, scanner);
|
||||
} else if ("2".equals(cmd)) {
|
||||
addFundsToLotteryAccount(bank, scanner);
|
||||
} else if ("3".equals(cmd)) {
|
||||
submitTicket(service, scanner);
|
||||
} else if ("4".equals(cmd)) {
|
||||
checkTicket(service, scanner);
|
||||
} else if ("5".equals(cmd)) {
|
||||
exit = true;
|
||||
} else {
|
||||
LOGGER.info("Unknown command");
|
||||
}
|
||||
} 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 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() {
|
||||
LOGGER.info("");
|
||||
LOGGER.info("### Lottery Service Console ###");
|
||||
@ -142,7 +159,6 @@ public class ConsoleLottery {
|
||||
|
||||
private static String readString(Scanner scanner) {
|
||||
System.out.print("> ");
|
||||
String cmd = scanner.next();
|
||||
return cmd;
|
||||
return scanner.next();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user