0% found this document useful (0 votes)
2 views

Import Java

The document is a Java program that implements a simple parking system allowing users to park and remove containers from a parking lot with 100 spots. It provides a menu for users to choose options such as parking a container, removing a container, displaying available spots, or exiting the system. The program uses an array to track the occupancy of parking spots and includes input validation for user actions.

Uploaded by

falakshivangi11
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Import Java

The document is a Java program that implements a simple parking system allowing users to park and remove containers from a parking lot with 100 spots. It provides a menu for users to choose options such as parking a container, removing a container, displaying available spots, or exiting the system. The program uses an array to track the occupancy of parking spots and includes input validation for user actions.

Uploaded by

falakshivangi11
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

Scanner;

public class ParkingSystem {

// Array representing parking spots (1 = occupied, 0 = available)

private static int[] parkingLot;

private static final int TOTAL_SPOTS = 100;

public static void main(String[] args) {

parkingLot = new int[TOTAL_SPOTS];

Scanner scanner = new Scanner(System.in);

while (true) {

System.out.println("Parking System Menu:");

System.out.println("1. Park a container");

System.out.println("2. Remove a container");

System.out.println("3. Display available spots");

System.out.println("4. Exit");

System.out.print("Choose an option: ");

int choice = scanner.nextInt();

switch (choice) {

case 1:

parkContainer(scanner);
break;

case 2:

removeContainer(scanner);

break;

case 3:

displayAvailableSpots();

break;

case 4:

System.out.println("Exiting parking system...");

System.exit(0);

break;

default:

System.out.println("Invalid option. Please try again.");

// Park a car in the first available spot

private static void parkContainer(Scanner scanner) {

for (int i = 0; i < TOTAL_SPOTS; i++) {

if (parkingLot[i] == 0) {

parkingLot[i] = 1; // Mark the spot as occupied

System.out.println("Container parked in spot " + (i + 1));

return;

}
}

System.out.println("No available spots! The parking lot is full.");

// Remove a car from a specified spot

private static void removeContainer(Scanner scanner) {

System.out.print("Enter the parking spot number to remove the car from: ");

int spot = scanner.nextInt();

if (spot < 1 || spot > TOTAL_SPOTS) {

System.out.println("Invalid spot number.");

return;

if (parkingLot[spot - 1] == 0) {

System.out.println("No Container is parked in this spot.");

} else {

parkingLot[spot - 1] = 0; // Mark the spot as available

System.out.println("Container removed from spot " + spot);

// Display all available parking spots

private static void displayAvailableSpots() {

System.out.print("Available spots: ");


for (int i = 0; i < TOTAL_SPOTS; i++) {

if (parkingLot[i] == 0) {

System.out.print((i + 1) + " ");

System.out.println();

You might also like