Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need a lot of help with this The Bashemin Parking Garage contains a single lan

ID: 3873662 • Letter: I

Question

I need a lot of help with this

The Bashemin Parking Garage contains a single lane that can hold up to ten cars. Arriving cars enter the garage at the rear and are parked in the empty space nearest to the front. Departing cars exit only from the front.

If a customer needs to pick up a car that is not nearest to the exit, then all cars blocking its path are moved out temporarily, the customer's car is driven out, and the other cars are restored in the order they were in originally. Whenever a car departs, all cars behind it in the garage are moved up one space.

Write a Java program to operate the garage.

The program will read and process lines of input from a file until end-of-file. Each input line contains a license plate number and an operation (ARRIVE or DEPART), separated by spaces. Cars arrive and depart in the order specified by the input. Each input operation must be "echoed" to an output file, along with an appropriate message showing the status of the operation.

When a car arrives, the message will include the license number and state whether the car is being parked or turned away because the garage is full. If the garage is full, the car leaves without ever having entered the garage.

When a car departs, the message will include the license number and the number of times the car was moved.

The number of moves does not include the one where the car departs from the garage, or the number of times the car was moved within the garage. It is only the number of times it was moved out of the garage temporarily to allow a car behind it to depart.

If a DEPART operation calls for a car that is not in the garage, the message should so state.

THERE WILL BE NO OUTPUT IN ANY CLASS OTHER THAN THE TESTER

ALSO, SCANNER NEEDS TO BE USED FOR READING A FILE FROM THIS FILE

_________________________________________________

This is what i have so far:

public class Car
{
private int moves=0;
private String license;
  
  
public void tempMove()
{
moves++;
}
  
public int getTempMoves(){
return moves;
}
  
public Car(String licenseNumber)
{
this.license = licenseNumber;
}
}


public class Garage
{
private Car[] Garage;
private String licensePlate;
private int count = 0;
  
  
public Garage()
{
Garage = new Car[10];

}
public void carArrival(){
boolean canFit = false;
if(count ==0){
  
}
  
  
}
  
}

import java.io.IOException;

public class GarageTester {
public static void main(String[] args) throws IOException {
  
  
  
}
  
}

Explanation / Answer



// Car.java
public class Car

{

private String licenceNo;

private int countOfMove;

public Car()

{

countOfMove=0;

}

public String getLicenceNo() {

return licenceNo;

}

public void setLicenceNo(String licenceNo) {

this.licenceNo = licenceNo;

}

public int getCountOfMove() {

return countOfMove++;

}

public void setCountOfMove() {

this.countOfMove++;

}

}

//Garage.java

public class Garage

{

Car[] cars=new Car[12];

//this variable keeps track of current free location in garage.

int currentPosition;

public Garage()

{

//By default it is set to 1, which indicates location 1 is free for parking.

currentPosition=1;

}

/*

* this method is the one getting called from main class.

* this method determines on the basis of input string what action so be done.(arrive or depart)

*/

public String doAction(String inputString)

{

String[] params=inputString.split(" ");

if(params[1].equals("ARRIVE"))

{

//call arrive method

return arrive(params[0]);

}

else if(params[1].equals("DEPART"))

{

//call depart method

return depart(params[0]);

}

return "UNEXPECTED_ERROR";

}

private String arrive(String licenceNo)

{

/*if currentPosition variable points to the location greater than 10 means,

aleady 10 locations have been occupied and we dont have enogh space to accomodate one more car.

*/

if(currentPosition>10)

{

//return the Turned_away status when we dont have space

return licenceNo+" TURNED_AWAY";

}

Car car=new Car();

car.setLicenceNo(licenceNo);

cars[currentPosition]=car;

currentPosition++;

return licenceNo+" PARKED";

}

private String depart(String licenceNo)

{

int location=getLocationOfCar(licenceNo);

if(location==0)

{

//if location is 0 means , the car we triing to depart is not parked before,

//may be it came for parking but turned it away because of no space

System.out.println("not Proper instructions in File, trying to depart car "+licenceNo+" which is not et parked");

return licenceNo+" RequestedCarIsNotYetParked";

}

int moves=cars[location].getCountOfMove();

for(int i=1;i<location;i++)

{

//this method will keep the car out of garage for some time and will increase its counter of movement b one each time.

keepOutTemporary(i);

}

for(int j=location;j<currentPosition;j++)

{

cars[j]=cars[j+1];

}

currentPosition--;

return licenceNo+" "+moves;

}

private void keepOutTemporary(int i)

{

cars[i].setCountOfMove();

}

/*

* this method returns a location of particular car in garage. so that i can be moved out of garage for departure.

*/

private int getLocationOfCar(String licenceNo)

{

for(int i=1;i<currentPosition;i++)

{

if(cars[i].getLicenceNo().equals(licenceNo))

{

return i;

}

}

return 0;

}

}

Driver.java

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.nio.Buffer;

public class Driver

{

public static void main(String args[])

{

Garage garage=new Garage();

BufferedReader br = null;

BufferedWriter bw=null;

FileReader fr = null;

FileWriter fw=null;

try {

//br = new BufferedReader(new FileReader(FILENAME));

fr = new FileReader("InputFile.txt");

fw=new FileWriter("OutputFile.txt");

br = new BufferedReader(fr);

bw=new BufferedWriter(fw);

String sCurrentLine;

while ((sCurrentLine = br.readLine()) != null)

{

String output=garage.doAction(sCurrentLine);

bw.write(output+" ");

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (br != null)

br.close();

if (fr != null)

fr.close();

if (bw != null)

bw.close();

if (fw != null)

fw.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

}

Output:

output file:

JAV001 PARKED
JAV002 PARKED
JAV003 PARKED
JAV004 PARKED
JAV005 PARKED
JAV001 0
JAV004 0
JAV006 PARKED
JAV007 PARKED
JAV008 PARKED
JAV009 PARKED
JAV010 PARKED
JAV011 PARKED
JAV012 PARKED
JAV013 TURNED_AWAY
JAV014 TURNED_AWAY
JAV006 0
JAV014 RequestedCarIsNotYetParked
JAV013 RequestedCarIsNotYetParked
JAV005 1
JAV015 PARKED
JAV010 0
JAV002 4
JAV015 0
JAV013 RequestedCarIsNotYetParked
JAV009 2
JAV003 6
JAV008 3
JAV007 4
JAV012 1
JAV011 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote