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

JAVA, this is what I\'ve gotten so far for this program, I need help getting the

ID: 3751078 • Letter: J

Question

JAVA, this is what I've gotten so far for this program, I need help getting the program to display how many times the CAR WAS MOVED on depart.

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 “echo printed” 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

To get credit, your Garage class must use an array (not an ArrayList) of Car objects to implement the garage. No credit will be given if you make the assignment unnecessarily more difficult by using more than one array

Use a defined constant to set the size of the array. Other than in the constant declaration, the literal 10 should not appear in your program

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 String returned to the output file

As stated in 5. and 7. above, all output is to be done in the test class. None of the Garage or Car class methods do any output

Here is the text file

this is what I have, please modify to show how many times the car was moved on depart

car class

public class Car {

private String licensePlate;

private int numberOfMoves;

/*Constructor for licensePlate initialization*/

public Car(String licenseNum) {

this.licensePlate = licenseNum;

}

/*Returns the Number of moves performed by each car*/

public int getNumberOfMoves() {

return numberOfMoves;

}

/*Count the Number of moves performed by each car*/

public void movedOutTemporarily() {

numberOfMoves++;

}

  

/*Convert licensePlate to string */

@Override

public String toString()

{

return licensePlate.toString() ;

}

}

garage class

package garage;
import java.io.FileNotFoundException;

import java.util.StringTokenizer;

public class Garage {

private Car[] garage;

private int count = 0;

private String plate;

private String action;

/* COnstructor for initialize the garage array */

public Garage() {

garage = new Car[10];

}

/* Method To Read the data from the file */

public void readData(String fileName) throws FileNotFoundException {

StringTokenizer st = new StringTokenizer(fileName);

plate = st.nextToken();

action = st.nextToken();

System.out.println("The car details " + plate + " " + action + " taken from the file it was moved");

}

/* Method for car Arriving */

public void arrive() {

/*

* park the car based on the empty space in the garage(count represent the empty

* space)

*/

if (count == 0 && action.equals("ARRIVE")) {

garage[count] = new Car(plate);

System.out.println(plate + " has been parked.");

count++;

} else if (count < 10 && action.equals("ARRIVE")) {

garage[count] = new Car(plate);

System.out.println(plate + " has been parked.");

count++;

} else if (count == 10 && action.equals("ARRIVE")) {

System.out.println("We're sorry " + plate + ", at the moment we are full");

}

}

/* Method for departing the Car */

public void depart() {

boolean found = false;

int index;

index = 0;

/* Found the car position in the garage */

for (int i = 0; i < garage.length; i++) {

if (garage[i] != null && garage[i].toString().equals(plate)) {

found = true;

index = i;

break;

}

}

/* If Plate found the remove the car from the garage */

if (found) {

/*

* If car is a initial position then remove it and move the cars forward

* (arraycopy will copy cars from 1 to 9 position)

*/

if (index == 0) {

Car[] temp = new Car[10];

System.arraycopy(garage, 1, temp, 0, 9);

garage = temp;

System.out.println("Car " + plate + " have been removed");

count--;

} else {

for (int a = 0; a < index; a++) {

garage[a].movedOutTemporarily();

}

Car[] temp = new Car[10];

/* Copy the cars from initial position to index position of departed car */

System.arraycopy(garage, 0, temp, 0, index);

/* Copy the cars from index position to reaming position */

System.arraycopy(garage, index + 1, temp, index, (garage.length - 1) - index);

/* Copy the cars from temp array to garage array */

System.arraycopy(temp, 0, garage, 0, garage.length);

count--;

System.out.println("Car " + plate + " have been removed");

}

} else {

/* If plate not found print the message */

System.out.println("Sorry " + plate + ", has not parked in this garage");

}

}

}

tester class

package garage;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.StringTokenizer;

public class Tester {

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

Garage javaGarage = new Garage();

/*File object creation */

File myFile = new File("\garage.txt");

FileReader fileReader = new FileReader(myFile);

BufferedReader reader = new BufferedReader(fileReader);

String line;

while ((line = reader.readLine()) != null) {

/*Invoke the method to Read the content of a file */

javaGarage.readData(line);

StringTokenizer st = new StringTokenizer(line);

String plate = st.nextToken();

String action = st.nextToken();

switch (action) {

case "ARRIVE":

/*Invoke if the Action is ARRIVE */

javaGarage.arrive();

break;

case "DEPART":

/*Invoke if the Action is DEPART*/

javaGarage.depart();

break;

}

}

}

}

Explanation / Answer

I have answered this question before. Here is the completed code for this problem. It would be slightly different than your code, but trust me, this is way more efficient. And all outputs are done to output.txt file as per the question, and not to the console. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// Car.java (modified)

public class Car

{

      private String licenceNo;

      private int countOfMove;

      public String getLicenceNo() {

            return licenceNo;

      }

      public void setLicenceNo(String licenceNo) {

            this.licenceNo = licenceNo;

      }

      public int getCountOfMove() {

            return countOfMove;

      }

      /**

      * method to increment the number of moves car moved

      */

      public void incrementCountOfMove() {

            this.countOfMove++;

      }

}

// Garage.java

public class Garage {

      // defining attributes

      private static final int SIZE = 10;

      private Car cars[];

      private int count;

      /**

      * default constructor

      */

