-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
231 lines (200 loc) · 6.69 KB
/
Main.java
File metadata and controls
231 lines (200 loc) · 6.69 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE
package main;
import java.util.*;
import vehicles.*;
import java.io.*;
import passengers.*;
import locations.*;
/**
* Defines a class that implements a simulation of a transportation system.
* @author leylayayladere
* @version 1.0
*/
public class Main {
/**
* Reads the input from input file, takes the operation number from input and then does the appropriate operations.
* <p>After does the appropriate operations which are given in input file, writes every locations and passengers
* in this locations to output file.
* @param args Takes input file to give an appropriate output file, also in order to test the code.
* @throws FileNotFoundException Prevents from an error in case of the file not found.
*/
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File(args[0]));
PrintStream output = new PrintStream(new File(args[1]));
ArrayList<Passenger> passengers = new ArrayList<Passenger>();
ArrayList<Location> locations = new ArrayList<Location>();
ArrayList<PublicTransport> vehicles = new ArrayList<PublicTransport>();
// Uncomment the lines below after implementing the Location class
Location l = new Location(0, 0, 0); // The first location is always (0,0).
locations.add(l);
int operations = input.nextInt(); // operation count
int locationID=1;
int passengerID=0;
int vehicleID=0;
while(operations!=0) {
int wantedPassenger = 0;
int wantedLocation = 0;
int wantedVehicle = 0;
int action = input.nextInt();
if(action==1) {
String s = input.next();
int driversLicense = input.nextInt();
boolean hasDriversLicense = (driversLicense==1);
int car = input.nextInt();
if(car==1) {
double fuelConsumption = input.nextDouble();
if(s.equals("D")) {
passengers.add(new DiscountedPassenger(passengerID, l, fuelConsumption));
}
if(s.equals("S")) {
passengers.add(new StandardPassenger(passengerID, l, fuelConsumption));
}
}else {
if(s.equals("D")) {
passengers.add(new DiscountedPassenger(passengerID, hasDriversLicense, l));
}
if(s.equals("S")) {
passengers.add(new StandardPassenger(passengerID, hasDriversLicense, l));
}
}
passengers.get(passengerID).currentLocation = l;
l.incomingPassenger(passengers.get(passengerID));
passengerID++;
}
if(action==2) {
double locationX = input.nextDouble();
double locationY = input.nextDouble();
locations.add(new Location(locationID, locationX, locationY));
locationID++;
}
if(action==3) {
int transportationType = input.nextInt();
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
if(transportationType == 1) {
vehicles.add(new Bus(vehicleID, x1, y1, x2, y2));
}else {
vehicles.add(new Train(vehicleID, x1, y1, x2, y2));
}
vehicleID++;
}
if(action==4) {
int IDpassenger = input.nextInt();
int IDlocation = input.nextInt();
int transportationType = input.nextInt();
boolean existVehicle = false;
if(transportationType!=3) {
int IDvehicle = input.nextInt();
if(vehicleID>=IDvehicle) {
for(int i =0; i<vehicles.size(); i++) {
if(vehicles.get(i).getID() == IDvehicle) {
wantedVehicle = i;
}
}
existVehicle = true;
}
}
if(passengerID>=IDpassenger) {
for(int i=0; i<passengers.size(); i++) {
if(passengers.get(i).getID() == IDpassenger) {
wantedPassenger = i;
}
}
if(locationID>=IDlocation) {
for(int i=0; i<locations.size(); i++) {
if(locations.get(i).getID() == IDlocation) {
wantedLocation = i;
}
}
if(transportationType==3) {
if(passengers.get(wantedPassenger).hasCar()) {
passengers.get(wantedPassenger).drive(locations.get(wantedLocation));
}
}else {
if(existVehicle) {
if(transportationType==1 && vehicles.get(wantedVehicle) instanceof Bus)
passengers.get(wantedPassenger).ride(vehicles.get(wantedVehicle), locations.get(wantedLocation));
if(transportationType==2 && vehicles.get(wantedVehicle) instanceof Train)
passengers.get(wantedPassenger).ride(vehicles.get(wantedVehicle), locations.get(wantedLocation));
}
}
}
}
}
if(action==5) {
int IDpassenger = input.nextInt();
double fuelConsumption = input.nextDouble();
for(int i=0; i<passengers.size(); i++) {
if(passengers.get(i).getID() == IDpassenger) {
wantedPassenger = i;
}
}
passengers.get(wantedPassenger).purchaseCar(fuelConsumption);
}
if(action==6) {
int IDpassenger = input.nextInt();
double amountFuel = input.nextDouble();
for(int i=0; i<passengers.size(); i++) {
if(passengers.get(i).getID() == IDpassenger) {
wantedPassenger = i;
}
}
if(passengers.get(wantedPassenger).hasCar()) {
passengers.get(wantedPassenger).refuel(amountFuel);
}
}
if(action==7) {
int IDpassenger = input.nextInt();
double amountCard = input.nextDouble();
for(int i=0; i<passengers.size(); i++) {
if(passengers.get(i).getID() == IDpassenger) {
wantedPassenger = i;
}
}
passengers.get(wantedPassenger).refillCard(amountCard);
}
operations--;
}
for(int i=0; i<locations.size(); i++) {
output.print("Location "+i+": (");
output.printf("%.2f",locations.get(i).locationX);
output.printf(", %.2f)\n", locations.get(i).locationY );
Collections.sort(locations.get(i).current, new Comparator<Passenger>() {
public int compare(Passenger p1, Passenger p2) {
return Integer.valueOf(p1.getID()).compareTo(p2.getID());
}
});
for(int j=0; j<locations.get(i).current.size(); j++) {
output.print("Passenger " + locations.get(i).current.get(j).getID() + ": ");
if(locations.get(i).current.get(j).hasCar()) {
output.println(twoDecimal(locations.get(i).current.get(j).car.fuelAmount));
}else {
output.printf("%.2f\n",locations.get(i).current.get(j).cardBalance);
}
}
}
}
/**
* Shortens given number to two decimal.
* @param amount A double data type, which is wanted to shorten two decimal.
* @return A double data type.
*/
public static String twoDecimal(double amount) {
String value = Double.toString(amount);
String wanted = "";
int index = 0;
while(index != value.indexOf(".")) {
wanted += value.charAt(index);
index++;
}
for(int i=0; i<3; i++) {
wanted += value.charAt(index);
if(value.length() > index+1)
index++;
}
return wanted;
}
}
//DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE