停车场管理_栈&队列

2.停车场管理

设停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在停车场的最北端),若停车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。试为停车场编制按上述要求进行管理的模拟程序。

package Topic02;


public class Car {
    private String number;
    private String timeArrive;//到达时间
    private String timeLeave;//没用上

    public Car(String number, String timeArrive) {
        this.number = number;
        this.timeArrive = timeArrive;
        this.timeLeave ="";
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getTimeArrive() {
        return timeArrive;
    }

    public void setTimeArrive(String timeArrive) {
        this.timeArrive = timeArrive;
    }

    public String getTimeLeave() {
        return timeLeave;
    }

    public void setTimeLeave(String timeLeave) {
        this.timeLeave = timeLeave;
    }
}

package Topic02;


import java.util.Scanner;

public class Main {
    static final public int PRICE = 10;//每小时十块钱
    static public final Stack<Car> PARKINGLOT = new Stack<>();//停车场
    static public final Stack<Car> REVERSE = new Stack<>();//从停车场出去的车
    static public final Queue<Car> ROOD = new Queue<>();//便道

    static final public int CAPACITY = 2;
    public static final Scanner SCANNER = new Scanner(System.in);

    static public void carEnter() {//车辆进入停车场
        System.out.println("输入车牌号:");
        String number = SCANNER.next();
        System.out.println("输入到来时间(xx:xx):");
        String arriveTime = SCANNER.next();
        Car car = new Car(number, arriveTime);
        //直接开进来
        if (PARKINGLOT.size() < CAPACITY) {
            PARKINGLOT.push(car);
            System.out.println("汽车所在停车场位置:" + PARKINGLOT.size());
        } else {//在外面等
            ROOD.enqueue(car);
            System.out.println("汽车所在便道位置:" + ROOD.size());
        }
    }

    static public void carLeave() {//车辆离开停车场
        System.out.println("输入车牌号:");
        String number = SCANNER.next();//车牌号
        while (true) {//车辆依次离开停车场
            Car car = PARKINGLOT.pop();
            if (car == null) {//没有这个车牌的车
                while (!REVERSE.isEmpty()) {

                    PARKINGLOT.push( REVERSE.pop());
                }
                System.err.println("没有该车");
                break;
            }
            if (car.getNumber().equals(number)) {//取出对应车牌的车
                System.out.println("输入离开时间");
                String leaveTime = SCANNER.next();
                System.out.println(processTime(car.getTimeArrive(), leaveTime));
                while (!REVERSE.isEmpty()) {
                    Car car1 = REVERSE.pop();
                    PARKINGLOT.push(car1);
                }
                if (!ROOD.isEmpty()) {
                    Car car1 = ROOD.dequeue();
                    car1.setTimeArrive(leaveTime);
                    PARKINGLOT.push(car1);
                    System.out.println("车牌:" + car1.getNumber() +"在"+car1.getTimeArrive() +
                            "时进入停车场,汽车所在停车场位置:" + PARKINGLOT.size());
                }
                break;
            } else {
                REVERSE.push(car);
            }
        }
    }

    static String processTime(String begin, String end) {//计算花了多少钱
        String[] b = begin.split(":");
        String[] e = end.split(":");
        Integer bh = Integer.parseInt(b[0]);
        int bm = Integer.parseInt(b[1]);
        Integer eh = Integer.parseInt(e[0]);
        int em = Integer.parseInt(e[1]);
        double times = (eh - bh) * 60 + em - bm;
        double price = (times / 60) * PRICE;
        return "时间为:" + times + "分钟  价格为:" + price + "元";
    }

    public static void main(String[] args) {
        System.out.println("停车场管理系统");
        while (true) {

            System.out.print("1.车辆进入");
            System.out.print("  2.车辆离开");
            System.out.print("  3.程序结束");
            System.out.println();
            int option = SCANNER.nextInt();
            switch (option) {
                case 1:
                    carEnter();
                    break;
                case 2:
                    carLeave();
                    break;
                default:
                    System.exit(0);
            }
        }
    }
}
package Topic02;


public class Queue<T> {
    private  static class Node<T>{//节点
        private T item;
        private Node next;
        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
    /*
    进入的一段为head,出去的一端为last
     */
    private Node head;
    private Node last;
    private int N;

    public Queue() {
        head=null;
        last=new Node(null, null);


        N=0;
    }
    public boolean isEmpty(){//是否为空
        return N==0;
    }
    public int size(){//队列容量
        return N;

    }
    /*
    添加元素
     */
    public void enqueue(T t){//入队

        if(head==null){
            head=new Node(t,null);
            last.next=head;
        }else{

            Node<T> temp=new Node<>(t,null);
            head.next=temp;
            head=temp;
        }
        N++;

    }
    public T dequeue(){//出队
        if(isEmpty()){
            return null;
        }else{
            Node<T> temp=last.next;
            last=last.next;
            N--;
            return temp.item;

        }
    }
}

package Topic02;


public class Stack<T> {
    private static class Node<T>{//节点
        private T item;
        private Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
    private Node head;//头节点
    private int N;

    public Stack() {
        head= new Node(null, null);
        N=0;
    }
    public boolean isEmpty(){//判断是否为空
        return N==0;
    }
    public void push(T t){//进栈
        Node<T> oldHead=head;
        head= new Node<T>(t, oldHead);
        N++;
    }
    public T pop(){//出栈
        if(!isEmpty()){
            N--;
            Node<T> oldHead=head;
            head=head.next;
            return oldHead.item;
        }else{
            return null;
        }
    }
    public int size()//栈的容量
    {
        return N;
    }

}

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值