This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
101 lines (81 loc) · 4.02 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Import classes from java packages
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;
// Import packages and classes from simulator package
import simulator.Server;
import simulator.Comparison;
import simulator.Event;
import simulator.ArriveEvent;
import simulator.ServeEvent;
import simulator.DoneEvent;
import simulator.LeaveEvent;
import simulator.Customer;
class Main {
public static void main(String[] args) {
final Scanner sc = new Scanner(System.in);
/**
* So there is a pointer/reference, pointing to the List of servers.
*
* Everytime I create a new ArriveEvent and pass it the List of servers, I am
* passing its pointer BY VALUE to the new event object. However since the
* parameter passed is actually a pointer, modifying/replacing Server Objects in
* the List will actually still modify the original Servers List.
*
* This means that, EVERY SINGLE EVENT object will ALWAYS have a reference to
* the same Servers List, and any of them modifying any Server Object in the
* List, modify the objects in the List.
*
* Thus, EVERY OTHER EVENT, can access the updated Server Objects in the List.
* Specifically through the "ServerList.getServerByID" static method.
*/
List<Server> servers = new ArrayList<Server>();
// List to store all the events as strings to output after simulation
List<String> schedule = new ArrayList<String>();
// @todo Remove hardcoded initial capacity
// 11 following the default value of the class
PriorityQueue<Event> queue = new PriorityQueue<Event>(11, new Comparison());
// Create list of servers at the start using the first user input
// Server ID starts from 1
for (int i = 1, numOfServers = sc.nextInt(); i <= numOfServers; i++)
servers.add(new Server(i, true, false, 0));
// Customer ID starts from 1
// Read user input 1 by 1, and create a customer object, before
// Inserting an Arrive event into Queue for every customer
for (int customerID = 1; sc.hasNextDouble(); customerID++)
queue.add(new ArriveEvent(new Customer(customerID, sc.nextDouble()), servers));
// Explicitly close scanner to prevent resource leak
sc.close();
// Loop to simulate running through all the events.
while (!queue.isEmpty()) {
final Event event = queue.poll();
// Only for DoneEvent and LeaveEvent, where their execute methods are not
// expected to return anymore new events, do we skip adding new events.
if (event instanceof DoneEvent || event instanceof LeaveEvent)
event.execute();
else
queue.add(event.execute());
// Add the string representation of each event after execution into the schedule
// List to be displayed after simulation completes.
schedule.add(event.toString());
}
// Sort schedule by execution time (the first number in each string)
Collections.sort(schedule);
// Print the string representation of the entire simulation
for (String schedules : schedule)
System.out.println(schedules);
// Get and print the statistics of the simulation.
getStatistics();
}
public static void getStatistics() {
// Statistics 1, "the average waiting time for customers who have been served"
final double stat1 = ServeEvent.getTotalWaitingTime() / ServeEvent.getNumberOfCustomersServed();
// Statistics 2, "the number of customers served"
final int stat2 = ServeEvent.getNumberOfCustomersServed();
// Statistics 3, "the number of customers who left without being served"
final int stat3 = LeaveEvent.getNumberOfCustomersLeftWithoutService();
System.out.printf("[%.3f %d %d]\n", stat1, stat2, stat3);
}
}