-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCarServiceSimulation.java
More file actions
125 lines (106 loc) · 4.58 KB
/
CarServiceSimulation.java
File metadata and controls
125 lines (106 loc) · 4.58 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
import java.util.Random;
import java.util.Scanner;
public class CarServiceSimulation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random rand = new Random();
CarServer sedanServer = new CarServer("Sedan");
sedanServer.addServiceTableRow(30, 2);
sedanServer.addServiceTableRow(28, 3);
sedanServer.addServiceTableRow(25, 4);
sedanServer.addServiceTableRow(17, 5);
CarServer SUVServer = new CarServer("SUV");
SUVServer.addServiceTableRow(35, 3);
SUVServer.addServiceTableRow(25, 4);
SUVServer.addServiceTableRow(20, 5);
SUVServer.addServiceTableRow(20, 6);
int prevArrivalTime = 0;
int totalWaitingTime = 0;
int totalServiceTime = 0;
int customersWhoWait = 0;
int simulationTime = 0;
System.out.print("Enter the number of cars to simulate for: ");
int totalCustomers = sc.nextInt();
System.out.println(
"\n-------------------------------------------------------------------------------------------------------------");
System.out.println("No\tRD_Car\tCar\tRD_IAT\tIAT\tAT\tServer\tRD_ST\tST\tSB\tSE\tWT\tIdle");
System.out.println(
"-------------------------------------------------------------------------------------------------------------");
for (int i = 0; i < totalCustomers; i++) {
CarServer servicedBy = null;
int randomDigitForInterArrivalTime = rand.nextInt(100);
int interArrivalTime = 0;
if (i == 0)
interArrivalTime = 0;
else if (randomDigitForInterArrivalTime < 26)
interArrivalTime = 1;
else if (randomDigitForInterArrivalTime < 66)
interArrivalTime = 2;
else if (randomDigitForInterArrivalTime < 86)
interArrivalTime = 3;
else
interArrivalTime = 4;
prevArrivalTime += interArrivalTime;
int randomDigitForCarType = rand.nextInt(100);
String carType = "";
if (randomDigitForCarType < 50) {
carType = "Sedan";
if (sedanServer.busyTill <= prevArrivalTime)
servicedBy = sedanServer;
else if (SUVServer.busyTill <= prevArrivalTime)
servicedBy = SUVServer;
else
servicedBy = sedanServer;
} else {
carType = "SUV";
if (SUVServer.busyTill <= prevArrivalTime)
servicedBy = SUVServer;
else if (sedanServer.busyTill <= prevArrivalTime)
servicedBy = sedanServer;
else
servicedBy = SUVServer;
}
int randomDigitForServiceTime = rand.nextInt(100);
int serviceTime = 0, waitingTime, idleTime, serviceBegin, serviceEnd;
for (int j = 0; j < servicedBy.higherLimits.size(); j++)
if (randomDigitForServiceTime <= servicedBy.higherLimits.get(j)) {
serviceTime = servicedBy.serviceValues.get(j);
break;
}
if (servicedBy.busyTill <= prevArrivalTime) {
waitingTime = 0;
serviceBegin = prevArrivalTime;
idleTime = prevArrivalTime - servicedBy.busyTill;
} else {
customersWhoWait++;
waitingTime = servicedBy.busyTill - prevArrivalTime;
serviceBegin = servicedBy.busyTill;
idleTime = 0;
}
servicedBy.idleTime += idleTime;
serviceEnd = servicedBy.busyTill = serviceBegin + serviceTime;
System.out.println((i + 1) + "\t" + randomDigitForCarType + "\t" + carType + "\t"
+ randomDigitForInterArrivalTime + "\t" + interArrivalTime + "\t" + prevArrivalTime + "\t"
+ servicedBy.getType() + "\t" + randomDigitForServiceTime + "\t" + serviceTime + "\t" + serviceBegin
+ "\t" + serviceEnd + "\t" + waitingTime + "\t" + idleTime);
totalWaitingTime += waitingTime;
totalServiceTime += serviceTime;
simulationTime = serviceEnd;
}
System.out.println("\n\n-----------------------------------------------------");
System.out.println("Simulation statistics");
System.out.println("-----------------------------------------------------");
System.out.printf("Average waiting time:\t\t\t\t%.2f\n", (totalWaitingTime * 1.0 / totalCustomers));
System.out.printf("Probability that a customer has to wait:\t%.2f\n",
(customersWhoWait * 1.0 / totalCustomers));
System.out.printf("Probability of sedan server being idle:\t\t%.2f\n",
(sedanServer.idleTime * 1.0 / simulationTime));
System.out.printf("Probability of SUV server being idle:\t\t%.2f\n",
(SUVServer.idleTime * 1.0 / simulationTime));
System.out.printf("Average service time:\t\t\t\t%.2f\n", (totalServiceTime * 1.0 / totalCustomers));
System.out.printf("Average time between intervals:\t\t\t%.2f\n", (prevArrivalTime * 1.0 / totalCustomers));
System.out.printf("Average waiting time of those who wait:\t\t%.2f\n",
(totalWaitingTime * 1.0 / customersWhoWait));
sc.close();
}
}