      public Garage() {

            // initializing array

            cars = new Car[SIZE];

            // setting current number of cars to 0

            count = 0;

      }

      /**

      * method to perform the arrival operation of a car

      *

      * @param licenseNumber

      *            license number of the car

      * @return the result of operation, in a String format

      */

      public String arrival(String licenseNumber) {

            if (count == cars.length) {

                  // garage is full

                  return "Garage is full, " + licenseNumber + " is not parked";

            }

            // creating a new car

            Car newCar = new Car();

            newCar.setLicenceNo(licenseNumber);

            // adding to the end and updating count

            cars[count] = newCar;

            count++;

            // return success message

            return licenseNumber + " parked in the Garage";

      }

      /**

      * method to perform the departure operation of a car

      *

      * @param licenseNumber

      *            license number of the car

      * @return the result of operation, in a String format

      */

      public String depart(String licenseNumber) {

            // creating a car

            Car c = new Car();

            c.setLicenceNo(licenseNumber);

            // finding the index of car in the array

            int index = indexOf(c);

            if (index == -1) {

                  // car not found

                  return licenseNumber + " is not found in the Garage";

            }

            /**

            * simulating the movement of cars before the specified car out of the

            * garage

            */

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

                  // incrementing move count

                  cars[i].incrementCountOfMove();

            }

            // storing the car to be removed

            Car removed = cars[index];

            // shifting the remaining cars to occupy the vacant space in array

            for (int i = index; i < count - 1; i++) {

                  cars[i] = cars[i + 1];

            }

            count--; // decreasing the count

            // returning removed car stats

            return removed.getLicenceNo() + " is departed from Garage,"

                        + " number of moves made by this car: "

                        + removed.getCountOfMove();

      }

      /**

      * a private helper method to find the index of a car c in the cars array

      *

      * @param c

      *            - Car object

      * @return index if found, -1 if not found

      */

      private int indexOf(Car c) {

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

                  if (cars[i].getLicenceNo().equals(c.getLicenceNo())) {

                        return i;

                  }

            }

            return -1;

      }

}

// Test.java

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

public class Test {

      public static void main(String[] args) throws FileNotFoundException {

            // replace the file name with yours

            Scanner scanner = new Scanner(new File("input.txt"));

            // creating a Garage

            Garage garage = new Garage();

            // creating a PrintWriter for writing to output file

            PrintWriter writer = new PrintWriter(new File("output.txt"));

            String result;

            // looping through the file

            while (scanner.hasNext()) {

                  // getting attributes

                  String license = scanner.next();

                  String operation = scanner.next();

                  // appending to output file

                  writer.println(license + " " + operation);

                  // performing operations and appending results to output file

                  if (operation.equalsIgnoreCase("DEPART")) {

                        result = garage.depart(license);

                        writer.println(result);

                  } else {

                        result = garage.arrival(license);

                        writer.println(result);

                  }

            }

            writer.close(); //important to close the writer to save the file

            System.out.println("output has been saved to output.txt file");

      }

}

/*output.txt*/

JAV001 ARRIVE

JAV001 parked in the Garage

JAV002 ARRIVE

JAV002 parked in the Garage

JAV003 ARRIVE

JAV003 parked in the Garage

JAV004 ARRIVE

JAV004 parked in the Garage

JAV005 ARRIVE

JAV005 parked in the Garage

JAV001 DEPART

JAV001 is departed from Garage, number of moves made by this car: 0

JAV004 DEPART

JAV004 is departed from Garage, number of moves made by this car: 0

JAV006 ARRIVE

JAV006 parked in the Garage

JAV007 ARRIVE

JAV007 parked in the Garage

JAV008 ARRIVE

JAV008 parked in the Garage

JAV009 ARRIVE

JAV009 parked in the Garage

JAV010 ARRIVE

JAV010 parked in the Garage

JAV011 ARRIVE

JAV011 parked in the Garage

JAV012 ARRIVE

JAV012 parked in the Garage

JAV013 ARRIVE

Garage is full, JAV013 is not parked

JAV014 ARRIVE

Garage is full, JAV014 is not parked

JAV006 DEPART

JAV006 is departed from Garage, number of moves made by this car: 0

JAV014 DEPART

JAV014 is not found in the Garage

JAV013 DEPART

JAV013 is not found in the Garage

JAV005 DEPART

JAV005 is departed from Garage, number of moves made by this car: 1

JAV015 ARRIVE

JAV015 parked in the Garage

JAV010 DEPART

JAV010 is departed from Garage, number of moves made by this car: 0

JAV002 DEPART

JAV002 is departed from Garage, number of moves made by this car: 4

JAV015 DEPART

JAV015 is departed from Garage, number of moves made by this car: 0

JAV014 DEPART

JAV014 is not found in the Garage

JAV009 DEPART

JAV009 is departed from Garage, number of moves made by this car: 2

JAV003 DEPART

JAV003 is departed from Garage, number of moves made by this car: 6

JAV008 DEPART

JAV008 is departed from Garage, number of moves made by this car: 3

JAV007 DEPART

JAV007 is departed from Garage, number of moves made by this car: 4

JAV012 DEPART

JAV012 is departed from Garage, number of moves made by this car: 1

JAV011 DEPART

JAV011 is departed from Garage, number of moves made by this car: 2