assignment requirements: When a car departs, the message will include the licens
ID: 3628239 • Letter: A
Question
assignment requirements:When a car departs, the message will include the license number and the number of times the car was moved. 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.
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 (A(for arrive or D for depart), separated by spaces. Cars arrive and depart in the order specified by the input. Each input operation must be "echo printed" (i.e., copied) 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.
Note that the number of moves does not include the one where the car departs from the garage, 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. requirements
~ 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 will use an ArrayList of Car objects to maintain the list of cars in the garage.
~Your Garage class must implement separate methods arrive() and depart(), to handle the arrival and departure, respectively, of a car (in addition to any other methods you may need)
~Also write a CarGaragetest class with a main method that just reads the operations from the file and calls the appropriate Garage class method for each. All the details of processing an arrival or departure are done in the Garage class
this is the data from the input file data which was call carport.txt:
CAR001 A
CAR002 A
CAR003 A
CAR004 A
CAR005 A
CAR001 D
CAR004 D
CAR006 A
CAR007 A
CAR008 A
CAR009 A
CAR010 A
CAR011 A
CAR012 A
CAR013 A
CAR014 A
CAR006 D
CAR014 D
CAR013 D
CAR005 D
CAR015 A
CAR010 D
CAR002 D
CAR015 D
CAR013 D
CAR009 D
CAR003 D
CAR008 D
CAR007 D
CAR012 D
CAR011 D
Explanation / Answer
// Car.java public class Car { private String license; private int numberOfMoves; public Car( String license ) { this.license = license; } public String getLicense() { return license; } public int getNumberOfMoves() { return numberOfMoves; } public void movedOutTemporarily() { numberOfMoves++; } } // Garage.java import java.util.ArrayList; import java.util.List; public class Garage { private List laneSpaces; private int garageSize; public Garage(int size) { laneSpaces = new ArrayList(size); garageSize = size; } public boolean arrive(String license) { if( laneSpaces.size() == garageSize ) { return false; } laneSpaces.add(new Car(license)); return true; } public Car depart(String license) { // Find car in the garage. int foundIndex = -1; for( int index = 0; (indexRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.