Parking Lot
A parking garage consists of several levels with many parking spaces on each level. A garage management program keeps track of the number of cars parked in the garage and helps to find the location of a car with a given ID. Consider the following three Java classes used in this program. The class Car represents an individual car:
Car.java
public class Car { // method getID: returns the id of this Car public String getID() { /* implementation not shown */ } // ... private data, constructors, and other methods not shown }
A ParkingLevel object keeps track of the cars parked within its spaces as well as empty spaces. The parking spaces on each level are numbered by consecutive integers, starting from 0.
ParkingLevel.java
public class ParkingLevel { // Method numSpaces: returns the number of parking spaces on this level public int numSpaces() { /* implementation not shown */ } // Method getCar: returns the car parked in the space number spaceNum // or null if that space is empty public Car getCar(int spaceNum) { /* implementation not shown */ } // Method findEmptySpace: returns the smallest space number of an available space or // -1 if the level is full and there are no empty spaces public int findEmptySpace() { /* implementation not shown */ } //Method setCar: marks space number spaceNum on this level as occupied // by Car v; if v == null the space is marked as empty // precondition: space number spaceNum is empty public void setCar(int spaceNum, Car v) { /* implementation not shown */ } // ... private data, constructors, and other methods // not shown }
The ParkingGarage class keeps track of all the levels in the garage. It keeps the information about the levels in an array of ParkingLevel objects.
ParkingGarage.java
public class ParkingGarage { private int totalCars; // total number of cars in this garage private ParkingLevel[] levels; // each element corresponds to a level in the garage // Method isFull: returns true if there are no available spaces on any // level within the garage; otherwise, returns false public boolean isFull() { /* to be implemented */ } // Method isCarAlreadyParked: returns true if a car with a given ID is // already parked somewhere in the garage; otherwise, returns false public boolean isCarAlreadyParked(String id) { /* implementation not shown */ } // parkCar: if the garage is not full and no car with the same ID // as v's ID is in the garage yet, parks Car v into the // garage, updates the number of cars parked in the // garage, and returns true; otherwise, leaves the garage // unchanged and returns false public boolean parkCar(Car v) { /* to be implemented */ } // removeCar if a car with a given ID String id is found, removes // that car, updates the number of cars parked in the garage // and returns the found car; otherwise, leaves the garage // unchanged and returns null // precondition: id is not null and is a non-empty string public Car removeCar(String id) { /* to be implemented */ } // ... private data, constructors, and other methods not shown }