Changes based on latest code review

This commit is contained in:
WSSIA 2016-09-19 21:50:04 +01:00
parent f2159515bc
commit dbd605e378
4 changed files with 43 additions and 18 deletions

View File

@ -138,11 +138,10 @@ public class App {
Scanner s = new Scanner(System.in); Scanner s = new Scanner(System.in);
int option = -1; int option = -1;
while (option != 5) { while (option != 4) {
System.out.println("Hello. Would you like to boil some eggs?"); System.out.println("Hello. Would you like to boil some eggs?");
System.out.println( System.out.println("(1) BOIL AN EGG \n(2) STOP BOILING THIS EGG \n(3) HOW ARE MY EGGS? \n(4) EXIT");
"(1) BOIL AN EGG \n(2) STOP BOILING THIS EGG \n(3) HOW IS MY EGG? \n(4) HOW ARE MY EGGS? \n(5) EXIT"); System.out.print("Choose [1,2,3,4]: ");
System.out.print("Choose [1,2,3,4,5]: ");
option = s.nextInt(); option = s.nextInt();
if (option == 1) { if (option == 1) {
@ -181,16 +180,22 @@ public class App {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
} }
} else if (option == 3) { } else if (option == 3) {
System.out.print("Which egg?: "); s.nextLine();
int eventId = s.nextInt(); System.out.print("Just one egg (O) OR all of them (A) ?: ");
try { String eggChoice = s.nextLine();
eventManager.status(eventId);
} catch (EventDoesNotExistException e) { if (eggChoice.equalsIgnoreCase("O")) {
System.out.println(e.getMessage()); System.out.print("Which egg?: ");
int eventId = s.nextInt();
try {
eventManager.status(eventId);
} catch (EventDoesNotExistException e) {
System.out.println(e.getMessage());
}
} else if (eggChoice.equalsIgnoreCase("A")) {
eventManager.statusOfAllEvents();
} }
} else if (option == 4) { } else if (option == 4) {
eventManager.statusOfAllEvents();
} else if (option == 5) {
eventManager.shutdown(); eventManager.shutdown();
} }
} }

View File

@ -25,13 +25,25 @@ public class Event implements IEvent, Runnable {
private int eventId; private int eventId;
private int eventTime; private int eventTime;
private boolean isSynchronous;
private Thread thread; private Thread thread;
private boolean isComplete = false; private boolean isComplete = false;
private ThreadCompleteListener eventListener; private ThreadCompleteListener eventListener;
public Event(final int eventId, final int eventTime) { /**
*
* @param eventId event ID
* @param eventTime event time
* @param isSynchronous is of synchronous type
*/
public Event(final int eventId, final int eventTime, final boolean isSynchronous) {
this.eventId = eventId; this.eventId = eventId;
this.eventTime = eventTime; this.eventTime = eventTime;
this.isSynchronous = isSynchronous;
}
public boolean isSynchronous() {
return isSynchronous;
} }
@Override @Override
@ -42,6 +54,9 @@ public class Event implements IEvent, Runnable {
@Override @Override
public void stop() { public void stop() {
if (null == thread) {
return;
}
thread.interrupt(); thread.interrupt();
} }

View File

@ -61,11 +61,12 @@ public class EventManager implements ThreadCompleteListener {
*/ */
public int create(int eventTime) public int create(int eventTime)
throws MaxNumOfEventsAllowedException, InvalidOperationException, LongRunningEventException { throws MaxNumOfEventsAllowedException, InvalidOperationException, LongRunningEventException {
int eventId = createEvent(eventTime);
if (currentlyRunningSyncEvent != -1) { if (currentlyRunningSyncEvent != -1) {
throw new InvalidOperationException( throw new InvalidOperationException(
"Event [" + currentlyRunningSyncEvent + "] is still running. Please wait until it finishes and try again."); "Event [" + currentlyRunningSyncEvent + "] is still running. Please wait until it finishes and try again.");
} }
int eventId = createEvent(eventTime, true);
currentlyRunningSyncEvent = eventId; currentlyRunningSyncEvent = eventId;
return eventId; return eventId;
@ -80,10 +81,11 @@ public class EventManager implements ThreadCompleteListener {
* @throws LongRunningEventException Long running events are not allowed in the app. * @throws LongRunningEventException Long running events are not allowed in the app.
*/ */
public int createAsync(int eventTime) throws MaxNumOfEventsAllowedException, LongRunningEventException { public int createAsync(int eventTime) throws MaxNumOfEventsAllowedException, LongRunningEventException {
return createEvent(eventTime); return createEvent(eventTime, false);
} }
private int createEvent(int eventTime) throws MaxNumOfEventsAllowedException, LongRunningEventException { private int createEvent(int eventTime, boolean isSynchronous)
throws MaxNumOfEventsAllowedException, LongRunningEventException {
if (eventPool.size() == MAX_RUNNING_EVENTS) { if (eventPool.size() == MAX_RUNNING_EVENTS) {
throw new MaxNumOfEventsAllowedException("Too many events are running at the moment. Please try again later."); throw new MaxNumOfEventsAllowedException("Too many events are running at the moment. Please try again later.");
} }
@ -95,7 +97,7 @@ public class EventManager implements ThreadCompleteListener {
int newEventId = generateId(); int newEventId = generateId();
Event newEvent = new Event(newEventId, eventTime); Event newEvent = new Event(newEventId, eventTime, isSynchronous);
newEvent.addListener(this); newEvent.addListener(this);
eventPool.put(newEventId, newEvent); eventPool.put(newEventId, newEvent);
@ -194,6 +196,9 @@ public class EventManager implements ThreadCompleteListener {
@Override @Override
public void completedEventHandler(int eventId) { public void completedEventHandler(int eventId) {
eventPool.get(eventId).status(); eventPool.get(eventId).status();
if (eventPool.get(eventId).isSynchronous()) {
currentlyRunningSyncEvent = -1;
}
eventPool.remove(eventId); eventPool.remove(eventId);
} }

View File

@ -1 +1 @@
INTERACTIVE_MODE=NO INTERACTIVE_MODE=YES