forked from ChathuminaVimukthi/Object-Oriented-Programing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWestminsterCarParkManger.java
More file actions
367 lines (308 loc) · 11.4 KB
/
WestminsterCarParkManger.java
File metadata and controls
367 lines (308 loc) · 11.4 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
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
import java.io.*;
import java.util.*;
import vehicle.park.MotorBike;
import vehicle.park.Van;
import vehicle.park.Car;
import vehicle.park.Vehicle;
public class WestminsterCarParkManger implements CarParkManager {
//vehicle arraylist to hold vehicle objects
static List<Vehicle> vehicle = new ArrayList<Vehicle>();
static int parkingSlots = 0;
static int carCount =0;
static int vanCount =0;
static int bikeCount =0;
static int numberOfVehicles =0;
static int hours=0;
static int parkingSlot = 0;
static String vehicleType = null;
static String vehicleId = null;
static String vehicleBrand = null;
static String date=null;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws IOException{
//creating a new westminstercarpark object
WestminsterCarParkManger carpark = new WestminsterCarParkManger();
String menu = null;
do{
System.out.println();
//menu to select functions
System.out.println("************************************************************************************************");
System.out.println("* Welcome to WESTMINSTER car park manager *");
System.out.println("************************************************************************************************");
System.out.println("* A : Press A to checkin the Vehicles *");
System.out.println("* D : Press D to checkout the vehicles *");
System.out.println("* V : Press V to get the current list of the vehicles in the park *");
System.out.println("* R : Press R to get the current list of the vehicles in the park according to the date *");
System.out.println("* P : Press P to get the percentage of each vehicle type *");
System.out.println("* I : Press I to get the list of the vehicles ordered Chronologically *");
System.out.println("* L : Press L to get the vehicles for the longest parked time and newest vehicle *");
System.out.println("* B : Press B to get the BILL for each vehicle in the parking lot *");
System.out.println("************************************************************************************************");
menu = sc.next().toUpperCase();
switch (menu) {
case "A": {
carpark.addVehicle();
break;
}
case "D": {
carpark.remove();
break;
}
case "V": {
carpark.view();
break;
}
case "R": {
carpark.specifiedDate();
break;
}
case "B": {
carpark.calculateBill();
break;
}
case "P": {
carpark.vehiclePercentage();
break;
}
case "L": {
carpark.longestParkedVehicle();
break;
}
case "I": {
chronological();
break;
}
case "X": {
System.out.println("Exit");
System.exit(0);
break;
}
default :{
System.out.println("Please Enter a valid Input...");
break;
}
}
}while(menu != "X");
}
//view method to show the vehicles currently parked
public void view(){
for(int i=0;i<vehicle.size();i++){
System.out.println(vehicle.get(i).getVehicleId());
}
}
// add vehicle method to add vehicles to the parking lot
public void addVehicle(){
//declaring the variables to take inputs
String carColor = null;
int numberofDoors = 0;
double cargoVolume = 0.0;
double engineSize = 0.0;
int day =0;
int month =0;
int year =0;
String str[];
try{
//receiving user input through the scanner
System.out.println("Enter the Vehicle Plate Number :");
vehicleId = sc.next().toUpperCase();
System.out.println("Eneter the brand of the Vehicle :");
vehicleBrand = sc.next().toUpperCase();
System.out.println("Enter the Parking Slot Number :");
parkingSlot = sc.nextInt();
System.out.println("Enter the Vehicle type :");
vehicleType = sc.next().toUpperCase();
System.out.println("Enter the Date in the format DD.MM.YYYY :");
date = sc.next();
//separating date month using splitter function
str = date.split("\\.");
day = Integer.parseInt(str[0]);
if(day<32 && day>0){
month = Integer.parseInt(str[1]);
if(month>0 && month<13){
year = Integer.parseInt(str[2]);
}else{
System.out.println("Invalid Month");
}
}else{
System.out.println("Invalid Date");
}
System.out.println("Enter the time in hours the vehicle checks in :");
hours = sc.nextInt();
//if parking lots are available adding relevant vehicle to the slot available
if(vehicleType.equalsIgnoreCase("CAR") && parkingSlots<21){
System.out.println("Enter the color of the CAR :");
carColor = sc.next().toUpperCase();
System.out.println("Eneter the NUmber of Doors in the CAR :");
numberofDoors = sc.nextInt();
Car car = new Car(vehicleId,vehicleBrand,vehicleType,year,month,day,hours,numberofDoors,carColor);
vehicle.add(car);
parkingSlots = parkingSlots +1;
carCount++;
numberOfVehicles++;
}
if(vehicleType.equalsIgnoreCase("VAN") && parkingSlots<21){
System.out.println("Enter the Cargo Volume of the VAN :");
cargoVolume = sc.nextDouble();
Van van = new Van(vehicleId,vehicleBrand,vehicleType,year,month,day,hours,cargoVolume);
parkingSlots = parkingSlots +2;
vehicle.add(van);
vanCount++;
numberOfVehicles++;
}
if(vehicleType.equalsIgnoreCase("MOTORBIKE") && parkingSlots<21){
System.out.println("Enter the Engine Capacity of the MotorBike :");
engineSize = sc.nextDouble();
MotorBike bike = new MotorBike(vehicleId,vehicleBrand,vehicleType,year,month,day,hours,engineSize);
parkingSlots = parkingSlots +1;
vehicle.add(bike);
bikeCount++;
numberOfVehicles++;
}
}catch(Exception e){
System.out.println("Invalid Input..Please enter a valid Input...");
}
//writting output to file
try {
File carPark = new File(date);//file name is set as date
PrintWriter printWriter = new PrintWriter(carPark);
for (int j = 0; j < vehicle.size(); j++) {
if (vehicle.get(j).getVehicleId() != null) {
printWriter.println(vehicle.get(j).getVehicleId());
} else {
printWriter.println(vehicle.get(j).getVehicleId());
}
}
System.out.println("Succesfully added");
printWriter.close();
}
catch (IOException e) {
System.out.println("Check your file.");
}
}
//remove method to remove a vehicle from the parkinglot
public void remove(){
try{
System.out.println("Enter the vehicle PLATE NUMBER that is leaving :");
vehicleId = sc.next().toUpperCase();
System.out.println("Enter the type of the vehicle :");
vehicleType = sc.next().toUpperCase();
System.out.println("Enter the Parking Slot Number :");
parkingSlot = sc.nextInt();
for(int i=0;i<vehicle.size();i++){
if(vehicle.get(i).getVehicleId().equalsIgnoreCase(vehicleId) && vehicle.get(i).getVehicleType().equalsIgnoreCase(vehicleType)){
System.out.println("Details for vehicle "+vehicle.get(i).getVehicleId()+" deleted..");
if(vehicleType.equalsIgnoreCase("CAR") || vehicleType.equalsIgnoreCase("MOTORBIKE")){
parkingSlots = parkingSlots - 1;
}
if(vehicleType.equalsIgnoreCase("VAN")){
parkingSlot = parkingSlot - 2;
}
vehicle.remove(i);
numberOfVehicles--;
}
}
}catch(Exception e){
System.out.println("Invalid Input..Please enter a valid Input...");
}
}
//method to view the parked vehicles in chronological order
public static void chronological(){
try{
System.out.println("List of the vehicles ordered Chronologically : ");
int index =vehicle.size()-1;
for(int i=0;i<vehicle.size();i++){
System.out.println(vehicle.get(index).getVehicleType()+" "+vehicle.get(index).getVehicleId()+" "+vehicle.get(index).getHours()+"h");
index--;
}
}catch(Exception e){
System.out.println("No vehicles in the parkinglot...");
}
}
//method to view the percentages of vehicles type currently in the parkinglot
public void vehiclePercentage(){
double carPercentage=0;
double vanPercentage=0;
double bikePercentage=0;
try{
for(int i=0;i<vehicle.size();i++){
if(vehicle.get(i).getVehicleType().equalsIgnoreCase("CAR")){
carPercentage = (carCount*100)/numberOfVehicles;
}
if(vehicle.get(i).getVehicleType().equalsIgnoreCase("MOTORBIKE")){
bikePercentage = (bikeCount*100)/numberOfVehicles;
}
if(vehicle.get(i).getVehicleType().equalsIgnoreCase("VAN")){
vanPercentage = (vanCount*100)/numberOfVehicles;
}
}
System.out.println("Percentage of the CARS in the parking is : "+carPercentage+"%");
System.out.println("Percentage of the VANS in the parking is : "+vanPercentage+"%");
System.out.println("Percentage of the BIKES in the parking is : "+bikePercentage+"%");
}catch(Exception e){
System.out.println("No vehicles in the Parkinglot...");
}
}
//method to view the vehicle that was parked for the longest time and last parked vehicle
public void longestParkedVehicle(){
if(vehicle.size()!=0){
System.out.println("The vehicle that is parked for the longest time :");
System.out.println("Vehicle plate Number:"+ vehicle.get(0).getVehicleId()+"\nVehicle Type:"+vehicle.get(0).getVehicleType()+"\nVehicle Entry Time:"+vehicle.get(0).getHours()+" h");
int i = vehicle.size()-1;
System.out.println();
System.out.println("The last vehicle that was parked :");
System.out.println("Vehicle plate Number:"+ vehicle.get(i).getVehicleId()+"\nVehicle Type:"+vehicle.get(i).getVehicleType()+"\nVehicle Entry Time:"+vehicle.get(i).getHours()+" h");
}else{
System.out.println("No Vehicles in the parking lot...");
}
}
//method to view the vehicles that were parked in a specified day
public void specifiedDate() throws IOException{
try{
System.out.println("Enter the date to view the vehicles in the park in the specified date in the format DD.MM.YYYYY :");
date = sc.next();
BufferedReader firstReader = new BufferedReader(new FileReader(date));
String sCurrentLine;
while((sCurrentLine = firstReader.readLine()) != null){
System.out.println(sCurrentLine);
}
firstReader.close();
}catch(Exception e){
System.out.println("No vehicle details found in the specified date...");
}
}
//method to calculate the ticket for the leaving vehicles
public void calculateBill(){
System.out.println("Enter the current time in the mode of hours only :");
int hours = sc.nextInt();
int checkedInTime=0;
int timeDifference=0;
int timeDifference2=0;
for(int i=0;i<vehicle.size();i++){
checkedInTime = vehicle.get(i).getHours();
timeDifference = hours - checkedInTime;
if(timeDifference <= 3 && timeDifference > 0){
System.out.println(vehicle.get(i).getVehicleId()+" "+timeDifference*3+"$");
}else if(timeDifference > 3 && timeDifference >0){
timeDifference2 = (timeDifference-3)+9;
System.out.println(vehicle.get(i).getVehicleId()+" "+timeDifference2+"$");
}else if(timeDifference == 0 ){
System.out.println(vehicle.get(i).getVehicleId()+" "+30+"$");
}
}
}
public boolean validation(int input,String str){
boolean validity = true;
do{
switch(str){
case"A":{
if(str.matches("\\d{2}.\\d{2}.\\d{4}")){
validity = true;
}else{
System.out.println("Invalid input...");
}
}
}
}while(validity!=true);
return validity;
}
}