From e17d72bca81523ae4521cc4b198e72980aad8b38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilkka=20Sepp=C3=A4l=C3=A4?= Date: Sat, 10 Sep 2016 14:28:05 +0300 Subject: [PATCH] Hexagonal pattern: Added console interfaces for players and administration. --- .../main/java/com/iluwatar/hexagonal/App.java | 81 +---------- .../administration/ConsoleAdministration.java | 77 +++++++++++ .../domain/LotteryAdministration.java | 3 + .../hexagonal/domain/LotteryNumbers.java | 7 +- .../hexagonal/domain/LotteryTicket.java | 5 + .../domain/LotteryTicketChecker.java | 22 +++ .../hexagonal/domain/LotteryTicketId.java | 9 ++ .../hexagonal/domain/PlayerDetails.java | 7 + .../hexagonal/{ => module}/LotteryModule.java | 2 +- .../module}/LotteryTestingModule.java | 2 +- .../hexagonal/sampledata/SampleData.java | 107 ++++++++++++++ .../hexagonal/service/ConsoleLottery.java | 130 ++++++++++++++++++ .../hexagonal/domain/LotteryTest.java | 2 +- 13 files changed, 374 insertions(+), 80 deletions(-) rename hexagonal/src/main/java/com/iluwatar/hexagonal/{ => module}/LotteryModule.java (97%) rename hexagonal/src/{test/java/com/iluwatar/hexagonal => main/java/com/iluwatar/hexagonal/module}/LotteryTestingModule.java (97%) create mode 100644 hexagonal/src/main/java/com/iluwatar/hexagonal/sampledata/SampleData.java diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/App.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/App.java index 83ae15032..a7d31446b 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/App.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/App.java @@ -22,19 +22,12 @@ */ package com.iluwatar.hexagonal; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - import com.google.inject.Guice; import com.google.inject.Injector; import com.iluwatar.hexagonal.domain.LotteryAdministration; -import com.iluwatar.hexagonal.banking.InMemoryBank; -import com.iluwatar.hexagonal.domain.LotteryConstants; -import com.iluwatar.hexagonal.domain.LotteryNumbers; -import com.iluwatar.hexagonal.domain.LotteryTicket; -import com.iluwatar.hexagonal.domain.PlayerDetails; import com.iluwatar.hexagonal.domain.LotteryService; +import com.iluwatar.hexagonal.module.LotteryTestingModule; +import com.iluwatar.hexagonal.sampledata.SampleData; /** * @@ -68,64 +61,13 @@ import com.iluwatar.hexagonal.domain.LotteryService; * */ public class App { - - private static final List PLAYERS; - - static { - PLAYERS = new ArrayList<>(); - PLAYERS.add(PlayerDetails.create("john@google.com", "312-342", "+3242434242")); - PLAYERS.add(PlayerDetails.create("mary@google.com", "234-987", "+23452346")); - PLAYERS.add(PlayerDetails.create("steve@google.com", "833-836", "+63457543")); - PLAYERS.add(PlayerDetails.create("wayne@google.com", "319-826", "+24626")); - PLAYERS.add(PlayerDetails.create("johnie@google.com", "983-322", "+3635635")); - PLAYERS.add(PlayerDetails.create("andy@google.com", "934-734", "+0898245")); - PLAYERS.add(PlayerDetails.create("richard@google.com", "536-738", "+09845325")); - PLAYERS.add(PlayerDetails.create("kevin@google.com", "453-936", "+2423532")); - PLAYERS.add(PlayerDetails.create("arnold@google.com", "114-988", "+5646346524")); - PLAYERS.add(PlayerDetails.create("ian@google.com", "663-765", "+928394235")); - PLAYERS.add(PlayerDetails.create("robin@google.com", "334-763", "+35448")); - PLAYERS.add(PlayerDetails.create("ted@google.com", "735-964", "+98752345")); - PLAYERS.add(PlayerDetails.create("larry@google.com", "734-853", "+043842423")); - PLAYERS.add(PlayerDetails.create("calvin@google.com", "334-746", "+73294135")); - PLAYERS.add(PlayerDetails.create("jacob@google.com", "444-766", "+358042354")); - PLAYERS.add(PlayerDetails.create("edwin@google.com", "895-345", "+9752435")); - PLAYERS.add(PlayerDetails.create("mary@google.com", "760-009", "+34203542")); - PLAYERS.add(PlayerDetails.create("lolita@google.com", "425-907", "+9872342")); - PLAYERS.add(PlayerDetails.create("bruno@google.com", "023-638", "+673824122")); - PLAYERS.add(PlayerDetails.create("peter@google.com", "335-886", "+5432503945")); - PLAYERS.add(PlayerDetails.create("warren@google.com", "225-946", "+9872341324")); - PLAYERS.add(PlayerDetails.create("monica@google.com", "265-748", "+134124")); - PLAYERS.add(PlayerDetails.create("ollie@google.com", "190-045", "+34453452")); - PLAYERS.add(PlayerDetails.create("yngwie@google.com", "241-465", "+9897641231")); - PLAYERS.add(PlayerDetails.create("lars@google.com", "746-936", "+42345298345")); - PLAYERS.add(PlayerDetails.create("bobbie@google.com", "946-384", "+79831742")); - PLAYERS.add(PlayerDetails.create("tyron@google.com", "310-992", "+0498837412")); - PLAYERS.add(PlayerDetails.create("tyrell@google.com", "032-045", "+67834134")); - PLAYERS.add(PlayerDetails.create("nadja@google.com", "000-346", "+498723")); - PLAYERS.add(PlayerDetails.create("wendy@google.com", "994-989", "+987324454")); - PLAYERS.add(PlayerDetails.create("luke@google.com", "546-634", "+987642435")); - PLAYERS.add(PlayerDetails.create("bjorn@google.com", "342-874", "+7834325")); - PLAYERS.add(PlayerDetails.create("lisa@google.com", "024-653", "+980742154")); - PLAYERS.add(PlayerDetails.create("anton@google.com", "834-935", "+876423145")); - PLAYERS.add(PlayerDetails.create("bruce@google.com", "284-936", "+09843212345")); - PLAYERS.add(PlayerDetails.create("ray@google.com", "843-073", "+678324123")); - PLAYERS.add(PlayerDetails.create("ron@google.com", "637-738", "+09842354")); - PLAYERS.add(PlayerDetails.create("xavier@google.com", "143-947", "+375245")); - PLAYERS.add(PlayerDetails.create("harriet@google.com", "842-404", "+131243252")); - InMemoryBank wireTransfers = new InMemoryBank(); - Random random = new Random(); - for (int i = 0; i < PLAYERS.size(); i++) { - wireTransfers.setFunds(PLAYERS.get(i).getBankAccount(), - random.nextInt(LotteryConstants.PLAYER_MAX_SALDO)); - } - } - + /** * Program entry point */ public static void main(String[] args) { - Injector injector = Guice.createInjector(new LotteryModule()); + Injector injector = Guice.createInjector(new LotteryTestingModule()); // start new lottery round LotteryAdministration administartion = injector.getInstance(LotteryAdministration.class); @@ -133,22 +75,9 @@ public class App { // submit some lottery tickets LotteryService service = injector.getInstance(LotteryService.class); - submitTickets(service, 20); + SampleData.submitTickets(service, 20); // perform lottery administartion.performLottery(); } - - private static void submitTickets(LotteryService lotteryService, int numTickets) { - for (int i = 0; i < numTickets; i++) { - LotteryTicket ticket = LotteryTicket.create(getRandomPlayerDetails(), LotteryNumbers.createRandom()); - lotteryService.submitTicket(ticket); - } - } - - private static PlayerDetails getRandomPlayerDetails() { - Random random = new Random(); - int idx = random.nextInt(PLAYERS.size()); - return PLAYERS.get(idx); - } } diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java index 5238edd24..4301c07e3 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java @@ -1,7 +1,84 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.hexagonal.administration; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.iluwatar.hexagonal.domain.LotteryAdministration; +import com.iluwatar.hexagonal.domain.LotteryNumbers; +import com.iluwatar.hexagonal.domain.LotteryService; +import com.iluwatar.hexagonal.module.LotteryModule; +import com.iluwatar.hexagonal.sampledata.SampleData; + +import java.util.Scanner; + /** * Console interface for lottery administration */ public class ConsoleAdministration { + + /** + * Program entry point + */ + public static void main(String[] args) { + Injector injector = Guice.createInjector(new LotteryModule()); + 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)->System.out.println("Key: " + k + " Value: " + v)); + } else if (cmd.equals("2")) { + LotteryNumbers numbers = administartion.performLottery(); + System.out.println("The winning numbers: " + numbers); + System.out.println("Time to reset the database for next round, eh?"); + } else if (cmd.equals("3")) { + administartion.resetLottery(); + System.out.println("The lottery ticket database was cleared."); + } else if (cmd.equals("4")) { + exit = true; + } else { + System.out.println("Unknown command: " + cmd); + } + } + } + + private static void printMainMenu() { + System.out.println(""); + System.out.println("### Lottery Administration Console ###"); + System.out.println("(1) Show all submitted tickets"); + System.out.println("(2) Perform lottery draw"); + System.out.println("(3) Reset lottery ticket database"); + System.out.println("(4) Exit"); + } + + private static String readString(Scanner scanner) { + System.out.print("> "); + String cmd = scanner.next(); + return cmd; + } } diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryAdministration.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryAdministration.java index 7117397d3..f73390863 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryAdministration.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryAdministration.java @@ -41,6 +41,9 @@ public class LotteryAdministration { private final WireTransfers wireTransfers; private final LotteryTicketChecker checker; + /** + * Constructor + */ @Inject public LotteryAdministration(LotteryTicketRepository repository, LotteryNotifications notifications, WireTransfers wireTransfers) { diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryNumbers.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryNumbers.java index 69d654657..9ce8d61bd 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryNumbers.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryNumbers.java @@ -92,7 +92,12 @@ public class LotteryNumbers { } } } - + + @Override + public String toString() { + return "LotteryNumbers{" + "numbers=" + numbers + '}'; + } + /** * * Helper class for generating random numbers. diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicket.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicket.java index e5828cfbf..08064f46c 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicket.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicket.java @@ -61,6 +61,11 @@ public class LotteryTicket { return lotteryNumbers; } + @Override + public String toString() { + return playerDetails.toString() + " " + lotteryNumbers.toString(); + } + @Override public int hashCode() { final int prime = 31; diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicketChecker.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicketChecker.java index 10042528b..ce193386b 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicketChecker.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicketChecker.java @@ -1,3 +1,25 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.hexagonal.domain; import com.iluwatar.hexagonal.database.LotteryTicketRepository; diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicketId.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicketId.java index 710091222..e2aa51792 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicketId.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryTicketId.java @@ -34,8 +34,17 @@ public class LotteryTicketId { public LotteryTicketId() { id = UUID.randomUUID(); } + + public LotteryTicketId(String str) { + id = UUID.fromString(str); + } public UUID getId() { return id; } + + @Override + public String toString() { + return id.toString(); + } } diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/PlayerDetails.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/PlayerDetails.java index 2fcdb6eb3..1061ad553 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/PlayerDetails.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/PlayerDetails.java @@ -70,6 +70,13 @@ public class PlayerDetails { return phoneNumber; } + @Override + public String toString() { + return "PlayerDetails{" + "emailAddress='" + emailAddress + '\'' + + ", bankAccountNumber='" + bankAccountNumber + '\'' + + ", phoneNumber='" + phoneNumber + '\'' + '}'; + } + @Override public int hashCode() { final int prime = 31; diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/LotteryModule.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/module/LotteryModule.java similarity index 97% rename from hexagonal/src/main/java/com/iluwatar/hexagonal/LotteryModule.java rename to hexagonal/src/main/java/com/iluwatar/hexagonal/module/LotteryModule.java index dbaf494c2..c62dbfa54 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/LotteryModule.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/module/LotteryModule.java @@ -20,7 +20,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package com.iluwatar.hexagonal; +package com.iluwatar.hexagonal.module; import com.google.inject.AbstractModule; import com.iluwatar.hexagonal.banking.InMemoryBank; diff --git a/hexagonal/src/test/java/com/iluwatar/hexagonal/LotteryTestingModule.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/module/LotteryTestingModule.java similarity index 97% rename from hexagonal/src/test/java/com/iluwatar/hexagonal/LotteryTestingModule.java rename to hexagonal/src/main/java/com/iluwatar/hexagonal/module/LotteryTestingModule.java index 22bfa8f01..c934ed43c 100644 --- a/hexagonal/src/test/java/com/iluwatar/hexagonal/LotteryTestingModule.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/module/LotteryTestingModule.java @@ -20,7 +20,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package com.iluwatar.hexagonal; +package com.iluwatar.hexagonal.module; import com.google.inject.AbstractModule; import com.iluwatar.hexagonal.banking.InMemoryBank; diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/sampledata/SampleData.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/sampledata/SampleData.java new file mode 100644 index 000000000..1af18118d --- /dev/null +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/sampledata/SampleData.java @@ -0,0 +1,107 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.hexagonal.sampledata; + +import com.iluwatar.hexagonal.banking.InMemoryBank; +import com.iluwatar.hexagonal.domain.LotteryConstants; +import com.iluwatar.hexagonal.domain.LotteryNumbers; +import com.iluwatar.hexagonal.domain.LotteryService; +import com.iluwatar.hexagonal.domain.LotteryTicket; +import com.iluwatar.hexagonal.domain.PlayerDetails; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +/** + * Utilities for creating sample lottery tickets + */ +public class SampleData { + + private static final List PLAYERS; + + static { + PLAYERS = new ArrayList<>(); + PLAYERS.add(PlayerDetails.create("john@google.com", "312-342", "+3242434242")); + PLAYERS.add(PlayerDetails.create("mary@google.com", "234-987", "+23452346")); + PLAYERS.add(PlayerDetails.create("steve@google.com", "833-836", "+63457543")); + PLAYERS.add(PlayerDetails.create("wayne@google.com", "319-826", "+24626")); + PLAYERS.add(PlayerDetails.create("johnie@google.com", "983-322", "+3635635")); + PLAYERS.add(PlayerDetails.create("andy@google.com", "934-734", "+0898245")); + PLAYERS.add(PlayerDetails.create("richard@google.com", "536-738", "+09845325")); + PLAYERS.add(PlayerDetails.create("kevin@google.com", "453-936", "+2423532")); + PLAYERS.add(PlayerDetails.create("arnold@google.com", "114-988", "+5646346524")); + PLAYERS.add(PlayerDetails.create("ian@google.com", "663-765", "+928394235")); + PLAYERS.add(PlayerDetails.create("robin@google.com", "334-763", "+35448")); + PLAYERS.add(PlayerDetails.create("ted@google.com", "735-964", "+98752345")); + PLAYERS.add(PlayerDetails.create("larry@google.com", "734-853", "+043842423")); + PLAYERS.add(PlayerDetails.create("calvin@google.com", "334-746", "+73294135")); + PLAYERS.add(PlayerDetails.create("jacob@google.com", "444-766", "+358042354")); + PLAYERS.add(PlayerDetails.create("edwin@google.com", "895-345", "+9752435")); + PLAYERS.add(PlayerDetails.create("mary@google.com", "760-009", "+34203542")); + PLAYERS.add(PlayerDetails.create("lolita@google.com", "425-907", "+9872342")); + PLAYERS.add(PlayerDetails.create("bruno@google.com", "023-638", "+673824122")); + PLAYERS.add(PlayerDetails.create("peter@google.com", "335-886", "+5432503945")); + PLAYERS.add(PlayerDetails.create("warren@google.com", "225-946", "+9872341324")); + PLAYERS.add(PlayerDetails.create("monica@google.com", "265-748", "+134124")); + PLAYERS.add(PlayerDetails.create("ollie@google.com", "190-045", "+34453452")); + PLAYERS.add(PlayerDetails.create("yngwie@google.com", "241-465", "+9897641231")); + PLAYERS.add(PlayerDetails.create("lars@google.com", "746-936", "+42345298345")); + PLAYERS.add(PlayerDetails.create("bobbie@google.com", "946-384", "+79831742")); + PLAYERS.add(PlayerDetails.create("tyron@google.com", "310-992", "+0498837412")); + PLAYERS.add(PlayerDetails.create("tyrell@google.com", "032-045", "+67834134")); + PLAYERS.add(PlayerDetails.create("nadja@google.com", "000-346", "+498723")); + PLAYERS.add(PlayerDetails.create("wendy@google.com", "994-989", "+987324454")); + PLAYERS.add(PlayerDetails.create("luke@google.com", "546-634", "+987642435")); + PLAYERS.add(PlayerDetails.create("bjorn@google.com", "342-874", "+7834325")); + PLAYERS.add(PlayerDetails.create("lisa@google.com", "024-653", "+980742154")); + PLAYERS.add(PlayerDetails.create("anton@google.com", "834-935", "+876423145")); + PLAYERS.add(PlayerDetails.create("bruce@google.com", "284-936", "+09843212345")); + PLAYERS.add(PlayerDetails.create("ray@google.com", "843-073", "+678324123")); + PLAYERS.add(PlayerDetails.create("ron@google.com", "637-738", "+09842354")); + PLAYERS.add(PlayerDetails.create("xavier@google.com", "143-947", "+375245")); + PLAYERS.add(PlayerDetails.create("harriet@google.com", "842-404", "+131243252")); + InMemoryBank wireTransfers = new InMemoryBank(); + Random random = new Random(); + for (int i = 0; i < PLAYERS.size(); i++) { + wireTransfers.setFunds(PLAYERS.get(i).getBankAccount(), + random.nextInt(LotteryConstants.PLAYER_MAX_SALDO)); + } + } + + /** + * Inserts lottery tickets into the database based on the sample data + */ + public static void submitTickets(LotteryService lotteryService, int numTickets) { + for (int i = 0; i < numTickets; i++) { + LotteryTicket ticket = LotteryTicket.create(getRandomPlayerDetails(), LotteryNumbers.createRandom()); + lotteryService.submitTicket(ticket); + } + } + + private static PlayerDetails getRandomPlayerDetails() { + Random random = new Random(); + int idx = random.nextInt(PLAYERS.size()); + return PLAYERS.get(idx); + } +} diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleLottery.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleLottery.java index e48410982..aade24a91 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleLottery.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleLottery.java @@ -1,7 +1,137 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.hexagonal.service; +import com.google.inject.Guice; +import com.google.inject.Injector; +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.sampledata.SampleData; + +import java.util.HashSet; +import java.util.Optional; +import java.util.Scanner; +import java.util.Set; + /** * Console interface for lottery players */ public class ConsoleLottery { + + + /** + * Program entry point + */ + public static void main(String[] args) { + Injector injector = Guice.createInjector(new LotteryModule()); + LotteryService service = injector.getInstance(LotteryService.class); + WireTransfers bank = injector.getInstance(WireTransfers.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")) { + System.out.println("What is the account number?"); + String account = readString(scanner); + System.out.println(String.format("The account %s has %d credits.", account, bank.getFunds(account))); + } else if (cmd.equals("2")) { + System.out.println("What is the account number?"); + String account = readString(scanner); + System.out.println("How many credits do you want to deposit?"); + String amount = readString(scanner); + bank.setFunds(account, Integer.parseInt(amount)); + System.out.println(String.format("The account %s now has %d credits.", account, bank.getFunds(account))); + } else if (cmd.equals("3")) { + System.out.println("What is your email address?"); + String email = readString(scanner); + System.out.println("What is your bank account number?"); + String account = readString(scanner); + System.out.println("What is your phone number?"); + String phone = readString(scanner); + PlayerDetails details = PlayerDetails.create(email, account, phone); + System.out.println("Give 4 comma separated lottery numbers?"); + String numbers = readString(scanner); + String[] parts = numbers.split(","); + Set chosen = new HashSet<>(); + for (int i = 0; i < 4; i++) { + chosen.add(Integer.parseInt(parts[i])); + } + LotteryNumbers lotteryNumbers = LotteryNumbers.create(chosen); + LotteryTicket lotteryTicket = LotteryTicket.create(details, lotteryNumbers); + Optional id = service.submitTicket(lotteryTicket); + if (id.isPresent()) { + System.out.println("Submitted lottery ticket with id: " + id.get()); + } else { + System.out.println("Failed submitting lottery ticket - please try again."); + } + } else if (cmd.equals("4")) { + System.out.println("What is the ID of the lottery ticket?"); + String id = readString(scanner); + System.out.println("Give the 4 comma separated winning numbers?"); + String numbers = readString(scanner); + String[] parts = numbers.split(","); + Set winningNumbers = new HashSet<>(); + for (int i = 0; i < 4; i++) { + winningNumbers.add(Integer.parseInt(parts[i])); + } + LotteryTicketCheckResult result = service.checkTicketForPrize( + new LotteryTicketId(id), LotteryNumbers.create(winningNumbers)); + if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) { + System.out.println("Congratulations! The lottery ticket has won!"); + } else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) { + System.out.println("Unfortunately the lottery ticket did not win."); + } else { + System.out.println("Such lottery ticket has not been submitted."); + } + } else if (cmd.equals("5")) { + exit = true; + } else { + System.out.println("Unknown command"); + } + } + } + + private static void printMainMenu() { + System.out.println(""); + System.out.println("### Lottery Service Console ###"); + System.out.println("(1) Query lottery account funds"); + System.out.println("(2) Add funds to lottery account"); + System.out.println("(3) Submit ticket"); + System.out.println("(4) Check ticket"); + System.out.println("(5) Exit"); + } + + private static String readString(Scanner scanner) { + System.out.print("> "); + String cmd = scanner.next(); + return cmd; + } } diff --git a/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTest.java b/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTest.java index ed6f8d180..943440f07 100644 --- a/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTest.java +++ b/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTest.java @@ -33,7 +33,7 @@ import java.util.Optional; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; -import com.iluwatar.hexagonal.LotteryTestingModule; +import com.iluwatar.hexagonal.module.LotteryTestingModule; import org.junit.Before; import org.junit.Test;