can I have a little explanation of what is done in each code. The Bashemin Parki
ID: 3890743 • Letter: C
Question
can I have a little explanation of what is done in each code.
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.
II. Specifications
Create separate classes to implement a Car and a Garage
The Car class will have private instance variables that store the license number and number of times the car has been moved, and any methods you discover to be necessary
The Garage class must use an array (not an ArrayList) of Car objects to implement the garage
To handle the arrival and departure of each car, your Garage class must implement separate methods arrive() and depart(), each of which returns a String showing the result of the operation (see above).
Your garage class may contain other methods should you find them necessary or advisable.
Also write a “test” class that reads the operations from the input file, echoes each to the output file, calls the appropriate Garage class method, and writes the result to the output file.
As stated in 4. and 6. above, all output is to be done in the test class
input file:
Explanation / Answer
Please see below code for your parking problem.
all the code is commented properly to let you understand it.
please give us feedback so that i can keep working to help you further.
Thanks
Code:
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:
The containt for input file is same as you have attached.
output files is mentioned below.
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
Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.