diff --git a/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/App.java b/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/App.java index 6e9138d3c..820279bc0 100644 --- a/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/App.java +++ b/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/App.java @@ -60,7 +60,8 @@ public class App { /** * Program entry point. * - * @param args command line args + * @param args + * command line args */ public static void main(String[] args) { App app = new App(); @@ -150,56 +151,11 @@ public class App { option = s.nextInt(); if (option == 1) { - s.nextLine(); - LOGGER.info("Boil multiple eggs at once (A) or boil them one-by-one (S)?: "); - String eventType = s.nextLine(); - LOGGER.info("How long should this egg be boiled for (in seconds)?: "); - int eventTime = s.nextInt(); - if (eventType.equalsIgnoreCase("A")) { - try { - int eventId = eventManager.createAsync(eventTime); - eventManager.start(eventId); - LOGGER.info("Egg [{}] is being boiled.", eventId); - } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) { - LOGGER.error(e.getMessage()); - } - } else if (eventType.equalsIgnoreCase("S")) { - try { - int eventId = eventManager.create(eventTime); - eventManager.start(eventId); - LOGGER.info("Egg [{}] is being boiled.", eventId); - } catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException - | EventDoesNotExistException e) { - LOGGER.error(e.getMessage()); - } - } else { - LOGGER.info("Unknown event type."); - } + processOption1(eventManager, s); } else if (option == 2) { - LOGGER.info("Which egg?: "); - int eventId = s.nextInt(); - try { - eventManager.cancel(eventId); - LOGGER.info("Egg [{}] is removed from boiler.", eventId); - } catch (EventDoesNotExistException e) { - LOGGER.error(e.getMessage()); - } + processOption2(eventManager, s); } else if (option == 3) { - s.nextLine(); - LOGGER.info("Just one egg (O) OR all of them (A) ?: "); - String eggChoice = s.nextLine(); - - if (eggChoice.equalsIgnoreCase("O")) { - LOGGER.info("Which egg?: "); - int eventId = s.nextInt(); - try { - eventManager.status(eventId); - } catch (EventDoesNotExistException e) { - LOGGER.error(e.getMessage()); - } - } else if (eggChoice.equalsIgnoreCase("A")) { - eventManager.statusOfAllEvents(); - } + processOption3(eventManager, s); } else if (option == 4) { eventManager.shutdown(); } @@ -208,4 +164,61 @@ public class App { s.close(); } + private void processOption3(EventManager eventManager, Scanner s) { + s.nextLine(); + LOGGER.info("Just one egg (O) OR all of them (A) ?: "); + String eggChoice = s.nextLine(); + + if (eggChoice.equalsIgnoreCase("O")) { + LOGGER.info("Which egg?: "); + int eventId = s.nextInt(); + try { + eventManager.status(eventId); + } catch (EventDoesNotExistException e) { + LOGGER.error(e.getMessage()); + } + } else if (eggChoice.equalsIgnoreCase("A")) { + eventManager.statusOfAllEvents(); + } + } + + private void processOption2(EventManager eventManager, Scanner s) { + LOGGER.info("Which egg?: "); + int eventId = s.nextInt(); + try { + eventManager.cancel(eventId); + LOGGER.info("Egg [{}] is removed from boiler.", eventId); + } catch (EventDoesNotExistException e) { + LOGGER.error(e.getMessage()); + } + } + + private void processOption1(EventManager eventManager, Scanner s) { + s.nextLine(); + LOGGER.info("Boil multiple eggs at once (A) or boil them one-by-one (S)?: "); + String eventType = s.nextLine(); + LOGGER.info("How long should this egg be boiled for (in seconds)?: "); + int eventTime = s.nextInt(); + if (eventType.equalsIgnoreCase("A")) { + try { + int eventId = eventManager.createAsync(eventTime); + eventManager.start(eventId); + LOGGER.info("Egg [{}] is being boiled.", eventId); + } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) { + LOGGER.error(e.getMessage()); + } + } else if (eventType.equalsIgnoreCase("S")) { + try { + int eventId = eventManager.create(eventTime); + eventManager.start(eventId); + LOGGER.info("Egg [{}] is being boiled.", eventId); + } catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException + | EventDoesNotExistException e) { + LOGGER.error(e.getMessage()); + } + } else { + LOGGER.info("Unknown event type."); + } + } + } diff --git a/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/EventManager.java b/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/EventManager.java index 729900620..d54665bb2 100644 --- a/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/EventManager.java +++ b/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/EventManager.java @@ -39,6 +39,8 @@ public class EventManager implements ThreadCompleteListener { private int currentlyRunningSyncEvent = -1; private Random rand; private Map eventPool; + + private static final String DOES_NOT_EXIST = " does not exist."; /** * EventManager constructor. @@ -112,7 +114,7 @@ public class EventManager implements ThreadCompleteListener { */ public void start(int eventId) throws EventDoesNotExistException { if (!eventPool.containsKey(eventId)) { - throw new EventDoesNotExistException(eventId + " does not exist."); + throw new EventDoesNotExistException(eventId + DOES_NOT_EXIST); } eventPool.get(eventId).start(); @@ -126,7 +128,7 @@ public class EventManager implements ThreadCompleteListener { */ public void cancel(int eventId) throws EventDoesNotExistException { if (!eventPool.containsKey(eventId)) { - throw new EventDoesNotExistException(eventId + " does not exist."); + throw new EventDoesNotExistException(eventId + DOES_NOT_EXIST); } if (eventId == currentlyRunningSyncEvent) { @@ -145,7 +147,7 @@ public class EventManager implements ThreadCompleteListener { */ public void status(int eventId) throws EventDoesNotExistException { if (!eventPool.containsKey(eventId)) { - throw new EventDoesNotExistException(eventId + " does not exist."); + throw new EventDoesNotExistException(eventId + DOES_NOT_EXIST); } eventPool.get(eventId).status(); diff --git a/event-sourcing/src/main/java/com/iluwatar/event/sourcing/domain/Account.java b/event-sourcing/src/main/java/com/iluwatar/event/sourcing/domain/Account.java index d2e5f429b..a8ba649eb 100644 --- a/event-sourcing/src/main/java/com/iluwatar/event/sourcing/domain/Account.java +++ b/event-sourcing/src/main/java/com/iluwatar/event/sourcing/domain/Account.java @@ -44,6 +44,8 @@ public class Account { private final int accountNo; private final String owner; private BigDecimal money; + + private static final String MSG = "Some external api for only realtime execution could be called here."; /** * Instantiates a new Account. @@ -126,7 +128,7 @@ public class Account { depositMoney(money); AccountAggregate.putAccount(this); if (realTime) { - LOGGER.info("Some external api for only realtime execution could be called here."); + LOGGER.info(MSG); } } @@ -138,7 +140,7 @@ public class Account { withdrawMoney(money); AccountAggregate.putAccount(this); if (realTime) { - LOGGER.info("Some external api for only realtime execution could be called here."); + LOGGER.info(MSG); } } @@ -160,7 +162,7 @@ public class Account { public void handleEvent(AccountCreateEvent accountCreateEvent) { AccountAggregate.putAccount(this); if (accountCreateEvent.isRealTime()) { - LOGGER.info("Some external api for only realtime execution could be called here."); + LOGGER.info(MSG); } }