Wednesday, January 5, 2011

OO Design for ParkingLot

Its a parking lot design day. I am thinking of a simple parking lot where there are finite number of spaces and the space can either be a compact/handicap/regular.
A car can be parked or unparked. The parking lot can be full or empty.

I think below design will do:
ParkingLot: this class can park or unpark and holds finite number of spaces and the spaces are held by two lists one for Parkedspaces and one for unparkedSpaces
ParkingSpace: is another class that represents the actual parking space. It needs a property to specify whether it is a compact/handicap/regular parking space
Car: is the final class that has a property of what kind of car it is and if it is parked, the slot number where it is parked.
public class ParkingLot {
int totalSpaces = 100;
public static List ParkingSpace parkedSpaces = new ArrayList();
public static List ParkingSpace emptySpaces = new ArrayList();

public static void park(Car car) {
//Traverse though the emptySpaces list to find the
//spot for specific car type
//set the car object for that parkingspace
//change it to park. move the object into parkedSpaces
// remove it from emptySpaces
}

public static void unPark(int parkingSpot) {
//get the parkingspot from array
//set the parkingspace to available
}

public static boolean isEmpty() {
if (emptySpaces.size() > 0)
return true;
return false;
}

public static void main(String args[]) {
//build emptySpaces list. Insert 100 parking spaces
//with their type properties
Car car = new Car();
car.setCompact(true);
if(ParkingLot.isEmpty())
ParkingLot.park(car);

ParkingLot.unPark(20);
}
}

public class ParkingSpace {

boolean isCompact;
boolean isRegular;
boolean isHandicap;

//setter and getter calls

}

public class Car {
boolean isCompact;
boolean isRegular;
boolean isHandicap;
int parkingSlotNumber;

public Car() {
}

public void setParkingSlotNumber(int value) {
this.parkingSlotNumber = value;
}
}

The Car object can be further subclassed as CompactCar, HandiCapCar, RegularCar instead of the booleans. Same with the ParkingSpace as well.

But is there any use in that?

Please share your thoughts.

No comments:

Post a Comment