parking lot

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.

 

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;
    }
}

 

转载于:https://www.cnblogs.com/leetcode/p/3570584.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值