Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -150,56 +151,11 @@ public void runInteractiveMode() {
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();
}
Expand All @@ -208,4 +164,61 @@ public void runInteractiveMode() {
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.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class EventManager implements ThreadCompleteListener {
private int currentlyRunningSyncEvent = -1;
private Random rand;
private Map<Integer, Event> eventPool;

private static final String DOES_NOT_EXIST = " does not exist.";

/**
* EventManager constructor.
Expand Down Expand Up @@ -112,7 +114,7 @@ private int createEvent(int eventTime, boolean isSynchronous)
*/
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();
Expand All @@ -126,7 +128,7 @@ public void start(int eventId) throws EventDoesNotExistException {
*/
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) {
Expand All @@ -145,7 +147,7 @@ public void cancel(int eventId) throws EventDoesNotExistException {
*/
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -126,7 +128,7 @@ private void handleDeposit(BigDecimal money, boolean realTime) {
depositMoney(money);
AccountAggregate.putAccount(this);
if (realTime) {
LOGGER.info("Some external api for only realtime execution could be called here.");
LOGGER.info(MSG);
}
}

Expand All @@ -138,7 +140,7 @@ private void handleWithdrawal(BigDecimal money, boolean realTime) {
withdrawMoney(money);
AccountAggregate.putAccount(this);
if (realTime) {
LOGGER.info("Some external api for only realtime execution could be called here.");
LOGGER.info(MSG);
}
}

Expand All @@ -160,7 +162,7 @@ public void handleEvent(MoneyDepositEvent moneyDepositEvent) {
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);
}
}

Expand Down