In: Computer Science
using java and use stack library java
Bashemin Parking garage
The Bashemin Parking Garage contains a single lane that holds up to ten cars. There is only a single entrance/exit to the garage at one end of the lane. If a customer arrives to pick up a car that is not nearest the exit, all cars blocking its path. are moved out, the customer's car is driven out and the other cars are restored in the same order that they were in originally.
Write a program that processes a group of input lines. Each input line contains an "A" for arrival or a "D" for departure and a license plate number. Cars are assumed to arrive and depart in the order specified by the input. The program should print a message whenever a car arrives or departs. When a car arrives, the message should specify whether or not there is room for the car in the garage. If there is no room the car leaves without entering the garage. When a car departs, the message should include the number of times that the car was moved out to allow other cars to depart.
You will submit a program listing (properly commented), your sample data (at least 20 lines of text) and the output.
PROGRAM:
import java.util.*;
import java.lang.*;
import java.io.*;
class garage
{
public static void main (String[] args)
{
Scanner sc = new
Scanner(System.in);
String s="";
s = sc.nextLine();
// STORE WHETHER A CAR IS
ARRIVING OR DEPARTING WITH ITS PLATE NUMBER
String type;
String plateNumber;
//STACK TO SIMULATE THE
GARAGE WHICH CAN STORE ONLY 10 CARS
Stack<String>
parkingGarage = new Stack<String>();
//TEMPORARY STACK USED DURING REMOVAL OF ANY CAR
Stack<String> temp =
new Stack<String>();
//STORES THE NUMBER OF TIMES
A CAR WITH A PLATENUMBER P MOVES
//OUT OF THE GARAGE FOR OTHER
CARS
HashMap<String,Integer>
map = new HashMap<>();
//UNTIL USER ENTERS THE TEXT
END
//A VALID SEQUENCE OF ARRIVALS AND DEPARTURES IS
//EXPECTED FROM THE USER
while(!s.equals("END")){
//OBTAINING THE PLATE NUMBER AND
TYPE(ARRIVAL/DEPARTURE) FROM THE USER
String[] typeAndPlateNumber = s.split(" ",2);
type
= typeAndPlateNumber[0];
plateNumber = typeAndPlateNumber[1];
//IN CASE OF ARRIVAL
if(type.equals("A")){
//CAR
ENTERS ONLY IF GARAGE HAS LESS THAN 10 CARS
if (parkingGarage.size()<10){
System.out.println("There is room in the garage for the
car.");
parkingGarage.push(plateNumber);
System.out.println("Car with plate number "+plateNumber+" has
arrived.");
map.put(plateNumber,0);
}else{
System.out.println("There is no room in the garage for the
car.");
}
}else{
//REMOVE THE CARS UNTIL THE ACTUAL CAR TO BE REMOVED
//IS
REACHED
while(!(parkingGarage.peek().equals(plateNumber))){
int temp1 =
map.get(parkingGarage.peek());
map.put(parkingGarage.peek(),temp1+1);
temp.push(parkingGarage.peek());
parkingGarage.pop();
}
//REMOVE THE CAR THAT IS TO DEPART
System.out.println("\nCar with plate number "+plateNumber+" has
departed and it moved out "+String.valueOf(map.get(plateNumber)+"
times.\n"));
parkingGarage.pop();
//REENTER THE CARS IN THE ORIGINAL ORDER BACK INTO THE
GARAGE
while(!temp.empty()){
parkingGarage.push(temp.peek());
temp.pop();
}
}
s =
sc.nextLine();
}
}
}
INPUT:
A C1
A C2
A C3
A C4
A C5
A C6
A C7
A C8
A C9
D C7
A C10
A C11
A C12
D C1
D C12
D C11
D C10
D C9
D C8
D C6
D C5
CONSOLE INTERACTION & OUTPUT:
A C1
There is room in the garage for the car.
Car with plate number C1 has arrived.
A C2
There is room in the garage for the car.
Car with plate number C2 has arrived.
A C3
There is room in the garage for the car.
Car with plate number C3 has arrived.
A C4
There is room in the garage for the car.
Car with plate number C4 has arrived.
A C5
There is room in the garage for the car.
Car with plate number C5 has arrived.
A C6
There is room in the garage for the car.
Car with plate number C6 has arrived.
A C7
There is room in the garage for the car.
Car with plate number C7 has arrived.
A C8
There is room in the garage for the car.
Car with plate number C8 has arrived.
A C9
There is room in the garage for the car.
Car with plate number C9 has arrived.
D C7
Car with plate number C7 has departed and it moved out 0 times.
A C10
There is room in the garage for the car.
Car with plate number C10 has arrived.
A C11
There is room in the garage for the car.
Car with plate number C11 has arrived.
A C12
There is no room in the garage for the car.
D C1
Car with plate number C1 has departed and it moved out 0 times.
D C11
Car with plate number C11 has departed and it moved out 1 times.
D C10
Car with plate number C10 has departed and it moved out 1 times.
D C9
Car with plate number C9 has departed and it moved out 2 times.
D C8
Car with plate number C8 has departed and it moved out 2 times.
D C6
Car with plate number C6 has departed and it moved out 1 times.
D C5
Car with plate number C5 has departed and it moved out 1 times.
END