-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vehicle.java
398 lines (321 loc) · 10.2 KB
/
Vehicle.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public abstract class Vehicle implements Comparable<Vehicle>, Cloneable {
// instance variables 추가하였습니다.
private int vehicleNum;
private int rate = 0;
private int distance = 0;
private double currentLocation;
private LocalDateTime entryTime; // 진입 시각
private LocalDateTime exitTime; // 진출 시각
private City entryLocation; // 진입 장소
private City exitLocation; // 진출 장소
// abstract method
public abstract int calcToll(); // 통행료 계산하는 함수
public abstract String showVehicleInfo(); // 차량 객체의 차량 정보(시속, 위치 등)를 String형으로 반환하는 함수
public abstract String showMovementInfo(); // 차량 객체의 통행 정보(진출 시간, 통행료 등)를 String형으로 반환하는 함수
// instance variable을 위한 get/set 메소드
public int getVehicleNum() {
return vehicleNum;
}
public double getRate() {
return rate;
}
public double getCurrentLocation() {
return currentLocation;
}
public LocalDateTime getEntryTime() {
return entryTime;
}
public City getEntryLocation() {
return entryLocation;
}
public City getExitLocation() {
return exitLocation;
}
public void setVehicleNum(int vehicleNum) {
this.vehicleNum = vehicleNum;
}
public void setRate(int rate) {
this.rate = rate;
}
public void setCurrentLocation(double currentLocation) {
this.currentLocation = currentLocation;
}
public void setEntryTime(LocalDateTime entryTime) {
this.entryTime = entryTime;
}
public void setEntryLocation(City entryLocation) {
this.entryLocation = entryLocation;
}
public void setExitLocation(City exitLocation) {
this.exitLocation = exitLocation;
}
// 진입도시와 진출도시 사이의 거리를 계산하는 함수
public int calcDistance() {
distance = this.getExitLocation().getDistanceFromSeoul() - this.getEntryLocation().getDistanceFromSeoul();
if (distance < 0)
distance = (-1) * distance;
return distance;
}
// 진입도시에서 진출도시까지 가는데 걸리는 시간을 계산하여 진출 시각을 반환하는 함수
public LocalDateTime calcExitTime() {
int movingDistance = calcDistance();
int t = (int) (((double) movingDistance / this.rate) * 60);// 진출하기까지 걸리는 분
int hour = t / 60;
int minute = t % 60;
exitTime = getEntryTime().plusHours(hour).plusMinutes(minute);
return exitTime;
}
@Override
// Vehicle 차량을 차량 종류와 진입 순서 기준으로 정렬하는 함수
public int compareTo(Vehicle other) {
// 차량 종류를 기준으로 정렬합니다.
int thisOrder = getVehicleOrder(this);
int otherOrder = getVehicleOrder(other);
int typeComparison = Integer.compare(thisOrder, otherOrder);
if (typeComparison != 0) {
return typeComparison;
}
// 진입 순서를 기준으로 정렬합니다.
return this.getEntryTime().compareTo(other.getEntryTime());
}
// 차량 종류에 따른 순서를 반환하는 함수
private int getVehicleOrder(Vehicle vehicle) {
// 차량 종류에 따라 순서를 반환합니다.
if (vehicle instanceof Car) {
if (vehicle instanceof HybridCar) {
return 2;
}
return 1;
} else if (vehicle instanceof Bus) {
return 3;
} else if (vehicle instanceof Truck) {
return 4;
}
return 0; // 정의되지 않은 경우 0을 반환합니다.
}
// 해당 Vehicle 객체를 복제하는 함수
@Override
public Vehicle clone() throws CloneNotSupportedException {
return (Vehicle) super.clone();
}
}
class Car extends Vehicle {
private int volume; // 차량 배기량
private double volumeRate; // 배기량 요율
private static int basicPrice; // 기본 요금
private static int distanceRate; // 거리 요율
public Car(int carNum, int volume) {
this.setVehicleNum(carNum);
this.volume = volume;
volumeRate = calcVolumeRate(volume);
}
// instance variable을 위한 get/set 메소드
public int getVolume() {
return volume;
}
public double getVolumRate() {
return volumeRate;
}
public int getBasicCharge() {
return basicPrice;
}
public int getDistanceRate() {
return distanceRate;
}
public static void setDistanceRate(int distanceRate) {
Car.distanceRate = distanceRate;
}
public static void setBasicCharge(int basicPrice) {
Car.basicPrice = basicPrice;
}
// 차량 배기량에 따른 배기량 요율을 반환하는 함수
public double calcVolumeRate(int volume) {
if (volume >= 2400) {
volumeRate = 1.2;
return volumeRate;
} else if (volume > 1000) {
volumeRate = 1.0;
return volumeRate;
}
else {
volumeRate = 0.8;
return volumeRate;
}
}
@Override
public int calcToll() {
calcDistance();
return (int) (basicPrice + (calcDistance() * distanceRate * getVolumRate()));
}
@Override
public String toString() {
return "car ";
}
@Override
public String showVehicleInfo() {
String entryTime = this.getEntryTime().format(DateTimeFormatter.ofPattern("yyyy/MM/dd:HH:mm"));
return this.toString() + this.getVehicleNum() + " " + this.getVolume() + "cc " + entryTime + " "
+ this.getEntryLocation() + "->" + this.getExitLocation() + " 시속:" + (int) this.getRate() + "km 위치:"
+ (int) this.getCurrentLocation() + "km";
}
@Override
public String showMovementInfo() {
String entryTime = this.getEntryTime().format(DateTimeFormatter.ofPattern("yyyy/MM/dd:HH:mm"));
String exitTime = this.calcExitTime().format(DateTimeFormatter.ofPattern("yyyy/MM/dd:HH:mm"));
return this.toString() + this.getVehicleNum() + " " + this.getVolume() + "cc " + entryTime + " "
+ this.getEntryLocation() + "->" + this.getExitLocation() + " " + exitTime + " " + this.calcToll()
+ "원";
}
}
class HybridCar extends Car {
public HybridCar(int carNum, int volume) {
super(carNum, volume);
}
@Override
public int calcToll() {
return (int) (super.calcToll() / 2);
}
@Override
public String toString() {
return "hybrid car ";
}
}
class Bus extends Vehicle {
private static int basicPrice; // 기본 요금
private static int distanceRate; // 거리 요율
private int noOfPassengers; // 승객수
private double passengerRate; // 승객 요율
public Bus(int carNum, int p) {
this.setVehicleNum(carNum);
noOfPassengers = p;
passengerRate = this.getPassengerRate();
}
// instance variable을 위한 get/set 메소드
public int getBasicCharge() {
return basicPrice;
}
public int getDistanceRate() {
return distanceRate;
}
public double getPassengerRate() {
return passengerRate;
}
public static int getBasicPrice() {
return basicPrice;
}
public int getNoOfPassengers() {
return noOfPassengers;
}
public static void setDistanceRate(int distanceRate) {
Bus.distanceRate = distanceRate;
}
public static void setBasicCharge(int basicPrice) {
Bus.basicPrice = basicPrice;
}
// 승객수에 따른 승객요율을 반환하는 함수
public double calcPassengerRate(int noOfPassengers) {
if (noOfPassengers >= 40) {
passengerRate = 1.2;
return passengerRate;
} else if (noOfPassengers >= 20) {
passengerRate = 1.0;
return passengerRate;
}
else {
passengerRate = 0.8;
return passengerRate;
}
}
@Override
public int calcToll() {
calcDistance();
return (int) (basicPrice + (calcDistance() * distanceRate * calcPassengerRate(noOfPassengers)));
}
@Override
public String toString() {
return "bus ";
}
@Override
public String showMovementInfo() {
String entryTime = this.getEntryTime().format(DateTimeFormatter.ofPattern("yyyy/MM/dd:HH:mm"));
String exitTime = this.calcExitTime().format(DateTimeFormatter.ofPattern("yyyy/MM/dd:HH:mm"));
return this.toString() + this.getVehicleNum() + " " + this.getNoOfPassengers() + "인승 " + entryTime + " "
+ this.getEntryLocation() + "->" + this.getExitLocation() + " " + exitTime + " " + this.calcToll()
+ "원";
}
@Override
public String showVehicleInfo() {
String entryTime = this.getEntryTime().format(DateTimeFormatter.ofPattern("yyyy/MM/dd:HH:mm"));
return this.toString() + this.getVehicleNum() + " " + this.getNoOfPassengers() + "인승 " + entryTime + " "
+ this.getEntryLocation() + "->" + this.getExitLocation() + " 시속:" + (int) this.getRate() + "km 위치:"
+ (int) this.getCurrentLocation() + "km";
}
}
class Truck extends Vehicle {
private static int basicPrice;
private static int distanceRate;
private int weight;
private double weightRate;
public Truck(int carNum, int weight) {
this.setVehicleNum(carNum);
weightRate = this.calcWeightRate();
}
// instance variable을 위한 get/set 메소드
public static int getBasicPrice() {
return basicPrice;
}
public int getWeight() {
return weight;
}
public double getWeightRate() {
return weightRate;
}
public int getBasicCharge() {
return basicPrice;
}
public int getDistanceRate() {
return distanceRate;
}
public static void setDistanceRate(int distanceRate) {
Truck.distanceRate = distanceRate;
}
public static void setBasicCharge(int basicPrice) {
Truck.basicPrice = basicPrice;
}
// 중량에 따른 중량 요율을 반환하는 함수
public double calcWeightRate() {
if (weight >= 4) {
return 1.2;
} else if (weight >= 2) {
return 1;
} else {
return 0.8;
}
}
@Override
public int calcToll() {
calcDistance();
return (int) (basicPrice + (calcDistance() * distanceRate * weightRate));
}
@Override
public String toString() {
return "truck ";
}
@Override
public String showVehicleInfo() {
String entryTime = this.getEntryTime().format(DateTimeFormatter.ofPattern("yyyy/MM/dd:HH:mm"));
return this.toString() + this.getVehicleNum() + " " + this.getWeight() + "ton " + entryTime + " "
+ this.getEntryLocation() + "->" + this.getExitLocation() + " 시속:" + (int) this.getRate() + "km 위치:"
+ this.getCurrentLocation() + "km";
}
@Override
public String showMovementInfo() {
String entryTime = this.getEntryTime().format(DateTimeFormatter.ofPattern("yyyy/MM/dd:HH:mm"));
String exitTime = this.calcExitTime().format(DateTimeFormatter.ofPattern("yyyy/MM/dd:HH:mm"));
return this.toString() + this.getVehicleNum() + " " + this.getWeight() + "ton " + entryTime + " "
+ this.getEntryLocation() + "->" + this.getExitLocation() + " " + exitTime + " " + this.calcToll()
+ "원";
}
}