diff --git a/app/src/main/java/simulation/Event.java b/app/src/main/java/simulation/Event.java index a47928d..4dc1cad 100644 --- a/app/src/main/java/simulation/Event.java +++ b/app/src/main/java/simulation/Event.java @@ -1,6 +1,6 @@ package simulation; -class Event{ +class Event { public double eventTime; public int eventType; public Event next; diff --git a/app/src/main/java/simulation/EventList.java b/app/src/main/java/simulation/EventList.java index fb469a3..2e1ebe7 100644 --- a/app/src/main/java/simulation/EventList.java +++ b/app/src/main/java/simulation/EventList.java @@ -1,33 +1,35 @@ package simulation; -public class EventList{ - +public class EventList { + private Event list, last; - - EventList(){ + + EventList() { list = new Event(); - last = new Event(); - list.next = last; + last = new Event(); + list.next = last; } - - public void InsertEvent(int type, double TimeOfEvent){ - Event dummy, predummy; - Event newEvent = new Event(); - newEvent.eventType = type; - newEvent.eventTime = TimeOfEvent; - predummy = list; - dummy = list.next; - while ((dummy.eventTime < newEvent.eventTime) & (dummy != last)){ - predummy = dummy; - dummy = dummy.next; - } - predummy.next = newEvent; - newEvent.next = dummy; - } - - public Event FetchEvent(){ - Event dummy; - dummy = list.next; + + public void InsertEvent(int type, double TimeOfEvent) { + Event dummy, predummy; + predummy = this.list; + dummy = this.list.next; + + Event newEvent = new Event(); + newEvent.eventType = type; + newEvent.eventTime = TimeOfEvent; + + while ((dummy.eventTime < newEvent.eventTime) && (dummy != last)) { + predummy = dummy; + dummy = dummy.next; + } + + predummy.next = newEvent; + newEvent.next = dummy; + } + + public Event FetchEvent() { + Event dummy = list.next; list.next = dummy.next; dummy.next = null; return dummy; diff --git a/app/src/main/java/simulation/MainSimulation.java b/app/src/main/java/simulation/MainSimulation.java index c6ac416..7f22336 100644 --- a/app/src/main/java/simulation/MainSimulation.java +++ b/app/src/main/java/simulation/MainSimulation.java @@ -2,20 +2,22 @@ package simulation; import java.io.*; -public class MainSimulation extends GlobalSimulation{ - - public static void main(String[] args) throws IOException { - Event actEvent; - EventList myEventList = new EventList(); - State actState = new State(myEventList); - myEventList.InsertEvent(ARRIVAL, 0); - myEventList.InsertEvent(MEASURE, 5); - while (time < 50000){ - actEvent = myEventList.FetchEvent(); - time = actEvent.eventTime; - actState.TreatEvent(actEvent); - } - System.out.println("Mean number of customers: " + 1.0*actState.accumulated/actState.noMeasurements); - System.out.println("Number of measurements done: " + actState.noMeasurements); - } +public class MainSimulation extends GlobalSimulation { + + public static void main(String[] args) throws IOException { + Event actEvent; + EventList myEventList = new EventList(); + + State actState = new State(myEventList); + + myEventList.InsertEvent(ARRIVAL, 0); + myEventList.InsertEvent(MEASURE, 5); + while (MainSimulation.time < 50000) { + actEvent = myEventList.FetchEvent(); + MainSimulation.time = actEvent.eventTime; + actState.TreatEvent(actEvent); + } + System.out.println("Mean number of customers: " + (double) actState.accumulated / actState.noMeasurements); + System.out.println("Number of measurements done: " + actState.noMeasurements); + } } \ No newline at end of file diff --git a/app/src/main/java/simulation/State.java b/app/src/main/java/simulation/State.java index 29cff41..5f192f4 100644 --- a/app/src/main/java/simulation/State.java +++ b/app/src/main/java/simulation/State.java @@ -2,25 +2,24 @@ package simulation; import java.util.*; -class State extends GlobalSimulation{ - +class State extends GlobalSimulation { + public int numberInQueue = 0, accumulated = 0, noMeasurements = 0; - + private EventList myEventList; - Random slump = new Random(); - - State(EventList x){ + static Random slump = new Random(); + + State(EventList x) { myEventList = x; } - - private void InsertEvent(int event, double timeOfEvent){ + + private void InsertEvent(int event, double timeOfEvent) { myEventList.InsertEvent(event, timeOfEvent); } - - - public void TreatEvent(Event x){ - switch (x.eventType){ + + public void TreatEvent(Event x) { + switch (x.eventType) { case ARRIVAL: arrival(); break; @@ -32,25 +31,25 @@ class State extends GlobalSimulation{ break; } } - - private double generateMean(double mean){ - return 2*mean*slump.nextDouble(); + + private double generateMean(double mean) { + return 2 * mean * slump.nextDouble(); } - - private void arrival(){ + + private void arrival() { if (numberInQueue == 0) InsertEvent(READY, time + generateMean(1)); numberInQueue++; InsertEvent(ARRIVAL, time + generateMean(2)); } - - private void ready(){ + + private void ready() { numberInQueue--; if (numberInQueue > 0) InsertEvent(READY, time + generateMean(1)); } - - private void measure(){ + + private void measure() { accumulated = accumulated + numberInQueue; noMeasurements++; InsertEvent(MEASURE, time + generateMean(5));