java实现停车场管理系统
本文实例为大家分享了java停车场管理系统的具体代码,供大家参考,具体内容如下
//车的类 public class car{ public String number;//车牌号 public int inhour; //进入小时 public int inminute; //进入分 public int insecond; //进入秒 public int outhour; //开出小时 public int outminute; //开出分 public int outsecond; //开出秒 public int count; //倒车次数 public car link; //队列的指针 //构造方法1 public car(String num){ this.number=num; this.inhour=0; this.inminute=0; this.insecond=0; this.outhour=0; this.outminute=0; this.outsecond=0; int count=0; car link=null; } //构造方法2 public car(){ this.number=""; this.inhour=0; this.inminute=0; this.insecond=0; this.outhour=0; this.outminute=0; this.outsecond=0; int count=0; car link=null; } //成员方法:得到指针 public car getLink(){ return link; } //成员方 法:修改指针 public void setLink(car n){ link=n; } //成员方法;得到车牌号 public String getNum(){ return number; } } ++++++++++++++++++++++ //停车场基于顺序存储结构 栈//停车场基于顺序存储结构 栈 import java.util.Date; public class mylist{ public int Length1; public int Length2; public car[] carStark1; //停车场 public car[] carStark2; //用于倒车的临时栈 //构造方法 public mylist(){ carStark1 =new car[5]; //5个车位的停车场 即栈1 carStark2 =new car[5]; //倒车的临时栈 车位也为5 即栈2 Length1=0; Length2=0; } //入栈 同时将系统时间压入 public void push(String value){ car newcar=new car(value); int n=Length1; carStark1[n]=newcar; Date now = new Date(); //时间类 int hour = now.getHours(); int minute = now.getMinutes(); int second = now.getSeconds(); carStark1[n].inhour=hour; carStark1[n].inminute=minute; carStark1[n].insecond=second; Length1++; } //由栈2回到栈1 public void push2(){ int n=Length1; carStark1[n]=getTop2(); Length1++; Length2--; } //出栈1 入栈2 public void pop(){ car temp =getTop1(); int n=Length2; carStark2[n]=temp; carStark2[n].count++; Length1--; Length2++; } //出栈2 public void pop3(){ Length2--; } //有时间的出栈1 并且暂时入栈2 public void pop2(){ int n=Length1-1; Date now = new Date(); int hour = now.getHours(); int minute = now.getMinutes(); int second = now.getSeconds(); carStark1[n].outhour=hour; carStark1[n].outminute=minute; carStark1[n].outsecond=second; car temp = getTop1(); carStark2[Length2]=temp; Length1--; Length2++; } //得到栈1顶元素 public car getTop1(){ return carStark1[Length1-1]; } //得到栈2顶元素 public car getTop2(){ return carStark2[Length2-1]; } //停车场里有几辆车 public int getLength(){ return Length1; } //获得停车场中第i号车位的车牌号 public String getNum(int i){ return carStark1[i-1].number; } //察看车位情况 public void print(){ int i=0; for(i=0;i<Length1;i++){ System.out.println(i+1+"号车位:"+carStark1[i].number); } } ++++++++++++++++++++++++++++++ //队列基于单向链式存储结构 import java.lang.*; import java.util.*; //队列 public class myQueue { car header; car current; int size; //构造方法:建立一个空表 public myQueue() { header=new car(""); current=header; size = 0; } //把当前位置之后插入一个新结点 public void insertcar(String value){ car newcar=new car(value); newcar.setLink(current.link); current.setLink(newcar); current=newcar; size += 1; System.out.println("*********************************************"); System.out.println("您的车牌号:"+newcar.number); System.out.println("停车场已满,您现在在等待中,您的位置是"+size+"个位置"); System.out.println("*********************************************"); } //删除当前位置结点后面的结点 public void delete(){ car replace; replace = current.link; if (replace == null) return; current.setLink(replace.link); replace=null; size -= 1; } //删除第i个结点 public void delete(int i) { if(i>size || i<=0) return; locate(i-1); delete(); } //由当前结点到下一个结点 public car getNext(){ if(current == null) return null; current=current.link; return current; } //得到队列的左后一辆车 public car getLast() { while (current.link!=null) { getNext(); } return current; } //由车牌号找到车 public car find(String value){ current=header; while(current.number!=value) { if (current.link==null) { System.out.println("找不到需要的数!!!!"); current=header; break; } current=current.link; } return current; } //取得第m 位置的元素 public car locate(int m ){ if (m > size) return null; current=header; int i; for (i=m;i>0;i--) { getNext(); } return current; } //取得第一个元素 public car getHeader() { return header.link; } //队列里有多少车 public int getLength() { return size; } //察看车位 public void print() { current=header.getLink(); while(current!=null){ int i=1; System.out.print(i+"号车位:"+current.getNum()); System.out.println(" "); i++; getNext(); } } } +++++++++++++++++++++++++ //执行程序 import java.io.*; import java.lang.*; public class DemoParking{ public mylist parking; //停车场 public myQueue waiting; //队列 public String x1; //选择 public String x2; //开入的车牌号 public String x3; //开出的车牌号 //构造方法 public DemoParking(){ parking=new mylist(); //初始化栈 waiting=new myQueue(); //初始化队列 x1=""; x2=""; x3=""; } // 成员方法:菜单 public void menu(){ System.out.println(" "); System.out.println(" "); System.out.println(" 欢迎进入停车场系统 "); System.out.println("*********************************************"); System.out.println("* 本停车场每秒收费1元 停车场内有5个车位 *"); System.out.println("* 倒车一次返回2元(最少不收费!) *"); System.out.println("*********************************************"); System.out.println(" 1:开进"); System.out.println(" 2:开出"); System.out.println(" 3:车位情况"); System.out.println(" 4:退出"); //+++++++++++++++++++++++++++ System.out.print("请选择:"); try{ BufferedReader keyin = new BufferedReader( new InputStreamReader(System.in)); x1 = keyin.readLine(); } catch (IOException e) { System.out.print("出错了"); } choice(); } //成员方法:选择 public void choice(){ if(x1.equals("1")){ //输入1 开入 System.out.print("请输入车牌号:"); try{ BufferedReader keyin1 = new BufferedReader( new InputStreamReader(System.in)); x2 =new String( keyin1.readLine()); } catch (IOException e){ System.out.print("出错了"); } in(x2); menu(); } if(x1.equals("2")){ //输入2 开出 System.out.print("请输入车牌号:"); try{ BufferedReader keyin1 = new BufferedReader( new InputStreamReader(System.in)); x3 =new String( keyin1.readLine()); } catch (IOException e){ System.out.print("出错了"); } out(x3); } if(x1.equals("3")){ //输入3 查询 Allprint(); menu(); } if(x1.equals("4")){ //输入4 退出 } else{ //输入不符合规定 返回菜单 menu(); } } //成员方法:开入车 public void in(String value){ if (parking.getLength()<5){ //入栈 parking.push(value); System.out.println("*********************************************"); System.out.println("您的车牌号:"+parking.getTop1().number); System.out.println("进入停车场时间:"+parking.getTop1().inhour+":"+parking.getTop1().inminute+":"+parking.getTop1().insecond); System.out.println("*********************************************"); }else{ waiting.insertcar(value); //入队列 } } //成员方法:开出车 public void out(String value){ int i=parking.getLength(); int b=parking.getLength(); //只有栈里有车 查找到开出 if(waiting.getLength()==0){ while(parking.getTop1().number.equals(value)==false){ parking.pop(); //开始倒车 i--; } parking.pop2(); //开出这辆车 进入栈2 System.out.println("*********************************************"); System.out.println(" 您的车牌号:"+parking.getTop2().number); System.out.println(" 进入时间:"+parking.getTop2().inhour+":"+parking.getTop2().inminute+":"+parking.getTop2().insecond); System.out.println(" 离开时间:"+parking.getTop2().outhour+":"+parking.getTop2().outminute+":"+parking.getTop2().outsecond); System.out.println(" 倒车次数:"+parking.getTop2().count); int s=((parking.getTop2().outhour-parking.getTop2().inhour)*60+parking.getTop2().outminute-parking.getTop2().inminute)*60+parking.getTop2().outsecond-parking.getTop2().insecond-2*parking.getTop2().count; System.out.println(" 您的费用:"+s+"元"); System.out.println(" 欢迎下次再来!@_@再见"); System.out.println("*********************************************"); parking.pop3(); //再从栈2中弹出 if(i==b){ }else{ for(int n=i;n<b;n++){ //将车倒回栈1 parking.push2(); } } }else{ //看在队列还是在栈里 while(parking.getNum(i).equals(value)==false){ //栈 i--; if(i==0){ //检验队列 int a=waiting.getLength(); while(waiting.locate(a).number.equals(value)==false){ a--; } System.out.println("*********************************************"); System.out.println(" 您的车牌号:"+waiting.locate(a).number); System.out.println(" 欢迎下次再来!@_@再见"); System.out.println("*********************************************"); waiting.delete(a); break; } } if(i>0){ //要开出的车在栈里 int c=i; for(;i<b;i++){ parking.pop(); } parking.pop2(); System.out.println("*********************************************"); System.out.println(" 您的车牌号:"+parking.getTop2().number); System.out.println(" 进入时间:"+parking.getTop2().inhour+":"+parking.getTop2().inminute+":"+parking.getTop2().insecond); System.out.println(" 离开时间:"+parking.getTop2().outhour+":"+parking.getTop2().outminute+":"+parking.getTop2().outsecond); System.out.println(" 倒车次数:"+parking.getTop2().count); int s=((parking.getTop2().outhour-parking.getTop2().inhour)*60+parking.getTop2().outminute-parking.getTop2().inminute)*60+parking.getTop2().outsecond-parking.getTop2().insecond-2*parking.getTop2().count; if(s<0){ s=0; //费用最少为0 } System.out.println(" 您的费用:"+s); //利用时间计算费用 System.out.println(" 欢迎下次再来!@_@再见"); System.out.println("*********************************************"); parking.pop3(); for(;c<b;c++){ parking.push2(); } parking.push(waiting.getHeader().number); //队列的第一个元素入栈 waiting.delete(1); //删除队列的第一个元素 } } } //查询车位情况 包括栈中和队列中的 public void Allprint(){ System.out.println("*********************************************"); System.out.println("停车场中:"); parking.print(); if(waiting.getLength()>0){ System.out.println("等待中:"); waiting.print(); } System.out.println("*********************************************"); } public static void main(String[] args)throws IOException{ //应用!! DemoParking demo=new DemoParking(); demo.menu(); } }
更多学习资料请关注专题《管理系统开发》。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)