-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBusEvent.java
More file actions
115 lines (99 loc) · 3.88 KB
/
BusEvent.java
File metadata and controls
115 lines (99 loc) · 3.88 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
import java.util.*;
import java.lang.*;
// inspired by FerryEvent in CSCI 1933 Lab 10
public class BusEvent implements Event {
private int currentStop;
private Bus bus;
private double timeRemaining;
public BusEvent (int s, Bus bus){
this.currentStop = s;
this.bus = bus;
this.timeRemaining = 15;
}
public void setStop(int s){
this.currentStop = s;
}
public int getStopNumber(){
return this.currentStop;
}
public Bus getBus(){
return this.bus;
}
public void setTimeRemaining(double t){
this.timeRemaining = t;
}
/* bus takes 4 min (240 s) to travel between bus stops
takes passengers 2 seconds to get off bus, 3 seconds to get on
bus will either wait at stop for 15 seconds or whatever time it takes for riders
to get on & off
*/
public void run(){
// 1. removes riders who want to exit bus at this stop.
Rider[] removed = bus.removeRidersAtStop(this.currentStop);
double time = removed.length*2;
// 2. boards as many riders who want to go in the bus' direction as possible
int numberBoarded = 0;
if (!bus.isExpress()){
// if the bus isn't express, board all the passengers at the stop
while (!(this.bus.isFull()) && (BusSim.stopList[currentStop].getQLength() > 0)){
Rider r = BusSim.stopList[currentStop].getWaitline().remove();
this.bus.addRider(r);
BusSim.riderStats.updateWaitTimeStats(BusSim.agenda.getCurrentTime(), r.getArrivalTime());
numberBoarded++;
}
} else {
// if it is an express bus, it only boards certain passengers
Q1Gen<Rider> dummy = new Q1Gen<Rider>();
// ^^ dummy queue for all the passengers who aren't going to express stops
ArrayList<Rider> toBoard = new ArrayList<Rider>();
while (!(this.bus.isFull()) && (BusSim.stopList[currentStop].getQLength() > 0)){
Rider s = (BusSim.stopList[currentStop].getWaitline()).remove();
int dest = s.getDestination();
if ((dest%4)==0 || dest == 0 || dest == 1 || dest == 14 || dest == 15 || dest == 29){
this.bus.addRider(s);
BusSim.riderStats.updateWaitTimeStats(BusSim.agenda.getCurrentTime(), s.getArrivalTime());
} else {
dummy.add(s);
}
numberBoarded++;
}
// reverse order of dummy node, copy over dummy to actual waitline
while (dummy.length() > 0) {
(BusSim.stopList[currentStop]).enter(dummy.remove());
}
}
time += (numberBoarded*3);
// System.out.println("Bus " + this.bus.getNumber() + " deboarded " + removed.length +
// " passengers and boarded " + numberBoarded + " riders at Stop " + this.currentStop +
// " for " + time + "seconds.");
if (time != 0){
BusSim.agenda.add(this, time);
//this.bus.updateServiceTimeStats(time);
this.setTimeRemaining(this.timeRemaining - time);
return;
}
if (this.timeRemaining <= 0){
BusSim.busStats.updateCapacityStats(BusSim.agenda.getCurrentTime(), this.bus.getCurrentNumOfPassengers());
if (!bus.isExpress()){
// if the bus isn't express, just schedule it for the next stop
this.setStop((this.currentStop+1)%30);
BusSim.agenda.add(this, 240);
} else {
// if the bus is express, you must schedule the next stop differently
int[] expressStops = {0, 1, 4, 8, 12, 14, 15, 16, 20, 24, 28, 29};
int idx = 0;
for (int j = 0; j < expressStops.length; j++)
if (this.currentStop == expressStops[j]){
idx = j;
break;
}
this.setStop(expressStops[(idx+1)%expressStops.length]);
BusSim.agenda.add(this, 240);
return;
}
} else {
BusSim.agenda.add(this, timeRemaining);
this.timeRemaining = 0;
}
} // run() method
} // BusEvent class