您好,登錄后才能下訂單哦!
這篇文章給大家介紹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 Length2; public int Length3; public car[] carStark1; //停車場 public car[] carStark2; //用于倒車的臨時棧 //構造方法 public mylist(){ carStark1 =new car[5]; //5個車位的停車場 即棧1 carStark2 =new car[5]; //倒車的臨時棧 車位也為5 即棧2 Length2=0; Length3=0; }//入棧 同時將系統時間壓入 public void push(String value){ car newcar=new car(value); int n=Length2; 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; Length2++; } //由棧2回到棧1 public void push3(){ int n=Length2; carStark1[n]=getTop2(); Length2++; Length3--; } //出棧1 入棧2 public void pop(){ car temp =getTop1(); int n=Length3; carStark2[n]=temp; carStark2[n].count++; Length2--; Length3++; } //出棧2 public void pop3(){ Length3--; } //有時間的出棧1 并且暫時入棧2 public void pop2(){ int n=Length2-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[Length3]=temp; Length2--; Length3++; } //得到棧1頂元素 public car getTop1(){ return carStark1[Length2-1]; } //得到棧2頂元素 public car getTop2(){ return carStark2[Length3-1]; } //停車場里有幾輛車 public int getLength(){ return Length2; } //獲得停車場中第i號車位的車牌號 public String getNum(int i){ return carStark1[i-1].number; }//察看車位情況 public void print(){ int i=0; for(i=0;i<Length2;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.push3(); } } }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.push3(); } 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();}}
關于java中怎么實現一個停車場管理系統就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。