Fix checkstyle & update interface services
This commit is contained in:
		| @@ -25,7 +25,6 @@ package com.iluwatar.hexagonal.administration; | |||||||
| import com.google.inject.Guice; | import com.google.inject.Guice; | ||||||
| import com.google.inject.Injector; | import com.google.inject.Injector; | ||||||
| import com.iluwatar.hexagonal.domain.LotteryAdministration; | import com.iluwatar.hexagonal.domain.LotteryAdministration; | ||||||
| import com.iluwatar.hexagonal.domain.LotteryNumbers; |  | ||||||
| import com.iluwatar.hexagonal.domain.LotteryService; | import com.iluwatar.hexagonal.domain.LotteryService; | ||||||
| import com.iluwatar.hexagonal.module.LotteryModule; | import com.iluwatar.hexagonal.module.LotteryModule; | ||||||
| import com.iluwatar.hexagonal.mongo.MongoConnectionPropertiesLoader; | import com.iluwatar.hexagonal.mongo.MongoConnectionPropertiesLoader; | ||||||
| @@ -48,23 +47,21 @@ public class ConsoleAdministration { | |||||||
|   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()); | ||||||
|     LotteryAdministration administartion = injector.getInstance(LotteryAdministration.class); |     LotteryAdministration administration = injector.getInstance(LotteryAdministration.class); | ||||||
|     LotteryService service = injector.getInstance(LotteryService.class); |     LotteryService service = injector.getInstance(LotteryService.class); | ||||||
|     SampleData.submitTickets(service, 20); |     SampleData.submitTickets(service, 20); | ||||||
|  |     ConsoleAdministrationSrv consoleAdministration = new ConsoleAdministrationSrvImpl(administration, LOGGER); | ||||||
|     try (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 ("1".equals(cmd)) { |         if ("1".equals(cmd)) { | ||||||
|           administartion.getAllSubmittedTickets().forEach((k, v) -> LOGGER.info("Key: {}, Value: {}", k, v)); |           consoleAdministration.getAllSubmittedTickets(); | ||||||
|         } else if ("2".equals(cmd)) { |         } else if ("2".equals(cmd)) { | ||||||
|           LotteryNumbers numbers = administartion.performLottery(); |           consoleAdministration.performLottery(); | ||||||
|           LOGGER.info("The winning numbers: {}", numbers.getNumbersAsString()); |  | ||||||
|           LOGGER.info("Time to reset the database for next round, eh?"); |  | ||||||
|         } else if ("3".equals(cmd)) { |         } else if ("3".equals(cmd)) { | ||||||
|           administartion.resetLottery(); |           consoleAdministration.resetLottery(); | ||||||
|           LOGGER.info("The lottery ticket database was cleared."); |  | ||||||
|         } else if ("4".equals(cmd)) { |         } else if ("4".equals(cmd)) { | ||||||
|           exit = true; |           exit = true; | ||||||
|         } else { |         } else { | ||||||
|   | |||||||
| @@ -0,0 +1,22 @@ | |||||||
|  | package com.iluwatar.hexagonal.administration; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Console interface for lottery administration | ||||||
|  |  */ | ||||||
|  | public interface ConsoleAdministrationSrv { | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * Get all submitted tickets | ||||||
|  |    */ | ||||||
|  |   void getAllSubmittedTickets(); | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * Draw lottery numbers | ||||||
|  |    */ | ||||||
|  |   void performLottery(); | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * Begin new lottery round | ||||||
|  |    */ | ||||||
|  |   void resetLottery(); | ||||||
|  | } | ||||||
| @@ -0,0 +1,39 @@ | |||||||
|  | package com.iluwatar.hexagonal.administration; | ||||||
|  |  | ||||||
|  | import com.iluwatar.hexagonal.domain.LotteryAdministration; | ||||||
|  | import com.iluwatar.hexagonal.domain.LotteryNumbers; | ||||||
|  | import org.slf4j.Logger; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Console implementation for lottery administration | ||||||
|  |  */ | ||||||
|  | public class ConsoleAdministrationSrvImpl implements ConsoleAdministrationSrv { | ||||||
|  |   private final LotteryAdministration administration; | ||||||
|  |   private final Logger logger; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * Constructor | ||||||
|  |    */ | ||||||
|  |   public ConsoleAdministrationSrvImpl(LotteryAdministration administration, Logger logger) { | ||||||
|  |     this.administration = administration; | ||||||
|  |     this.logger = logger; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public void getAllSubmittedTickets() { | ||||||
|  |     administration.getAllSubmittedTickets().forEach((k, v) -> logger.info("Key: {}, Value: {}", k, v)); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public void performLottery() { | ||||||
|  |     LotteryNumbers numbers = administration.performLottery(); | ||||||
|  |     logger.info("The winning numbers: {}", numbers.getNumbersAsString()); | ||||||
|  |     logger.info("Time to reset the database for next round, eh?"); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public void resetLottery() { | ||||||
|  |     administration.resetLottery(); | ||||||
|  |     logger.info("The lottery ticket database was cleared."); | ||||||
|  |   } | ||||||
|  | } | ||||||
| @@ -25,6 +25,7 @@ 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.LotteryService; | ||||||
| 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; | ||||||
| @@ -45,22 +46,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()); | ||||||
|     com.iluwatar.hexagonal.domain.LotteryService service = injector.getInstance( com.iluwatar.hexagonal.domain.LotteryService.class); |     LotteryService service = injector.getInstance( 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); |         LotteryConsoleService lotteryConsoleService = new LotteryConsoleServiceImpl(LOGGER); | ||||||
|         if ("1".equals(cmd)) { |         if ("1".equals(cmd)) { | ||||||
|           lotteryService.queryLotteryAccountFunds(bank, scanner); |           lotteryConsoleService.queryLotteryAccountFunds(bank, scanner); | ||||||
|         } else if ("2".equals(cmd)) { |         } else if ("2".equals(cmd)) { | ||||||
|           lotteryService.addFundsToLotteryAccount(bank, scanner); |           lotteryConsoleService.addFundsToLotteryAccount(bank, scanner); | ||||||
|         } else if ("3".equals(cmd)) { |         } else if ("3".equals(cmd)) { | ||||||
|           lotteryService.submitTicket(service, scanner); |           lotteryConsoleService.submitTicket(service, scanner); | ||||||
|         } else if ("4".equals(cmd)) { |         } else if ("4".equals(cmd)) { | ||||||
|           lotteryService.checkTicket(service, scanner); |           lotteryConsoleService.checkTicket(service, scanner); | ||||||
|         } else if ("5".equals(cmd)) { |         } else if ("5".equals(cmd)) { | ||||||
|           exit = true; |           exit = true; | ||||||
|         } else { |         } else { | ||||||
|   | |||||||
| @@ -0,0 +1,32 @@ | |||||||
|  | package com.iluwatar.hexagonal.service; | ||||||
|  |  | ||||||
|  | import com.iluwatar.hexagonal.banking.WireTransfers; | ||||||
|  | import com.iluwatar.hexagonal.domain.LotteryService; | ||||||
|  |  | ||||||
|  | import java.util.Scanner; | ||||||
|  |  | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Console interface for lottery service | ||||||
|  |  */ | ||||||
|  | public interface LotteryConsoleService { | ||||||
|  |  | ||||||
|  |   void checkTicket(LotteryService service, Scanner scanner); | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |   * Submit lottery ticket to participate in the lottery | ||||||
|  |   */ | ||||||
|  |   void submitTicket(LotteryService service, Scanner scanner); | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |   * Add funds to lottery account | ||||||
|  |   */ | ||||||
|  |   void addFundsToLotteryAccount(WireTransfers bank, Scanner scanner); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |   * Recovery funds from lottery account | ||||||
|  |   */ | ||||||
|  |   void queryLotteryAccountFunds(WireTransfers bank, Scanner scanner); | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -0,0 +1,107 @@ | |||||||
|  | package com.iluwatar.hexagonal.service; | ||||||
|  |  | ||||||
|  | import com.google.inject.Inject; | ||||||
|  | import com.iluwatar.hexagonal.banking.WireTransfers; | ||||||
|  | import com.iluwatar.hexagonal.domain.*; | ||||||
|  | import org.slf4j.Logger; | ||||||
|  |  | ||||||
|  | import java.util.HashSet; | ||||||
|  | import java.util.Optional; | ||||||
|  | import java.util.Scanner; | ||||||
|  | import java.util.Set; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Console implementation for lottery console service | ||||||
|  |  */ | ||||||
|  | public class LotteryConsoleServiceImpl implements LotteryConsoleService { | ||||||
|  |  | ||||||
|  |   private final Logger logger; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * Constructor | ||||||
|  |    */ | ||||||
|  |   public LotteryConsoleServiceImpl(Logger logger) { | ||||||
|  |     this.logger = logger; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public 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] ) ); | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       final LotteryTicketId lotteryTicketId = new LotteryTicketId( Integer.parseInt( id ) ); | ||||||
|  |       final LotteryNumbers lotteryNumbers = LotteryNumbers.create( winningNumbers ); | ||||||
|  |       LotteryTicketCheckResult result = service.checkTicketForPrize( lotteryTicketId, lotteryNumbers ); | ||||||
|  |  | ||||||
|  |       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(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(); | ||||||
|  |   } | ||||||
|  | } | ||||||
| @@ -1,17 +0,0 @@ | |||||||
| 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); |  | ||||||
|  |  | ||||||
| } |  | ||||||
| @@ -1,99 +0,0 @@ | |||||||
| 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(); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
		Reference in New Issue
	
	Block a user