Question: I'm writing a garage java program and im getting out of bounds exceptions, my temp move method is messed up, and im having problems print

I'm writing a garage java program and im getting out of bounds exceptions, my temp move method is messed up, and im having problems print the file to a txt.file. any help would be great.

Summary

The garage is a single row of 10 (0-9). Cars can only leave from position 0. all cars in front of the car trying to leave have to be temporary moved and accounted for. And we are not alowed to use array list.

Current code

Car Class

public class car { private String licensePlate; private int timesMoved = 0; /** * * @param plate stores the value plate */ public car(String plate) { licensePlate = plate; } /** * adds the times a car was temporary moved */ public void carMoves() { timesMoved++; } /** * * @return the times a car was moved */ public int getCarMoves() { return timesMoved; } /** * * @return license plate stored */ public String getLicensePlate() { return licensePlate ; } }

import java.util.Arrays; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.StringTokenizer;

Garage class

public class garage {

private car[] garageCars; private String action; private String plate; private final int MAX_PARKING = 10; private int carCounter = 0; // counts the cars in the garage private int timesMoved = 0; /** * constructing an object for cars */ public garage() { garageCars = new car[MAX_PARKING]; }

/** * this method reads the garage txt file * and splits the string into two with * tokenizer * @param fileName is the garage.txt file * @throws FileNotFoundException */ public void readData(String fileName) throws FileNotFoundException { StringTokenizer st = new StringTokenizer(fileName); plate = st.nextToken(); action = st.nextToken(); System.out.println("Request: " + plate + " " + action + " "); } /** * a method for cars arriving, if garage contains 10 cars then car is turned * away. If not, a new car object is made incrementing by 1. * @return returns the results of arrival. */ public String arrive() { String arrive; if (carCounter >= MAX_PARKING) { arrive = "Sorry, " + plate + " was turned away due to insufficient parking space. "; return arrive; } else { garageCars[carCounter] = new car(plate); arrive = "Good news, " + plate + " was parked in position " + carCounter + ". "; carCounter++; } return arrive; }

/** * if the car being requested to depart exist in the parking lot it leaves. * all cars behind the car requested shift down one. all cars in front * temporary move out so the requested car may leave. * @return */ public String depart() { boolean carInLot = false; String carAction = ""; int carPosition; // element containing car object carPosition = 0; for (int i = 0; i < carCounter; i++) // loop checking if there are any cars parked { if (garageCars[i].getLicensePlate().equals(plate)) { carInLot = true; carPosition = i; break; } }

if (!carInLot) { carAction = "We're sorry " + plate + ", has not parked in this garage "; } else if (carInLot) { if (carPosition == 0) // if car position is 0 everything behind it moves down 1 { for (int i = carCounter; i > 0; i-- ) { int j = carCounter; garageCars[j] = garageCars[j-1]; carAction = "Vehicle " + plate + " has departed and was temporarly moved " + timesMoved + " times."; } carCounter--; } else if (carPosition > 0) { for (int i = carPosition + 1; i <= carCounter; i++) // counting the times a car was temp moved { garageCars[i].carMoves(); timesMoved = garageCars[i].getCarMoves(); carAction = "Vehicle " + plate + " has departed and was temporarly moved " + timesMoved + " times."; } for (int i = carCounter; i > 0; i-- ) // shift cars down 1 { int j = carCounter; garageCars[j] = garageCars[j-1]; } carCounter--; } }

return carAction; } /** * * @param output prints the output from depart and arrive methods in txt * @throws IOException */ public void outToText(String output) throws IOException { FileWriter outputText = new FileWriter("MyGarageActions.txt",true); outputText.write(output); outputText.close(); } }

Tester

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.StringTokenizer;

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author jmart790 */ public class GarageTester {

/** * @param args the command line arguments * @throws java.io.IOException */ public static void main(String[] args) throws IOException { garage myGarage = new garage(); URL garageText = new URL("http://users.cis.fiu.edu/~shawg/3337/garage.txt"); BufferedReader in = new BufferedReader(new InputStreamReader(garageText.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) { myGarage.readData(inputLine); StringTokenizer st = new StringTokenizer(inputLine); String plateNum = st.nextToken(); String carStatus = st.nextToken(); switch (carStatus) { case "ARRIVE": System.out.println(myGarage.arrive()); // myGarage.outToText(myGarage.arrive()); break; case "DEPART": System.out.println(myGarage.depart()); // myGarage.outToText(myGarage.depart()); break; } } in.close();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!