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.

How to build a simple parking system in Java Picture 1

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:

How to build a simple parking system in Java Picture 2

Good luck!

4 ★ | 1 Vote

May be interested

  • How to Report Illegal ParkingHow to Report Illegal Parking
    in large metropolitan areas and small towns across the us, parking regulations ensure that neighborhoods are safe and livable. an illegally parked car can interfere with emergency responders as well as disrupt the smooth flow of traffic....
  • Google Maps provides a parking search featureGoogle Maps provides a parking search feature
    recently, google maps has introduced a tool to help users find parking. anyone who goes inside or into the city knows that finding a parking space is a problem. moreover, if you are driving into a place where you are not on the road, it is really hard to find a reasonable parking position. understanding the user's difficulties, google maps has launched this feature to help users solve problems.
  • Search and launch Java Control Panel on Windows operating systemSearch and launch Java Control Panel on Windows operating system
    in java control panel you can search and change java settings. in the following article, network administrator will show you how to search for java control panel on windows operating system.
  • How to Check Java Version on MacHow to Check Java Version on Mac
    tipsmake today will show you how to check the java version installed on mac through customizing the system preferences system, java website or terminal.
  • What is JAVA file? How to open, edit and convert JAVA filesWhat is JAVA file?  How to open, edit and convert JAVA files
    a file with a .java extension is (or sometimes also used in .jav format) is a java source file written in the java programming language.
  • Build your own simple NAS system: Part 2: InstallationBuild your own simple NAS system: Part 2: Installation
    after we have finished preparing the nas system, in this second article, we will go through the steps of installing nas4free (freenas 7) on our old system.
  • eQuiz - Multiple choice quiz about JAVAeQuiz - Multiple choice quiz about JAVA
    following are multiple-choice questions related to java programming language, there will be 23 questions in total with no answer for each time. let's get started!
  • Download and install Java on the computerDownload and install Java on the computer
    to program java on your computer, you need to install the java environment, this tutorial will show you how to download and install java on your computer.
  • Basic Java exercises, with sample decodingBasic Java exercises, with sample decoding
    to serve your java learning needs, tipsmake.com has synthesized some java exercises from many sources, including sample code (for some articles). hopefully it can be helpful to learn your java programming language.
  • Which career Java programming options are waiting for you?Which career Java programming options are waiting for you?
    java programmers are experts in the java programming language. as of 2018, there are many job opportunities for java programmers. with an expected growth rate of 19% for the period 2014-2024, the career prospects for the java language are really bright.