Parking System Using Java.

Parking System using Java.

Introduction:

Welcome to Car Parking System using Java! This system allows you to make your task to park the cars easy. Java is a popular programming language that is widely used for developing parking management systems due to its simplicity, versatility, and platform independence. This system analyses for an available space in parking area and on the basis of it gives the output to user. If all slots are full then it will simply tell the user to not park car in that particular area. It increments the available slots if any user removed his/her car from parking area and decrements slots if any user park his/her car. Overall, a parking system in Java provides a flexible and powerful platform for managing parking spaces, improving traffic flow, and enhancing the overall parking experience for drivers. By leveraging the strengths of Java, developers can create robust and scalable parking systems that meet the unique needs of their users.

 

Explanation:

The Parking System we will build will be a simple console-based application that allows users to park their cars, removed their cars and view all parked cars. It presents a menu with four options: park a car, remove a car, view parked cars, and exit the program. It is very simple with coding point of view as it has only one java class in it. We need to import Scanner class and Array List class. Scanner class used to take input through the keyboard from user while Array List is used to make things simple for adding the cars and deleting them. User needs to provide total number of parking slots and then main menu will be displayed for further operations. In park car method, we will check for availability of slots. If slots are available then user will provide car license number and message will be displayed as “Car parked successfully”. In remove car method, we will check for number of parked cars if it appears as zero then process will not go further. By entering license number of parked car one can simply removed it. Inbuilt functions of Array List ‘add’ and ‘remove’ are used to add cars in array list and remove it from the array list, respectively. Along with the process of adding and removing the available slots will get decrement and increment accordingly. For displaying all parked cars we have used for each loop and displaying Array List named as parked cars. All these three methods have been called for particular case of switch case. We have tried our best to make a code as simple as possible for better understanding. Just some basic knowledge of java and even beginners can code it easily. Happy Coding!

 

Source Code:

				
					import java.util.ArrayList;
import java.util.Scanner;

public class ParkingSystem {
    
    static int totalSlots, availableSlots;
    static ArrayList<String> parkedCars = new ArrayList<String>();
    
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the total number of parking slots:");
        totalSlots = sc.nextInt();
        availableSlots = totalSlots;
        
        while (true) {
            System.out.println("\nWhat 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.");
            }
        }
    }
    
    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 parked successfully. Available slots: " + availableSlots);
    }
    
    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. Available slots: " + availableSlots);
        } else {
            System.out.println("The car is not parked here.");
        }
    }
    
    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);
        }
    }
}
				
			

Output:

Find More Projects

Hostel Management System Using Python With Source Code by using Python Graphical User Interface GUI Introduction: Managing a hostel efficiently can be …

Contra Game Using Python with source code using Pygame Introduction: Remember the super fun Contra game where you fight aliens and jump …

Restaurant Billing Management System Using Python with Tkinter (Graphical User Interface) Introduction: In the bustling world of restaurants, efficiency is paramount, especially …

Jungle Dash Game Using Python with source code with pygame module Introduction: Dive into the excitement of jungle adventures with our new …

Building a Tetris Game with Python with source code Introduction: Welcome to the world of Tetris, where falling blocks meet strategic maneuvers …

Super Mario Game Using Python with source code Introduction: Remember playing Super Mario as a kid? It’s that classic game where you …

All Coding Handwritten Notes

Browse Handwritten Notes