做好了,哈哈,分享給大家參考下,有些我沒有去封裝好!
package com.dome;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/*
?* auther:yeshili
?* qq:583870616
?* 初學(xué)者
?* */
public class test {
static List<Car> clist = new ArrayList<Car>();
//匿名內(nèi)部類
public class Car{
String code;//編號(hào)
String name;//名稱
int money;//每日租金
int passenger;//載客認(rèn)人數(shù)
int cargo;//載貨噸位
public Car(String code,String name,int money,int passenger,int cargo){
this.code=code;
this.name=name;
this.money=money;
this.passenger=passenger;
this.cargo=cargo;
}
public void printInfo(){
System.out.println(this.code+" ? ?"+this.name+" ? ?"+this.money+"元 ? ? "+this.passenger);
}
public void printResInfo(){
System.out.println(this.code+" ? ?"+this.name+" ? ?"+this.money+"元/天 ? ? 載人"+this.passenger+" 載貨"+this.cargo);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("歡迎使用達(dá)達(dá)租車系統(tǒng)");
@SuppressWarnings("resource")
Scanner in= new Scanner(System.in);
System.out.println("你是否要使用租車系統(tǒng):1是 ?0否");
int is0=in.nextInt();
if(is0==1){
test t= new test();
Car c1 = t.new Car("1","寶馬X1",1000,4,0);
? ?Car c2 = t.new Car("2","奔馳500",1500,4,0);
? ?Car c3 = t.new Car("3","東風(fēng)",2000,2,10);
? ?Car c4 = t.new Car("4","時(shí)風(fēng)",500,2,2);
? ?Car c5 = t.new Car("5","松花江",800,4,20);
? ?Car c6 = t.new Car("6","金龍",6000,20,0);
? ?clist.add(c1); clist.add(c2); clist.add(c3);
? ?clist.add(c4); clist.add(c5); clist.add(c6);
? ?System.out.println("你可租車的類型以及價(jià)目表:");
? ?System.out.println("序號(hào) ? ?汽車名稱 ? ?租金 ? ?容量");
? ?for(Car temp:clist){
? ? if(temp.cargo==0){
? ? temp.printInfo();
? ? }else{
? ? temp.printResInfo();
? ? }
}
? ?System.out.println("請你輸入你要租憑的車輛:");
? ?//累計(jì)各項(xiàng)數(shù)值
? ?int passNum = 0;//載客總?cè)藬?shù)
? ?int goNum = 0;//載貨總噸位
? ?int carNum = 0;//租車總數(shù)量
? ?int rentNum = 0;//租賃總天數(shù)
? ?String arrRenName="";//計(jì)算可載人車輛
? ?String arrHuoName="";//計(jì)算可載貨車輛
? ?double total = 0.0;//租金總額
? ? for(int i=1;i<=3;i++){
? ? int is1= in.nextInt();
? ? if(is1>0 && is1<=6){
? ? Car a=clist.get((is1-1));
? arrRenName=arrRenName+" ?"+a.name;
? passNum+=a.passenger;
? goNum+=a.cargo;
? total+=a.money;
? carNum++;
? if(a.cargo!=0) arrHuoName=arrHuoName+" ?"+a.name;
? if(i<3){
? ? System.out.println("請輸入第"+(i+1)+"輛車的序號(hào)");
? }else{
? System.out.println("請輸入租車天數(shù)");
? int is2= in.nextInt();
? rentNum=is2;
? }
? ? }else{
? ? i--;
? ? System.out.println("輸入錯(cuò)誤,請從新輸入輛車的序號(hào)");
? ? }
? ? }
? ? System.out.println("你的賬單:");
? ? System.out.println("可載人的車輛有:");
? ? System.out.println(arrRenName+" ? 共載人:"+passNum+"人");
? ? if(goNum!=0){
? ? System.out.println("可載貨的車輛有:");
? ? System.out.println(arrHuoName+" ? 共載貨:"+goNum+"噸");
? ? }
? ? System.out.println("租車總價(jià)格:"+(total*rentNum)+" ? 租車總數(shù)量:"+carNum+" ? 租車總天數(shù):"+rentNum);
}else{
System.out.println("參數(shù)錯(cuò)誤");
}
}
}
2015-12-25
簡單的說幾點(diǎn):
????1、二季的最后一節(jié)里有提到過,最好能分成多個(gè)類來實(shí)現(xiàn),Car是父類,子類分為:載人的車,載貨的車,既能載人又能載貨的車。
? 2、提示用戶輸入是否要租車的時(shí)候,用戶輸入的不是0或1怎么處理,直接就死掉了。這個(gè)地方需要你再想想(還有客戶輸入要租賃的車的編號(hào)的時(shí)候也有這個(gè)問題,輸入的編號(hào)沒有對(duì)應(yīng)的車)。
? 3、這個(gè)可能是我個(gè)人的想法,也不知道對(duì)不對(duì),共同學(xué)習(xí)。我覺得,這些車的對(duì)象不是說你不來租車就沒有的,無論這個(gè)客戶是不是來租車的,這些車都是存在的,所以車的這些對(duì)象應(yīng)當(dāng)在詢問客戶時(shí)候要租車前就應(yīng)該準(zhǔn)備好的。
。。。。。隨著我們的學(xué)習(xí)肯定還會(huì)發(fā)現(xiàn)更多的問題,慢慢完善