0% found this document useful (0 votes)
15 views6 pages

Java Parking System Program

Uploaded by

Dhanush A
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Java Parking System Program

Uploaded by

Dhanush A
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAM:

import [Link];
import [Link];

public class ParkingSystem {

static int totalSlots, availableSlots;


static ArrayList<String> parkedCars = new ArrayList<String>();

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter the total number of parking slots:");
totalSlots = [Link]();
availableSlots = totalSlots;

while (true) {
[Link]("\nWhat would you like to do?");
[Link]("1. Park a car");
[Link]("2. Remove a car");
[Link]("3. View parked cars");
[Link]("4. Exit");
int choice = [Link]();

switch (choice) {
case 1:
parkCar();
break;
case 2:
removeCar();
break;
case 3:
viewParkedCars();
break;
case 4:
[Link](0);
default:
[Link]("Invalid choice. Please try again.");
}
}
}

public static void parkCar() {


if (availableSlots == 0) {
[Link]("Sorry, there are no available parking slots.");
return;
}

Scanner sc = new Scanner([Link]);


[Link]("Enter the license plate number of the car:");
String licensePlate = [Link]();
[Link](licensePlate);
availableSlots--;
[Link]("Car parked successfully. Available slots: " + availableSlots);
}

public static void removeCar() {


if (availableSlots == totalSlots) {
[Link]("There are no parked cars.");
return;
}

Scanner sc = new Scanner([Link]);


[Link]("Enter the license plate number of the car to be removed:");
String licensePlate = [Link]();
if ([Link](licensePlate)) {
[Link](licensePlate);
availableSlots++;
[Link]("Car removed successfully. Available slots: " +
availableSlots);
} else {
[Link]("The car is not parked here.");
}
}

public static void viewParkedCars() {


if (availableSlots == totalSlots) {
[Link]("There are no parked cars.");
return;
}

[Link]("Parked cars:");
for (String licensePlate : parkedCars) {
[Link](licensePlate);
}
}
}
OUTPUT:
Enter the total number of parking slots:
45

What would you like to do?


1. Park a car
2. Remove a car
3. View parked cars
4. Exit
1
Enter the license plate number of the car:
1112
Car parked successfully. Available slots: 44

What would you like to do?


1. Park a car
2. Remove a car
3. View parked cars
4. Exit
1
Enter the license plate number of the car:
1011
Car parked successfully. Available slots: 43

What would you like to do?


1. Park a car
2. Remove a car
3. View parked cars
4. Exit
3
Parked cars:
1112
1011

What would you like to do?


1. Park a car
2. Remove a car
3. View parked cars
4. Exit
2
Enter the license plate number of the car to be removed:
1555
The car is not parked here.

What would you like to do?


1. Park a car
2. Remove a car
3. View parked cars
4. Exit
2
Enter the license plate number of the car to be removed:
1011
Car removed successfully. Available slots: 44

What would you like to do?


1. Park a car
2. Remove a car
3. View parked cars
4. Exit
2
Enter the license plate number of the car to be removed:
1112
Car removed successfully. Available slots: 45
What would you like to do?
1. Park a car
2. Remove a car
3. View parked cars
4. Exit
3
There are no parked cars.

What would you like to do?


1. Park a car
2. Remove a car
3. View parked cars
4. Exit
2
There are no parked cars.

What would you like to do?


1. Park a car
2. Remove a car
3. View parked cars
4. Exit
4

Common questions

Powered by AI

The ParkingSystem program provides basic error handling by checking conditions such as available slots before attempting to park a car and confirming the presence of a car before removal. However, it lacks robust input validation, such as handling non-integer inputs or negative numbers when setting the total number of slots. Additionally, the program does not handle exceptions such as input/output issues, which could be improved by implementing try-catch blocks and more detailed user feedback .

The usability of the ParkingSystem program can be enhanced by implementing features such as input validation to prevent invalid entries like negative numbers for slots. Additionally, the user interface could be improved with a more intuitive menu that guides users more effectively. Integrating a database could help persist data across sessions. The program's scalability can be increased by handling large datasets more efficiently, perhaps by using multithreading for concurrent user access .

Using static methods in the ParkingSystem class provides simplicity, enabling easy access to shared class-level data such as totalSlots and availableSlots without instantiating objects. However, this approach limits flexibility and testability, as static methods cannot be overridden in subclasses, which could impede extending functionality. Furthermore, static methods couple logic tightly to specific implementations, potentially leading to issues in maintaining and scaling the system in object-oriented designs .

The current ParkingSystem program does not support concurrent actions by multiple users, as it is designed for single-threaded execution where user input is processed sequentially. To accommodate concurrency, the program could be enhanced with synchronization mechanisms such as locks to manage access to shared resources like the list of parked cars and available slots. Implementing multithreading would allow the system to handle multiple requests simultaneously, improving throughput and responsiveness for multiple users .

The ParkingSystem class addresses some edge cases, such as preventing parking when no slots are available and removing cars only if they are currently parked. However, the system might not handle scenarios like duplicate license plate entries effectively or handle large volumes of simultaneous entries, which could lead to inconsistencies. Improving this could involve implementing checks for duplicate entries and developing more robust data structures that support higher efficiency and concurrency without sacrificing data integrity .

The state management of totalSlots and availableSlots ensures data consistency by only updating available slots when a car is successfully parked or removed, keeping these values congruent with the current state of parking occupancy. However, improvements could include implementing encapsulation for these fields to safeguard against unintended modifications. Additionally, using atomic operations for updating these slots could enhance safety in a concurrent processing environment, ensuring threads do not interfere with each other's operations .

The current ParkingSystem implementation is not highly scalable due to its reliance on a single ArrayList for managing parked cars and handling all operations sequentially. As the number of parking slots and cars increases, performance may degrade due to linear search operations required for adding and removing cars. To improve scalability, the use of more sophisticated data structures, indexing, and possibly a database backend could be considered. This would allow the system to efficiently manage larger data sets and support more robust operations .

The ParkingSystem class uses static variables to manage the state of the parking system, such as the total number of parking slots and available slots. It maintains a list of parked cars using an ArrayList, allowing dynamic addition and removal of car entries. The class contains methods like parkCar, removeCar, and viewParkedCars, which implement the core functionalities of the parking system. Through these methods, the class handles user input and updates the state of parked cars and available slots accordingly, enabling efficient management of parking slots .

The ParkingSystem program's user interface is text-based and relies on console input, which may not be intuitive for all users. To improve user experience, the program could be upgraded with a graphical user interface (GUI) that visually represents available and occupied parking slots. Interactive components like buttons and input fields can make interactions more user-friendly. Additionally, incorporating visual feedback, such as color-coded slot indicators, can further enhance user interactions and engagement .

The ParkingSystem program uses an ArrayList to store parked car license numbers, which is efficient for dynamic list operations like adding and removing cars. However, it might not be optimal for frequent membership checks during car removal. Alternatives such as a HashSet could provide faster lookup times when checking if a car is parked. This change would trade off the order of insertion preserved by ArrayList for the performance benefits of constant-time complexity for add, remove, and contains operations in a HashSet .

You might also like