Wednesday, December 11, 2013

[Fab.com] Design a car parking lot.

Design a car parking lot. Tell about all the classes and functions. It should be able to say full, empty, normal.
The lot has 3 different kind of parking : regular, handicapped and compact.

1 comment:

  1. public class ParkingLot
    {
    Vector vacantParkingSpaces = null;
    Vector fullParkingSpaces = null;

    int parkingSpaceCount = 0;

    boolean isFull;
    boolean isEmpty;

    ParkingSpace findNearestVacant(ParkingType type)
    {
    Iterator itr = vacantParkingSpaces.iterator();

    while(itr.hasNext())
    {
    ParkingSpace parkingSpace = itr.next();

    if(parkingSpace.parkingType == type)
    {
    return parkingSpace;
    }
    }
    return null;
    }

    void parkVehicle(ParkingType type, Vehicle vehicle)
    {
    if(!isFull())
    {
    ParkingSpace parkingSpace = findNearestVacant(type);

    if(parkingSpace != null)
    {
    parkingSpace.vehicle = vehicle;
    parkingSpace.isVacant = false;

    vacantParkingSpaces.remove(parkingSpace);
    fullParkingSpaces.add(parkingSpace);

    if(fullParkingSpaces.size() == parkingSpaceCount)
    isFull = true;

    isEmpty = false;
    }
    }
    }

    void releaseVehicle(Vehicle vehicle)
    {
    if(!isEmpty())
    {
    Iterator itr = fullParkingSpaces.iterator();

    while(itr.hasNext())
    {
    ParkingSpace parkingSpace = itr.next();

    if(parkingSpace.vehicle.equals(vehicle))
    {
    fullParkingSpaces.remove(parkingSpace);
    vacantParkingSpaces.add(parkingSpace);

    parkingSpace.isVacant = true;
    parkingSpace.vehicle = null;

    if(vacantParkingSpaces.size() == parkingSpaceCount)
    isEmpty = true;

    isFull = false;
    }
    }
    }
    }

    boolean isFull()
    {
    return isFull;
    }

    boolean isEmpty()
    {
    return isEmpty;
    }
    }

    public class ParkingSpace
    {
    boolean isVacant;
    Vehicle vehicle;
    ParkingType parkingType;
    int distance;
    }

    public class Vehicle
    {
    int num;
    }

    public enum ParkingType
    {
    REGULAR,
    HANDICAPPED,
    COMPACT,
    MAX_PARKING_TYPE,
    }

    ReplyDelete