-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.java
More file actions
161 lines (143 loc) · 5.5 KB
/
Copy pathSimulation.java
File metadata and controls
161 lines (143 loc) · 5.5 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.util.*;
/**
* The simulation blue print
*
* @author Khalid Almotaery
* @version 11/7/2020
*/
public class Simulation
{
//Information object:
Information i;
//needed fields:
final int CLOSING_TIME = 54000;
int casherSalary;
int cashersTotal=0;
int cashersAvailable;
int customerNumber=0;
int howLong=0;
int overFlowCount=0;
int dailyProfit=0;
int nextEvent=0;
int netProfit =0;
double avWaitTime=0;
double percentOverFlow=0;
// Data structures:
PriorityQueue<Event> eventSet;
ArrayList<Customer> customerList;
LinkedList<Customer> customerLinkedList;
ArrayDeque<Customer> customerDeque;
ArrayDeque<Customer> queue = new ArrayDeque<Customer>();
Queue<Event> q;
Iterator<Customer> itr;
/**
* Initializes the data structures and converts the LinkedList from Information to an ArrayList
*/
public Simulation(int cashers,int dequeOrLinkedList)
{
i = new Information();
eventSet = new PriorityQueue<Event>();
customerList = new ArrayList<Customer>();
customerDeque = new ArrayDeque<Customer>();
cashersAvailable=cashers;
cashersTotal = cashers;
casherSalary = i.getStaffingCost();
customerLinkedList=i.getCustomerLinkedList();
itr = customerLinkedList.iterator();
while(itr.hasNext()){
customerList.add(itr.next());
}
if(dequeOrLinkedList==0){
q = new ArrayDeque<Event>();
}else{
q = new LinkedList<Event>();
}
nextEvent();
}
/**
* Adds all the arrival events to the priorty queue
*
* @param null
* @return null
*/
public void nextEvent(){
for(int j=0; j<customerList.size(); j++){
Event ev = new Event(j,customerList.get(j).arrivalTime,"ARRIVAL",false);
eventSet.add(ev);
}
runSimulation();
}
/**
* Runs the whole simlation
*
* @param null
* @return null
*/
public void runSimulation(){
//needed local varibales:
int prevCusServTime=0;
int waitingTime=0;
int profit=0;
int counter=0;
Event e = null; // the event object
while(!eventSet.isEmpty()){ // loos until there are no events left
e = eventSet.remove(); //getting and removing the sooner events
if(e.time>CLOSING_TIME){ //stops looping when the store is closed
break;
}
//Departure case:
if(e.type.equals("Departure")){
cashersAvailable++; //makes more cashers available
System.out.println("User # "+e.name+" Leaves at time "+e.time);
//makes the next person from the queue if it is not empty:
if(!q.isEmpty()){
prevCusServTime=e.time;
e = q.poll();
e.time = prevCusServTime;
waitingTime = (e.time) - customerList.get(e.name).arrivalTime;
avWaitTime=avWaitTime+waitingTime;
counter++;
e.wasInQueue =true;
eventSet.add(e);
}
}else{
if(e.wasInQueue){
System.out.println("User # "+e.name+" is out of queue at time "+e.time+" the user waited in the queue for" +waitingTime);
}else{
System.out.println("User # "+e.name+" Enters at time "+e.time);
}
//Serves the custmoer and makes a departure event:
if(cashersAvailable>0){
cashersAvailable--;
howLong = customerList.get(e.name).serviceTime;
profit=customerList.get(e.name).profitGained;
dailyProfit+=profit;
System.out.println("And gets served for "+howLong+" seconds"+" paying $"+profit);
e.time+=howLong;
e.type = "Departure";
eventSet.add(e);
}else{
System.out.println("User # "+e.name+" waits in queue at time "+e.time/*+" and gets out of queue at time "+prevCusServTime*/);
//makes customers wait in the queue:
if(queue.size()<cashersTotal*8){
queue.add(customerList.get(e.name));
q.add(e);
}else{
//makes a customer overflow when the queue is c*8 long
System.out.println("User # "+e.name+" is an overflow!");
eventSet.remove(e.name);
overFlowCount++;
}
}
}
}
double dOverFlowCount=overFlowCount;
double dSize=customerList.size();
percentOverFlow = ((dOverFlowCount/dSize)*100);
netProfit = (dailyProfit-(cashersTotal*casherSalary));
avWaitTime = (avWaitTime/counter);
System.out.println("With "+cashersTotal+" cashers, the restaurant has %"+percentOverFlow+" cutomers that were an overflow.");
System.out.println("The daily net profit is: $"+netProfit);
System.out.println("The maximum average waiting time is: "+avWaitTime+" Seconds");
}
}