首页 > 其他 > 详细

parking lot

时间:2014-02-27 23:15:21      阅读:605      评论:0      收藏:0      [点我收藏+]

We follow the four steps in the design principle:

FUNCTION

There are three types of parking spaces: motorbike, car and handicapped car. Motorbikes and cars can only be parked in their designated parking spaces, while handicapped cars can be parked either in their own parks or those for regular cars.

Any car can only park to a space for its type available in the parking lot. Available parking spaces of each type are maintained in the parking lot object.

Each parking space has a permission system: either paying to park or free park.

OBJECTS

ParkingLot is a class
ParkingSpace is a class
ParkingLot has a finite number of ParkingSpace objects

No need to have subclasses for each type of ParkingSpace because they share the same operations as the generic ParkingSpace. The type is identified in terms of a member variable in ParkingSpace objects.

Class Vehicle and its subclasses implement each type of vehicles.

An interface Permission represents a generic payment system. Permission is  implemented as PaidPermission (pay to park) and FreePermission (free park).

An non-static inner class (Java) ParkingMeter in each ParkingSpace to record start and end time of a parking session.

RESPONSIBILIES

ParkingLot maintains arrays of vacant parking spaces, each for one type of spaces. No need to record occupied information separately in each ParkingSpace instance.

Define the class Vehicle to provide the method park for common parking operation, each subclass implements the vehicle interface to park into a parking space of its type.

ParkingLot has no operation besides accessor and mutator, it‘s used as a storage to maintain available parking spaces of each type. allocateFreeSpace() andreclaimFreeSpace() are responsible for allocating and reclaiming parking spaces. Readings of ParkingMeters in each ParkingSpace and Permission in ParkingLot are applied in these two methods to get parking fees. They are called by Vehicle::park() and Vehicle::unpark() methods.

 

bubuko.com,布布扣
public interface Permission{
    float getFee(Calendar start, Calendar end);
}

class ParkingLot{

    private List<ParkingSpace> freeMotorSpace;
    private List<ParkingSpace> freeHandicappedSpace;
    private List<ParkingSpace> freeCompactSpace;
    public ParkingLot();

    public ParkingSpace allocateFreeSpace(ParkingSpaceType pspaceType)
    {
        //get a ParkingSpace from the corresponding free list
        pspace.setStart();
        return pspace;
    }

    public float reclaimFreeSpace(ParkingSpace pspace){

        pspace.setEnd();

        //return free space to the list
        return pspace.getFee(perm);
    }

    private Permission perm;
}

class ParkingSpace
{
    enum ParkingSpaceType
    {
        MOTORBIKE, CAR, HANDICAPPED;
    }

    private int id;
    private ParkingSpaceType pspaceType;
    private ParkingMeter meter;

    public ParkingSpace(int id, ParkingSpaceType pspaceType)
    {
        super();
        this.id = id;
        this.pspaceType = pspaceType;
    }

    public int getId()
    {
        return id;
    }

    public ParkingSpaceType getpspaceType()
    {
        return pspaceType;
    }

    private class ParkingMeter{
        public GregorianCalendar start;
        public GregorianCalendar end;
    }

    public void setStart()
    {
        meter.start = new GregorianCalendar();
    }
    public void setEnd()
    {
        meter.end = new GregorianCalendar();
    }

    public float getFee(Permision perm)
    {
        return perm.getFee(meter.start, meter.end);
    }
}

public abstract class Vehicle{
    public boolean park(ParkingLot pLot);
    public boolean unpark(ParkingLot pLot){
        if(pspace != null){
            pLot.reclaimFreeSpace(pspace);
            return true;
        }
        return false;
    };
    private ParkingSpace pspace;
}

public class Motorbike extends Vehicle{
    public boolean park(ParkingLot pLot){
        if((pspace=pLot.allocateFreeSpace(ParkingSpaceType.MOTORBIKE))==null){
            return false;
        }
        return true;
    }
}

public class Cars extends Vehicle{
    public boolean park(ParkingLot pLot){
        if((pspace=pLot.allocateFreeSpace(ParkingSpaceType.CAR))==null){
            return false;
        }
        return true;
    }
}

public class HandicappedCars extends Vehicle{
    public boolean park(ParkingLot pLot){
        if((pspace=pLot.allocateFreeSpace(ParkingSpaceType.HANDICAPPED))==null && (pspace=pLot.getfreeSpace(ParkingSpaceType.CAR))==null){
            return false;
        }
        return true;
    }
}
bubuko.com,布布扣

parking lot,布布扣,bubuko.com

parking lot

原文:http://www.cnblogs.com/leetcode/p/3570584.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!