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

View File

@ -25,13 +25,25 @@ public class Event implements IEvent, Runnable {
private int eventId;
private int eventTime;
private boolean isSynchronous;
private Thread thread;
private boolean isComplete = false;
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.eventTime = eventTime;
this.isSynchronous = isSynchronous;
}
public boolean isSynchronous() {
return isSynchronous;
}
@Override
@ -42,6 +54,9 @@ public class Event implements IEvent, Runnable {
@Override
public void stop() {
if (null == thread) {
return;
}
thread.interrupt();
}

View File

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

View File

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