How to build a simple parking system in Java
Java can help you create a simple parking system that you can then design and integrate with the database, add authentication, develop a GUI to implement in the real world. Here are detailed instructions.
Java can help you create a simple parking system that you can then design and integrate with the database, add authentication, develop a GUI to implement in the real world. Here are detailed instructions.
How to build a parking system in Java
The example in the article uses ArrayList - a common and customizable array of sizes. You can access elements with an index, implement CRUD operations, and much more. To use ArrayList, you need to import the ArrayList class from the Java standard library. Similarly, for the input-input operation, enter the Scanner class. Define a public class, VehiceParkingSystem as the main class.
Define 3 static variables: totalSlots , availableSlots , and parkedCars . totalSlots is the total number of parking slots, availableSlots keeps track of the number of spaces left. parkedCars is an ArrayList that contains the license plates of currently parked cars.
Define the main() function and create an object of the Scanner class. Ask the user to enter the total number of parking spaces & save it in totalSlots . Initially, available slots are equal to total parking spaces so initialize availableSlots to the same value as totalSlots .
import java.util.ArrayList; import java.util.Scanner; public class VehicleParkingSystem { static int totalSlots, availableSlots; static ArrayList parkedCars = new ArrayList(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the total number of parking slots in the facility:"); totalSlots = sc.nextInt(); availableSlots = totalSlots;
Use while loop to run indefinitely. Ask the user if he wants to park the car, remove it, see if the car is parked, or exit the program. Depending on the selection, call the corresponding method. If the user wants to end this program, use System.exit(0) to terminate immediately.
while (true) { System.out.println("n What would you like to do?"); System.out.println("1. Park a car"); System.out.println("2. Remove a car"); System.out.println("3. View parked cars"); System.out.println("4. Exit"); int choice = sc.nextInt(); switch (choice) { case 1: parkCar(); break; case 2: removeCar(); break; case 3: viewParkedCars(); break; case 4: System.exit(0); default: System.out.println("Invalid choice. Please try again."); } } }
The public static method definition, parkCar() , has no return type. If the parking location is not available, notify the user and return. If not, ask the user to enter the license plate and use the add() function to insert it into the ArrayList. Reduce the number of available slots to 1 and display the parking program and the number of available slots.
public static void parkCar() { if (availableSlots == 0) { System.out.println("Sorry, there are no available parking slots."); return; } Scanner sc = new Scanner(System.in); System.out.println("Enter the license plate number of the car:"); String licensePlate = sc.nextLine(); parkedCars.add(licensePlate); availableSlots--; System.out.println("Car is parked successfully. The current available slots are: " + availableSlots); }
Define a function, removeCar() . If the vacancies value and the total number of slots match, there is no parking available and go back. If not, ask the user for the license plate number. Check the number plate the user entered in the ArrayList with contains() .
If successful, use the remove() method to remove that data from the ArrayList and increment the number of available parking slots. It is now announced that this program has removed that vehicle along with the current number of vacancies. In case you can't find the license plate, it means that the car is not currently parked in the garage.
public static void removeCar() { if (availableSlots == totalSlots) { System.out.println("There are no parked cars."); return; } Scanner sc = new Scanner(System.in); System.out.println("Enter the license plate number of the car to be removed:"); String licensePlate = sc.nextLine(); if (parkedCars.contains(licensePlate)) { parkedCars.remove(licensePlate); availableSlots++; System.out.println("Car removed successfully. The current available slots are: " + availableSlots); } else { System.out.println("The car is not parked here."); } }
Define the method, viewParkedCars() . Same as above, check to see if the parking space is full. If not, show parked license plates. To do this, use lopp for-each to iterate over the items in the ArrayList and display each number plate in turn.
public static void viewParkedCars() { if (availableSlots == totalSlots) { System.out.println("There are no parked cars."); return; } System.out.println("Parked cars:"); for (String licensePlate : parkedCars) { System.out.println(licensePlate); } } }
A simple parking system management program is ready for you to use.
And this is the result:
Good luck!
You should read it
- Download Java Development Kit 8-update-281
- What is Java? Why choose Java?
- Basic Java exercises, with sample decoding
- How to Download a Java Development Kit to Program on Android
- Which career Java programming options are waiting for you?
- How to Install the JDK (Java Development Kit) on a Mac
- Test on JAVA test P5
- eQuiz - Multiple choice quiz about JAVA
- How to Compile a Java Program
- JAVA test on P6
- Test on JAVA test on P9
- 9 things Java programmers should know in 2018 if they want a successful career
Maybe you are interested
The strange forest area looks like a chessboard when viewed from above Impressive image: ISS International Space Station flies over the Sun. Russian spacecraft 'resupply' successfully 3 tons of cargo for the ISS in just 3 hours How to Become a Costume Designer How to Make a Costume How to Make a Halloween Costume