码迷,mamicode.com
首页 > 其他好文 > 详细

amazon 设计 6 parking lot

时间:2014-08-04 06:13:26      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   io   art   ar   div   

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


public class Cars extends Vehicle{

    @Override
    public boolean park(ParkingLot pLot) {
        if( (pSpace = pLot.allocateFreeSpace(ParkingSpace.ParkingSpaceType.CAR)) ==null)
            return true;
        else
            return false;
    }

    
}



public class Motorbike extends Vehicle{
    @Override
    public boolean park(ParkingLot pLot) {
        if( (pSpace = pLot.allocateFreeSpace(ParkingSpace.ParkingSpaceType.MOTOBIKE)) ==null)
            return true;
        else
            return false;
    }
    
}



public class HandicappedCars extends Vehicle{

    @Override
    public boolean park(ParkingLot pLot) {
        if( (pSpace = pLot.allocateFreeSpace(ParkingSpace.ParkingSpaceType.HANDICAPPED)) ==null)
            return true;
        else
            return false;
    }

}



import java.sql.Time;
import java.util.GregorianCalendar;

public class ParkingSpace {

    public enum ParkingSpaceType
    {
        MOTOBIKE, 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 void setStart()
        {
            meter.start = new GregorianCalendar();
        }
        public void setEnd()
        {
            meter.end = new GregorianCalendar();
        }
        
     public ParkingSpaceType getpSpaceType(){
         return pSpaceType;
     }
     
     private class ParkingMeter{
         public GregorianCalendar start;
         public GregorianCalendar end;
     }
     
      public float getFee()
      {
            return perm.getFee(meter.start, meter.end);
      }

}




import java.util.List;

import parking.ParkingSpace.ParkingSpaceType;

public class ParkingLot {

       private List<ParkingSpace> freeRegularSpace;
       private List<ParkingSpace> freeCompactSpace;
       private List<ParkingSpace> freeHandicappedSpace;
        
       public ParkingLot(){};
       
       public ParkingSpace allocateFreeSpace(ParkingSpaceType pSpaceType) throws Exception{
           ParkingSpace pSpace = null;
           
           switch(pSpaceType)
           {
           case MOTOBIKE:
               if(freeRegularSpace.size()==0)
                   return null;
               pSpace = freeRegularSpace.remove(0);
               pSpace.setStart();
               break;
               
           case HANDICAPPED:
               if(freeHandicappedSpace.size()==0)
                   return null;
               pSpace = freeHandicappedSpace.remove(0);
               pSpace.setStart();
               break;
               
           case CAR:
               if(freeCompactSpace.size()==0)
                   return null;
               pSpace = freeCompactSpace.remove(0);
               pSpace.setStart();
               break;
               
           default:
               throw new Exception("Invalid String");
           }
           
           public float reclainFreeSpace(ParkingSpace Space){
               Space.setEnd();
               switch(Space.getpSpaceType()){
               case MOTOBIKE:
                   freeRegularSpace.add(Space);
                   break;
                   
               case CAR:
                   freeCompactSpace.add(Space);
                   break;
                   
               case HANDICAPPED:
                   freeHandicappedSpace.add(Space);
                   break;
                
               default:
                   break;
               }
               
               return Space.getFee();
               
               
           }
           
           
       }

}

 

amazon 设计 6 parking lot,布布扣,bubuko.com

amazon 设计 6 parking lot

标签:style   blog   color   java   io   art   ar   div   

原文地址:http://www.cnblogs.com/leetcode/p/3889233.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!