做了個差不多。。。 賬單那簡化了一點 不 其實都簡化了點。。。
入口:
package?com.mgh; import?java.util.Scanner; public?class?Initial?{ public?static?void?main(String[]?args)?{ //?TODO?Auto-generated?method?stub ????????Vehicle?vcl[]=new?Vehicle[6]; ????????vcl[0]=new?Car("奧迪A4",1,500,4); ????????vcl[1]=new?Car("馬自達6",2,400,4); ????????vcl[2]=new?Pickup("皮卡學6",3,450,4,2); ????????vcl[3]=new?Car("金龍",4,800,20); ????????vcl[4]=new?Truck("松花江",5,400,4); ????????vcl[5]=new?Truck("依維柯",6,1000,20); ???????? ????????System.out.println("歡迎使用答答租車系統:"); ????????System.out.println("您是否要租車:?1.是?0.否"); ???????? ????????Scanner?scan=new?Scanner(System.in); ????????String?isRent=scan.next(); ????????if(isRent.equals("1")){ ???????? System.out.println("您可租車的類型及其價目表:"); ???????? for(int?i=0;i<5;i++){ ???????? vcl[i].infoShow(); ???????? } ????????????System.out.println("請輸入您要租汽車的數量:"); ????????????int?rentNum=scan.nextInt(); ????????????StringBuffer?selectVclList=new?StringBuffer(); ????????????int?costPerDay=0; ????????????int?selectVclID[]=new?int[rentNum]; ????????????for(int?i=0;i<rentNum;i++){ ???????????? System.out.println("請輸入第"+(i+1)+"輛車的序號:"); ????????????????selectVclID[i]=scan.nextInt()-1; ????????????????selectVclList.append(vcl[selectVclID[i]].name+"?"); ????????????????costPerDay+=vcl[selectVclID[i]].rentPrice; ????????????} ????????????System.out.println("請輸入租車天數:"); ????????????int?rentDays=scan.nextInt(); ????????????int?allCost=costPerDay*rentDays; ????????????String?bill=new?String(); ????????????System.out.println("您的賬單:"); ????????????bill=selectVclList.toString()+"??"+?rentDays?+"天???總價"+allCost+"元"; ???????????? ????????????System.out.println(bill); ????????????scan.close(); ????????} ????????else{ ???????? System.out.println("感謝使用答答租車系統,歡迎再來!"); ????????} } }
父類:
package?com.mgh; public?abstract?class?Vehicle?{ String?name=new?String(); int?rentPrice; int?vehicleNum; public?abstract?void?infoShow(); }
Car子類,客車也在其中了 屬性一樣沒必要另開一類:
package?com.mgh; public?class?Car?extends?Vehicle?{ ????int?menLoad; ????public?Car(String?vName,int?vNum,int?rPrice,int?vmLoad){ ???? this.name=vName; ???? this.vehicleNum=vNum; ???? this.rentPrice=rPrice; ???? this.menLoad=vmLoad; ????} ????@Override ????public?void?infoShow(){ //?TODO?Auto-generated?method?stub ????????System.out.println(this.vehicleNum+".?"+this.name+"???"+this.rentPrice+"元/天????載客:"+this.menLoad+"人"); } }
Truck子類:
package?com.mgh; public?class?Truck?extends?Vehicle?{ ????private?int?cargoLoad; public?Truck(String?vName,int?vNum,int?rPrice,int?vcLoad){ ???? this.name=vName; ???? this.vehicleNum=vNum; ???? this.rentPrice=rPrice; ???? this.cargoLoad=vcLoad; } ????@Override public?void?infoShow()?{ //?TODO?Auto-generated?method?stub ????????System.out.println(this.vehicleNum+".?"+this.name+"???"+this.rentPrice+"元/天????載重:"+this.cargoLoad+"噸"); } }
Pickup子類 皮卡:
package?com.mgh; public?class?Pickup?extends?Vehicle?{ ????private?int?menLoad; ????private?int?cargoLoad; public?Pickup(String?vName,int?vNum,int?rPrice,int?vmLoad,int?vcLoad){ ???? this.name=vName; ???? this.vehicleNum=vNum; ???? this.rentPrice=rPrice; ???? this.cargoLoad=vcLoad; ???? this.menLoad=vmLoad; } ????@Override public?void?infoShow()?{ //?TODO?Auto-generated?method?stub ????????System.out.println(this.vehicleNum+".?"+this.name+"???"+this.rentPrice+"元/天????載客:"+this.menLoad+"人,載重:"+this.cargoLoad+"噸"); } }
2018-06-20
有沒有javae版本的租車系統
2018-04-29
但是賬單里不是應該有乘客數和載貨量的計數嗎
2016-11-28
寫的真好,比我簡略了好多好多。。
2015-03-30
selectVclID[i]=scan.next-1為什么啊,不太懂
2015-02-08
selectVclList.append(vcl[selectVclID[i]].name+"?");
costPerDay+=vcl[selectVclID[i]].rentPrice;
這兩句話是高級語言嗎?沒怎么見過,求樓主科普一下。
2014-11-17
為什么不能實例化呢?
2014-11-02
必須贊,慚愧啊
2014-10-16
挺好的
2014-10-15
還有封裝和各種容錯都沒做?