美文网首页
设计停车场系统

设计停车场系统

作者: 放开那个BUG | 来源:发表于2022-05-01 15:47 被阅读0次

    1、前言

    停车场系统也是一个经典的管理系统,主体是停车场系统,input 是车辆,output 是车位。所以有一个 Vehicle 作为公共父类,有 Bus、Motobike、Car 等一些公共子类。有一个 ParkingLot(停车场),和一个 ParkingSpot(停车位)。

    2、设计

    在此基础上,我们假设是分楼层的那种停车场,那么会有一个 Level,Level 里会有很多 ParkingSpot,而 ParkingLot 持有 Level。

    所以 ParkingLot 最主要的就是两个方法,parkVehicle(Vehicle vehicle) 和 leaveVehicle(Ticket ticket),当车走的时候,ParkingLot 进行一下计费。当然,他的停车或者离开最终而言是调用 ParkingSpot 的方法,毕竟它才是本质上提供功能的类。

    它们的类图关系如下:


    类图

    3、代码

    Vehicle:

    public class Vehicle {
    
        private SizeType type;
    
        public Vehicle(SizeType type) {
            this.type = type;
        }
    
        public SizeType getType() {
            return type;
        }
    
        public void setType(SizeType type) {
            this.type = type;
        }
    }
    

    Ticket:

    public class Ticket {
    
        /**
         * 停了哪一辆车
         */
        private Vehicle vehicle;
    
        /**
         * 开始停车时间
         */
        private Date startTime;
    
        public Ticket(Vehicle vehicle, Date startTime) {
            this.vehicle = vehicle;
            this.startTime = startTime;
        }
    
        public Vehicle getVehicle() {
            return vehicle;
        }
    
        public void setVehicle(Vehicle vehicle) {
            this.vehicle = vehicle;
        }
    
        public Date getStartTime() {
            return startTime;
        }
    
        public void setStartTime(Date startTime) {
            this.startTime = startTime;
        }
    }
    
    

    SizeType:

    public enum  SizeType {
        SMALL,
        MEDIUM,
        LARGE;
    
        public static SizeType random(){
            Random random = new Random();
            return SizeType.values()[random.nextInt(3)];
        }
    }
    

    ParkingLot:

    public class ParkingLot {
    
        /**
         * 有哪些层
         */
        private List<Level> levels;
    
        /**
         * 每毫秒收费
         */
        private float costOneMS = 5;
    
    
        public ParkingLot() {
            this.levels = new ArrayList<>();
            for(int i = 1; i <= 1; i++){
                Level level = new Level(new ArrayList<>(), 4, i);
                for(int j = 1; j <= level.getAvaliableCount(); j++){
                    level.getParkingSpots().add(new ParkingSpot(SizeType.random(), level, true));
                }
                this.levels.add(level);
            }
        }
    
        public boolean canVehicleFit(Vehicle vehicle){
            for (Level level : this.levels) {
                if(level.canVehicleFit(vehicle)){
                    return true;
                }
            }
            return false;
        }
    
        public Ticket parkVehicle(Vehicle vehicle){
            for (Level level : this.levels) {
                if(level.parkVehicle(vehicle)){
                    return new Ticket(vehicle, new Date());
                }
            }
            return null;
        }
    
        public void leaveVehicle(Ticket ticket){
            for (Level level : this.levels) {
                if(level.unParkVehicle(ticket.getVehicle())){
                    calculatePrice(ticket);
                    return;
                }
            }
        }
    
        private void calculatePrice(Ticket ticket) {
            System.out.println("花费了:" + (System.currentTimeMillis() - ticket.getStartTime().getTime()) * this.costOneMS);
        }
    
        public static void main(String[] args) {
            ParkingLot parkingLot = new ParkingLot();
            Ticket ticket = null;
            for(int i = 0; i < 10; i++){
                Vehicle bus = new Bus();
                boolean isFit = parkingLot.canVehicleFit(bus);
                if(isFit){
                    System.out.println("可以停");
                    ticket = parkingLot.parkVehicle(bus);
                }else {
                    System.out.println("停不了");
                }
            }
    
            if(ticket != null){
                parkingLot.leaveVehicle(ticket);
            }
        }
    }
    

    Level:

    public class Level {
    
        /**
         * 每层的停车位数量
         */
        private List<ParkingSpot> parkingSpots;
    
        /**
         * 可用的停车位数量
         */
        private int avaliableCount;
    
        /**
         * 第几层
         */
        private int levelNo;
    
        public Level(List<ParkingSpot> parkingSpots, int avaliableCount, int levelNo) {
            this.parkingSpots = parkingSpots;
            this.avaliableCount = avaliableCount;
            this.levelNo = levelNo;
        }
    
    
        public boolean canVehicleFit(Vehicle vehicle) {
            if(this.avaliableCount == 0){
                return false;
            }
            for (ParkingSpot parkingSpot : this.parkingSpots) {
                if(parkingSpot.canVehicleFit(vehicle)){
                    return true;
                }
            }
            return false;
        }
    
        public boolean parkVehicle(Vehicle vehicle) {
            for (ParkingSpot parkingSpot : this.parkingSpots) {
                if(parkingSpot.canVehicleFit(vehicle)){
                    parkingSpot.takeSpot(vehicle);
                    this.avaliableCount--;
                    return true;
                }
            }
            return false;
        }
    
        public boolean unParkVehicle(Vehicle vehicle){
            for (ParkingSpot parkingSpot : this.parkingSpots) {
                if(parkingSpot.findVehicle(vehicle)){
                    this.avaliableCount++;
                    parkingSpot.leavelSpot(vehicle);
                    return true;
                }
            }
            return false;
        }
    
    
        public List<ParkingSpot> getParkingSpots() {
            return parkingSpots;
        }
    
        public void setParkingSpots(List<ParkingSpot> parkingSpots) {
            this.parkingSpots = parkingSpots;
        }
    
        public int getAvaliableCount() {
            return avaliableCount;
        }
    
        public void setAvaliableCount(int avaliableCount) {
            this.avaliableCount = avaliableCount;
        }
    
        public int getLevelNo() {
            return levelNo;
        }
    
        public void setLevelNo(int levelNo) {
            this.levelNo = levelNo;
        }
    }
    
    

    ParkingSpot:

    /**
     * @author xushu
     * @create 5/1/22 1:44 PM
     * @desc 如果是有几层的那种停车楼,那么停车场持有的就是 Level 的引用,否则直接持有 ParkingSpot 的引用就行
     */
    public class ParkingSpot {
    
        /**
         * 车位类型
         */
        private SizeType type;
    
        /**
         * 停了什么车
         */
        private Vehicle vehicle;
    
    
        /**
         * 在哪个楼层
         */
        private Level level;
    
    
        /**
         * 是否可用
         */
        private boolean avaliable;
    
    
        public ParkingSpot(SizeType type, Level level, boolean avaliable) {
            this.type = type;
            this.level = level;
            this.avaliable = avaliable;
        }
    
    
        public boolean canVehicleFit(Vehicle vehicle) {
            return avaliable && this.type.equals(vehicle.getType());
        }
    
    
        public void takeSpot(Vehicle vehicle){
            this.avaliable = false;
            this.vehicle = vehicle;
        }
    
        public void leavelSpot(Vehicle vehicle){
            this.avaliable = true;
            this.vehicle = null;
        }
    
        public boolean findVehicle(Vehicle vehicle){
            return !avaliable && this.vehicle.equals(vehicle);
        }
    
    
        public SizeType getType() {
            return type;
        }
    
        public void setType(SizeType type) {
            this.type = type;
        }
    
        public Vehicle getVehicle() {
            return vehicle;
        }
    
        public void setVehicle(Vehicle vehicle) {
            this.vehicle = vehicle;
        }
    
        public Level getLevel() {
            return level;
        }
    
        public void setLevel(Level level) {
            this.level = level;
        }
    }
    

    相关文章

      网友评论

          本文标题:设计停车场系统

          本文链接:https://www.haomeiwen.com/subject/oysjyrtx.html