-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParkingManagementSystem.java
More file actions
227 lines (195 loc) · 8.77 KB
/
Copy pathParkingManagementSystem.java
File metadata and controls
227 lines (195 loc) · 8.77 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
import java.util.*;
public class ParkingManagementSystem {
private static final List<String> bikeSpots = new ArrayList<>(Arrays.asList("B-1", "B-2", "B-3"));
private static final List<String> carSpots = new ArrayList<>(Arrays.asList("C-1", "C-2", "C-3", "C-4"));
private static final List<String> truckSpots = new ArrayList<>(Arrays.asList("T-1", "T-2"));
private static final Map<String, Booking> bookings = new HashMap<>();
private static double totalEarnings = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean isRunning = true;
while (isRunning) {
System.out.println("\nWelcome to Smart Parking Management System");
System.out.println("------------------------------------------");
System.out.println("1. Park Vehicle");
System.out.println("2. Vacate Spot");
System.out.println("3. Display Available Spots");
System.out.println("4. Search for a Booking");
System.out.println("5. Administrator Access");
System.out.println("6. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1 -> parkVehicle(scanner);
case 2 -> vacateSpot(scanner);
case 3 -> displayAvailableSpots();
case 4 -> searchBooking(scanner);
case 5 -> administratorAccess(scanner);
case 6 -> {
System.out.println("Thank you for using the Parking Management System!");
isRunning = false;
}
default -> System.out.println("Invalid option! Please try again.");
}
}
scanner.close();
}
private static void parkVehicle(Scanner scanner) {
System.out.println("\nParking Vehicle...");
System.out.print("Enter vehicle type (Car/Bike/Truck): ");
String vehicleType = scanner.nextLine().toLowerCase();
String allocatedSpot = null;
switch (vehicleType) {
case "bike" -> allocatedSpot = allocateSpot(bikeSpots);
case "car" -> allocatedSpot = allocateSpot(carSpots);
case "truck" -> allocatedSpot = allocateSpot(truckSpots);
default -> {
System.out.println("Invalid vehicle type! Please try again.");
return;
}
}
if (allocatedSpot == null) {
System.out.println("No available spots for " + vehicleType + ".");
return;
}
System.out.println("Allocated Spot: " + allocatedSpot);
System.out.print("Enter vehicle number: ");
String vehicleNumber = scanner.nextLine();
System.out.print("Enter owner name: ");
String ownerName = scanner.nextLine();
bookings.put(allocatedSpot, new Booking(vehicleType, vehicleNumber, ownerName));
System.out.println("Vehicle parked successfully at spot: " + allocatedSpot);
}
private static void vacateSpot(Scanner scanner) {
System.out.print("\nEnter spot number to vacate: ");
String spot = scanner.nextLine();
if (bookings.containsKey(spot)) {
Booking booking = bookings.remove(spot);
// Free the spot
if (spot.startsWith("B")) {
bikeSpots.add(spot);
} else if (spot.startsWith("C")) {
carSpots.add(spot);
} else if (spot.startsWith("T")) {
truckSpots.add(spot);
}
double charge = calculateCharge(booking.vehicleType);
totalEarnings += charge;
System.out.println("Spot vacated successfully. Vehicle: " + booking.vehicleNumber);
System.out.println("Parking charge: $" + charge);
} else {
System.out.println("Invalid spot number or the spot is already empty.");
}
}
private static void displayAvailableSpots() {
System.out.println("\nAvailable Parking Spots:");
System.out.println("Bike Spots: " + bikeSpots);
System.out.println("Car Spots: " + carSpots);
System.out.println("Truck Spots: " + truckSpots);
}
private static void searchBooking(Scanner scanner) {
System.out.print("\nEnter vehicle number to search: ");
String vehicleNumber = scanner.nextLine();
for (Map.Entry<String, Booking> entry : bookings.entrySet()) {
if (entry.getValue().vehicleNumber.equalsIgnoreCase(vehicleNumber)) {
System.out.println("Booking Found:");
System.out.println("Spot: " + entry.getKey() + ", Vehicle Type: " + entry.getValue().vehicleType +
", Owner: " + entry.getValue().ownerName);
return;
}
}
System.out.println("No booking found for vehicle number: " + vehicleNumber);
}
private static void administratorAccess(Scanner scanner) {
final String adminPassword = "admin123";
System.out.print("\nEnter Administrator Password: ");
String enteredPassword = scanner.nextLine();
if (!enteredPassword.equals(adminPassword)) {
System.out.println("Invalid password! Access denied.");
return;
}
boolean adminSessionActive = true;
while (adminSessionActive) {
System.out.println("\nAdministrator Access Granted:");
System.out.println("1. View Total Earnings");
System.out.println("2. Monitor Real-Time Occupancy");
System.out.println("3. Add/Remove Parking Spots");
System.out.println("4. Exit Administration");
System.out.print("Choose an option: ");
int adminChoice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (adminChoice) {
case 1 -> System.out.println("Total Earnings: $" + totalEarnings);
case 2 -> {
int occupied = bookings.size();
int totalSpots = bikeSpots.size() + carSpots.size() + truckSpots.size() + occupied;
System.out.println("Real-Time Occupancy: " + occupied + "/" + totalSpots);
}
case 3 -> modifyParkingSpots(scanner);
case 4 -> {
System.out.println("Exiting Administrator Access...");
adminSessionActive = false;
}
default -> System.out.println("Invalid option! Please try again.");
}
}
}
private static void modifyParkingSpots(Scanner scanner) {
System.out.println("\nModify Parking Spots:");
System.out.println("1. Add Spots");
System.out.println("2. Remove Spots");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1 -> {
System.out.print("Enter spot type (Bike/Car/Truck): ");
String type = scanner.nextLine().toLowerCase();
System.out.print("Enter spot name: ");
String spotName = scanner.nextLine();
if (type.equals("bike")) {
bikeSpots.add(spotName);
} else if (type.equals("car")) {
carSpots.add(spotName);
} else if (type.equals("truck")) {
truckSpots.add(spotName);
} else {
System.out.println("Invalid type! No spots added.");
}
}
case 2 -> {
System.out.print("Enter spot name to remove: ");
String spotName = scanner.nextLine();
bikeSpots.remove(spotName);
carSpots.remove(spotName);
truckSpots.remove(spotName);
}
default -> System.out.println("Invalid option! Returning to menu.");
}
}
private static String allocateSpot(List<String> spots) {
return spots.isEmpty() ? null : spots.remove(0);
}
private static double calculateCharge(String vehicleType) {
return switch (vehicleType) {
case "bike" -> 5.0;
case "car" -> 10.0;
case "truck" -> 20.0;
default -> 0.0;
};
}
private static int countOccupiedSpots(List<String> spots) {
return spots.size();
}
private static class Booking {
String vehicleType;
String vehicleNumber;
String ownerName;
Booking(String vehicleType, String vehicleNumber, String ownerName) {
this.vehicleType = vehicleType;
this.vehicleNumber = vehicleNumber;
this.ownerName = ownerName;
}
}
}