[问题描述]:设有一个可以停放n辆汽车的狭长停车场,它只有一个大门可以供车辆进出。车辆按到达停车场时间的早晚依次从停车场最里面向大门口处停放(最先到达的第一辆车放在停车场的最里面)。如果停车场已放满n辆车,则后来的车辆只能在停车场大门外的便道上等待,一旦停车场内有车开走,则排在便道上的第一辆车就进入停车场。停车场内如有某辆车要开走,在它之后进入停车场的车都必须先退出停车场为它让路,待其开出停车场后,这些车辆再依原来的次序进场。每辆车在离开停车场时,都应根据它在停车场内停留的时间长短交费。如果停留在便道上的车未进停车场就要离去,允许其离去,不收停车费,并且仍然保持在便道上等待的车辆的次序。
[实现要求]:要求程序输出每辆车到达后的停车位置(停车场或便道上),以及某辆车离开停车场时应缴纳的费用和它在停车场内停留的时间。汽车的模拟输入格式可以是:(到达/离去,汽车牌照号,到达/离去的时刻)。例如,(‘A’,1,5)表示1号牌照车在5这个时刻到达,而(‘D’,5,20)表示5号牌照车在20这个时刻离去。整个程序可以在输入信息为(‘E’,0,0)时结束。
所需要的数据结构,停车场采用栈结构,停车场外的便道采用队列结构
值得注意的是,栈的特点是“先进后出,后进先出”,所以停车场里面的车要离开时,在它之后进入的车要为它让道,这就必须要在建一个新的栈用于存放让道的车
具体实现代码
汽车类,定义汽车拥有的属性
package DesignParking;
public class CarInfo{
public int state;
public int atime;
public int dtime;
public String license;
}
栈类,定义元素进栈、出栈方法以及输出栈的长度方法
package DesignParking;
public class Stack {
private CarInfo S[];
private int top;
public Stack(int maxSize) {
top = -1;
S = new CarInfo[maxSize];
}
public void push(CarInfo x) throws Exception {
if(S.length==top+1){
throw new Exception("栈已满");
}
else{
top++;
S[top]=x;
}
}
public CarInfo pop() {
if(top==-1){
return null;
}
else{
return S[top--];
}
}
public int gettop() {
return top+1;
}
}
队列类,定义入队、出队方法以及输出队中元素个数的方法
package DesignParking;
public class Queue<CarInfo> {
private static final int QUEUE_CAPACITY = 1000;
private Object[] elementData;
private int rear;
private int front;
public Queue(){
this.elementData=new Object[QUEUE_CAPACITY];
this.rear=-1;
this.front=-1;
}
public void push(CarInfo data) {
if(this.rear + 1>= QUEUE_CAPACITY){
throw new RuntimeException("队列已满,入队失败!");
}
++rear;
this.elementData[rear]=data;
}
public CarInfo pop(){
if(isEmpty()){
throw new RuntimeException("队列为空,出队失败!");
}
++front;
return (CarInfo) this.elementData[front];
}
public boolean isEmpty() {
return this.front==this.rear;
}
public int length(){
return rear-front;
}
}
实现类
package DesignParking;
import java.util.Scanner;
public class ParkingManagement{
private Stack S=new Stack(5);
private Queue<CarInfo> Q = new Queue<CarInfo>();
private double fee = 2;
public final static int DEPARTURE = 0;
public final static int ARRIVAL=1;
//停车场管理,license表示车牌号,action表示车辆的动作,到达或离开
public void parkingManage(String license,String action,int time) throws Exception{
if("arrive".equals(action)){
CarInfo info=new CarInfo();
info.license=license;
if(S.gettop()<5){
info.atime=time;
info.state=ARRIVAL;
S.push(info);
System.out.println("车牌号为"+info.license+"的车于"+time+"时刻停放在停车场第"+S.gettop()+"个位置!");
}
else {
Q.push(info);
System.out.println("车牌号为"+info.license+"的车停放在便道第"+Q.length()+"个位置!");
}
}
else if("depart".equals(action)){
CarInfo info=null;
int location=0;//用于记录车辆位置
//构造一个新栈存储因车辆离开而导致的其他车辆暂时退出停车场
Stack S2=new Stack(S.gettop());
for(int i=S.gettop();i>0;i--){
info=(CarInfo)S.pop();
if(info.license.equals(license)){
info.dtime=time;
info.state=DEPARTURE;
location=i;
break;
}
else
S2.push(info);//其他车辆暂时退出车场
}
while(S2.gettop()!=0){
//其他车辆重新进入车场
S.push(S2.pop());
}
if(location!=0){//停车场内存在指定车牌号的车辆
//计算停放时间,并把毫秒换成分钟
int stime=info.dtime-info.atime;
System.out.println("车牌号为"+info.license+"的车停放"+stime+"分钟,费用为"+(stime*fee));
}
if(!Q.isEmpty()){
info=(CarInfo)Q.pop();
info.atime=time;
info.state=ARRIVAL;
S.push(info);
}
}
}
public static void main(String[] args) throws Exception {
ParkingManagement plms=new ParkingManagement();
Scanner sc=new Scanner(System.in);
System.out.println("欢迎使用停车场管理系统!!提示表示arrive进入停车场,depart表示离开停车场,exit+“其他”+“其他”表示退出系统");
System.out.println("提示该停车场只能停放5辆车!!");
System.out.println("-----------------------------------------------------------------------------------");
while(true){
System.out.println("请输入arrive or depart or exit、车牌号和此时刻,中间用空格间隔");
String action=sc.next();
String license=sc.next();
int time=sc.nextInt();
plms.parkingManage(license, action,time);
if("exit".equals(action)){
System.out.println("成功退出系统!!");
break;
}
}
}
}
运行结果