-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParkingGarage.java
127 lines (117 loc) · 4.33 KB
/
ParkingGarage.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
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
import java.util.Random;
import java.util.Vector;
/**
* This class simulates activity inside of a 3-story parking garage at a grocery store.
*/
public class ParkingGarage{
// array representing the garage itself
@SuppressWarnings("unchecked")
private ArrayBag<Car>[] parkingGarage = (ArrayBag<Car>[]) new ArrayBag[3];
// Queue containing cars waiting to enter the garage
private QueueInterface<Car> entryLine = new LinkedQueue<Car>();
// Variables for performance tracking
private int numberOfCars; // the number of cars that parked during the simulation
private int totalTimeWaited; // the total number of clock ticks cars waited in queue before parking
private int totalArrivals; // the number of cars that arrived at the garage, regardless of whether or not they parked
public ParkingGarage(){
ArrayBag<Car> f1 = new ArrayBag<Car>(10);
ArrayBag<Car> f2 = new ArrayBag<Car>(10);
ArrayBag<Car> f3 = new ArrayBag<Car>(10);
parkingGarage[0] = f1;
parkingGarage[1] = f2;
parkingGarage[2] = f3;
}
public void simulate(int duration, double arrivalProb){
Random rand = new Random();
for (int time = 0; time <= duration; time++){
if (rand.nextFloat() < 0.3){ //chance a car leaves given that there's a car in the garage
int level = randomLevel();
if (level != -1){
Car departingCar = parkingGarage[level].randRemove();
}
}
if (rand.nextFloat() < arrivalProb){
Car newCar = new Car(time);
totalArrivals++;
int nextSpot = nextAvailableLocation();
if (nextSpot != -1){
if (entryLine.isEmpty()){
parkingGarage[nextSpot].add(newCar);
}
else{
Car enteringCar = entryLine.dequeue();
parkingGarage[nextSpot].add(enteringCar);
totalTimeWaited += time - enteringCar.getArrivalTime();
entryLine.enqueue(newCar);
}
numberOfCars++;
}
else {
entryLine.enqueue(newCar);
}
}
}
displayResults();
}
public int nextAvailableLocation(){
for (int i = 0; i < parkingGarage.length; i++){
if (!parkingGarage[i].isArrayFull()){
return i;
}
}
return -1;
}
public int randomLevel(){
Vector<Integer> occupiedFloors = new Vector<>();
for (int i = 0; i < parkingGarage.length; i++){
if (!parkingGarage[i].isEmpty()){
occupiedFloors.add(i);
}
}
Random rand = new Random();
if (occupiedFloors.size() > 0){
return occupiedFloors.get(rand.nextInt(occupiedFloors.size()));
}
else {
return -1;
}
}
/**
* Prints out relevant statistics about the simulation.
*/
public void displayResults(){
System.out.println("Cars parked: " + numberOfCars);
System.out.println("Cars in garage: " + totalArrivals);
System.out.println("Average wait time: " + (totalTimeWaited / numberOfCars));
System.out.println("Cars waiting to get in: " + countCars(entryLine));
for (int i = 0; i < parkingGarage.length; i++){
System.out.println("Number of cars on floor " + i + ": " + parkingGarage[i].getSize());
}
clearGarage();
}
/**
* retrieves the number of cars in a given queue
* @param carQueue the queueinterface implementing object whose contents will be counted
* @return an int equal to the number of cars in the queue
*/
public int countCars(QueueInterface<Car> carQueue){
int counter = 0;
while (!carQueue.isEmpty()){
counter++;
carQueue.dequeue();
}
return counter;
}
/**
* Completely resets the garage for a new simulation
*/
public void clearGarage(){
numberOfCars = 0;
totalTimeWaited = 0;
totalArrivals = 0;
for (int i = 0; i < parkingGarage.length; i++){
parkingGarage[i].clear();
}
entryLine.clear();
}
}