121

Design an OO parking lot. What classes and functions will it have. It should say, full, empty and also be able to find spot for Valet parking. The lot has 3 different types of parking: regular, handicapped and compact.

Thanks!

ojblass
  • 21,146
  • 22
  • 83
  • 132
burnt1ce
  • 14,387
  • 33
  • 102
  • 162

5 Answers5

168

Here is a quick start to get the gears turning...

ParkingLot is a class.

ParkingSpace is a class.

ParkingSpace has an Entrance.

Entrance has a location or more specifically, distance from Entrance.

ParkingLotSign is a class.

ParkingLot has a ParkingLotSign.

ParkingLot has a finite number of ParkingSpaces.

HandicappedParkingSpace is a subclass of ParkingSpace.

RegularParkingSpace is a subclass of ParkingSpace.

CompactParkingSpace is a subclass of ParkingSpace.

ParkingLot keeps array of ParkingSpaces, and a separate array of vacant ParkingSpaces in order of distance from its Entrance.

ParkingLotSign can be told to display "full", or "empty", or "blank/normal/partially occupied" by calling .Full(), .Empty() or .Normal()

Parker is a class.

Parker can Park().

Parker can Unpark().

Valet is a subclass of Parker that can call ParkingLot.FindVacantSpaceNearestEntrance(), which returns a ParkingSpace.

Parker has a ParkingSpace.

Parker can call ParkingSpace.Take() and ParkingSpace.Vacate().

Parker calls Entrance.Entering() and Entrance.Exiting() and ParkingSpace notifies ParkingLot when it is taken or vacated so that ParkingLot can determine if it is full or not. If it is newly full or newly empty or newly not full or empty, it should change the ParkingLotSign.Full() or ParkingLotSign.Empty() or ParkingLotSign.Normal().

HandicappedParker could be a subclass of Parker and CompactParker a subclass of Parker and RegularParker a subclass of Parker. (might be overkill, actually.)

In this solution, it is possible that Parker should be renamed to be Car.

Chris Morley
  • 2,426
  • 2
  • 19
  • 20
  • 37
    Please don't forget car. – ojblass Apr 19 '09 at 06:31
  • 5
    Why does ParkingSpace have to be a class? I don't see any need to create an object for it? At all times, any parking space has to be either a Handicapped, Regular or Compact. ParkingSpace should be an interface rather. – name_masked Dec 11 '10 at 18:22
  • 11
    Probably we can add floors to parking lot.. – Barry Nov 05 '11 at 14:06
  • @Chris,objlass: Can you please represent it in UML model. It will give more clarity? – cexplorer Aug 08 '12 at 16:47
  • 14
    Why does ParkingLotSign class exist? Wouldn't an attribute (say, bool isFull;) work? – Chinmay Nerurkar Sep 12 '12 at 16:11
  • 3
    Why make parking spot extensible? Why not just have an isHandicapped field and an isCompact field to the parking spot? – committedandroider Mar 27 '15 at 03:15
  • Can anyone write the code as well ? I am very new to JAVA actually. – Vaibhav Pachauri Nov 12 '16 at 16:41
  • How to find a parker's vehicle in case he forgets the slot he parked in? Any mapping between vehicle and parking space that I'm missing? – gaurav jain Jul 02 '17 at 05:12
  • 1
    The answer is overkill, No need to have different subclasses for ParkingSpace. We need to create subclasses where behaviour is different for every subclass. Adding `ParkingLot.FindVacantSpaceNearestEntrance()` feature is also wrong. We can create a Strategy Interface and make Strategy as an attribute in Parking Lot. in that way we can implement different strategies seamlessly. – Arun Gupta Dec 01 '18 at 06:52
  • How does ParkingSpace notify ParkingLot to update vacant spot count? I'm still learning the basics of OOP – highfivebrian Oct 23 '19 at 17:54
74
public class ParkingLot 
{
    Vector<ParkingSpace> vacantParkingSpaces = null;
    Vector<ParkingSpace> fullParkingSpaces = null;

    int parkingSpaceCount = 0;

    boolean isFull;
    boolean isEmpty;

    ParkingSpace findNearestVacant(ParkingType type)
    {
        Iterator<ParkingSpace> 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<ParkingSpace> 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,
}
  • 7
    Use HashMap instead of lists with vehicle number as key for efficiency – sanath_p Nov 20 '14 at 02:20
  • 6
    After you releaseVehicle, `vacantParkingSpaces` is not sorted anymore. You have to make it sorted so that `findNearestVacant` returns the nearest parking space. – laike9m Aug 03 '15 at 08:21
  • 1
    Why the function is named`findNearestVacant`, when its implementation only finds a vacant-space, not necessarily the "nearest" one? Why not "findVacant"? Though it would have been good to return the "nearest" space, using some states stored in the class. Maybe, we can store the distances from the "entrance" and "exit" in the "space" class so that "nearest" can be computed as well Or we can simply the coordinates of the space, so that the distances from all entrances and exits can be computed on need. – Nawaz Jan 22 '17 at 12:54
  • 2
    Also, the function `parkVehicle` should return a boolean value indicating whether the vehicle was parked or not. – Nawaz Jan 22 '17 at 12:55
  • No null checks. Will throw NPE – hitesh Feb 24 '19 at 15:08
12

Models don't exist in isolation. The structures you'd define for a simulation of cars entering a car park, an embedded system which guides you to a free space, a car parking billing system or for the automated gates/ticket machines usual in car parks are all different.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
8

In an Object Oriented parking lot, there will be no need for attendants because the cars will "know how to park".

Finding a usable car on the lot will be difficult; the most common models will either have all their moving parts exposed as public member variables, or they will be "fully encapsulated" cars with no windows or doors.

The parking spaces in our OO parking lot will not match the size and shape of the cars (an "impediance mismatch" between the spaces and the cars)

License tags on our lot will have a dot between each letter and digit. Handicaped parking will only be available for licenses beginning with "_", and licenses beginning with "m_" will be towed.

Paul Keister
  • 12,851
  • 5
  • 46
  • 75
6

you would need a parking lot, that holds a multi-dimensional array (specified in the constructor) of a type "space". The parking lot can keep track of how many spaces are taken via calls to functions that fill and empty spaces.Space can hold an enumerated type that tells what kind of space it is. Space also has a method taken(). for the valet parking, just find the first space thats open and put the car there. You will also need a Car object to put in the space, that holds whether it is a handicapped, compact, or regular vehicle.


class ParkingLot
{
    Space[][] spaces;

    ParkingLot(wide, long); // constructor

    FindOpenSpace(TypeOfCar); // find first open space where type matches
}

enum TypeOfSpace = {compact, handicapped, regular };
enum TypeOfCar = {compact, handicapped, regular };

class Space
{
    TypeOfSpace type;
    bool empty;
    // gets and sets here
    // make sure car type
}

class car
{
    TypeOfCar type;
}

Scott M.
  • 7,313
  • 30
  • 39