#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Exclamation [Homework Help] Problem with this code


    Hi, i am having problems with this assignment, 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.


    this is the code for the Garage class
    Code:
    import java.io.FileNotFoundException;
    
    import java.util.StringTokenizer;
    
    
    public class Garage {
    
        private Car[] garage;
        //private int size=10;
        private int count = 0;
        private String plate;
        private String action;
    
        public Garage() {
            garage = new Car[10];
        }
    
        public void readData(String fileName) throws FileNotFoundException {
            StringTokenizer st = new StringTokenizer(fileName);
            plate = st.nextToken();
            action = st.nextToken();
            System.out.println("I just read " + plate + " " + action);
        }
    
        public void arrive() {
    
            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");
            }
    
        }
    
        public void depart() {
    
            boolean found = false;
            int index;
            index = 0;
            for (int i = 0; i < garage.length; i++) {
                if (garage[i].toString().equals(plate)) {
                    found = true;
    
                }
                index = i;
                break;
            }
    
            if (found) {
                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];
                    System.arraycopy(garage, 0, temp, 0, index - 1);
                    System.arraycopy(garage, index + 1, temp, index, (garage.length - 1) - index);
                    System.arraycopy(temp, 0, garage, 0, garage.length);
                    count--;
                    System.out.println("Car " + plate + " have been removed");
                }
            } else {
                System.out.println("We're sorry " + plate + ", has not parked in this garage");
            }
    
    
        }
    }

    this is the code for the Car class

    Code:
    public class Car {
    
        private String licensePlate;
        private int numberOfMoves;
    
        public Car(String licenseNum) {
            this.licensePlate = licenseNum;
        }
        
        
    
        public int getNumberOfMoveS() {
            return numberOfMoves;
        }
    
        public void movedOutTemporarily() {
            numberOfMoves++;
        }
        
        @Override
        public String toString() 
        {
            // Do not modify this method
            return licensePlate.toString() ;  // note: calls inherited toString method
        }
    }

    this is the code for the tester class


    Code:
    public class Tester {
    
        public static void main(String[] args) throws IOException {
            Garage javaGarage = new Garage();
    
            File myFile = new File("C:\\Users\\Documents\\NetBeansProjects\\HW2\\garage.txt");
            FileReader fileReader = new FileReader(myFile);
            
            BufferedReader reader = new BufferedReader(fileReader);
                
                String line;
                while ((line = reader.readLine()) != null) {
                    javaGarage.readData(line);
    
                    StringTokenizer st = new StringTokenizer(line);
                    String plate = st.nextToken();
                    String action = st.nextToken();
                switch (action) {
                    case "ARRIVE":
                        javaGarage.arrive();
                        break;
                    case "DEPART":
                        javaGarage.depart();
                        break;
                }
                }
             
            }
    this is the text for the text file
    JAV001 ARRIVE
    JAV002 ARRIVE
    JAV003 ARRIVE
    JAV004 ARRIVE
    JAV005 ARRIVE
    JAV001 DEPART
    JAV004 DEPART
    JAV006 ARRIVE
    JAV007 ARRIVE
    JAV008 ARRIVE
    JAV009 ARRIVE
    JAV010 ARRIVE
    JAV011 ARRIVE
    JAV012 ARRIVE
    JAV013 ARRIVE
    JAV014 ARRIVE
    JAV006 DEPART
    JAV014 DEPART
    JAV013 DEPART
    JAV005 DEPART
    JAV015 ARRIVE
    JAV010 DEPART
    JAV002 DEPART
    JAV015 DEPART
    JAV013 DEPART
    JAV009 DEPART
    JAV003 DEPART
    JAV008 DEPART
    JAV007 DEPART
    JAV012 DEPART
    JAV011 DEPART

    this is the output that i get
    run:
    I just read JAV001 ARRIVE
    JAV001 has been parked.
    I just read JAV002 ARRIVE
    JAV002 has been parked.
    I just read JAV003 ARRIVE
    JAV003 has been parked.
    I just read JAV004 ARRIVE
    JAV004 has been parked.
    I just read JAV005 ARRIVE
    JAV005 has been parked.
    I just read JAV001 DEPART
    Car JAV001 have been removed
    I just read JAV004 DEPART
    We're sorry JAV004, has not parked in this garage
    I just read JAV006 ARRIVE
    JAV006 has been parked.
    I just read JAV007 ARRIVE
    JAV007 has been parked.
    I just read JAV008 ARRIVE
    JAV008 has been parked.
    I just read JAV009 ARRIVE
    JAV009 has been parked.
    I just read JAV010 ARRIVE
    JAV010 has been parked.
    I just read JAV011 ARRIVE
    JAV011 has been parked.
    I just read JAV012 ARRIVE
    We're sorry JAV012, at the moment we are full
    I just read JAV013 ARRIVE
    We're sorry JAV013, at the moment we are full
    I just read JAV014 ARRIVE
    We're sorry JAV014, at the moment we are full
    I just read JAV006 DEPART
    We're sorry JAV006, has not parked in this garage
    I just read JAV014 DEPART
    We're sorry JAV014, has not parked in this garage
    I just read JAV013 DEPART
    We're sorry JAV013, has not parked in this garage
    I just read JAV005 DEPART
    We're sorry JAV005, has not parked in this garage
    I just read JAV015 ARRIVE
    We're sorry JAV015, at the moment we are full
    I just read JAV010 DEPART
    We're sorry JAV010, has not parked in this garage
    I just read JAV002 DEPART
    Car JAV002 have been removed
    I just read JAV015 DEPART
    We're sorry JAV015, has not parked in this garage
    I just read JAV013 DEPART
    We're sorry JAV013, has not parked in this garage
    I just read JAV009 DEPART
    We're sorry JAV009, has not parked in this garage
    I just read JAV003 DEPART
    Car JAV003 have been removed
    I just read JAV008 DEPART
    We're sorry JAV008, has not parked in this garage
    I just read JAV007 DEPART
    We're sorry JAV007, has not parked in this garage
    I just read JAV012 DEPART
    We're sorry JAV012, has not parked in this garage
    I just read JAV011 DEPART
    We're sorry JAV011, has not parked in this garage
    BUILD SUCCESSFUL (total time: 0 seconds)
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    240
    Rep Power
    29
    Hi, i am having problems with this assignment
    I fear you forgot to tell what kind of problems you have exactly, so it is not clear what you are asking for.

    Also note that code should be wrapped in (code)...(/code) tags - only use square brackets - to be formatted.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by rodiongork
    I fear you forgot to tell what kind of problems you have exactly, so it is not clear what you are asking for.

    Also note that code should be wrapped in (code)...(/code) tags - only use square brackets - to be formatted.
    Thank you, I wrapped the code(this is my first post ever in a forum ). The problem is that it is supposed to check if the strings from the input are already in the garage, bur after the code gets to remove JAV001, and it does, from that point and on it checks only for JAV002 DEPART even though the input from the text file ask for JAV004 DEPART to be remove, so it does that and when it encounters JAV004 in the garage it is not removed, and the same happens till the end of file.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Try debugging the depart() method by using the println() method to print out the data that the code is looking at to see why a match is not found. What tests are not doing what you want them to do?
    For example if this was the code doing the tests:
    if (x == y)
    then print out the values of x and y just before that statement:
    System.out.println("x="+x +", y="+y);

IMN logo majestic logo threadwatch logo seochat tools logo