做好了,哈哈,分享給大家參考下,有些我沒有去封裝好!
package com.dome;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/*
?* auther:yeshili
?* qq:583870616
?* 初學者
?* */
public class test {
static List<Car> clist = new ArrayList<Car>();
//匿名內部類
public class Car{
String code;//編號
String name;//名稱
int money;//每日租金
int passenger;//載客認人數
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("歡迎使用達達租車系統");
@SuppressWarnings("resource")
Scanner in= new Scanner(System.in);
System.out.println("你是否要使用租車系統: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","東風",2000,2,10);
? ?Car c4 = t.new Car("4","時風",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("你可租車的類型以及價目表:");
? ?System.out.println("序號 ? ?汽車名稱 ? ?租金 ? ?容量");
? ?for(Car temp:clist){
? ? if(temp.cargo==0){
? ? temp.printInfo();
? ? }else{
? ? temp.printResInfo();
? ? }
}
? ?System.out.println("請你輸入你要租憑的車輛:");
? ?//累計各項數值
? ?int passNum = 0;//載客總人數
? ?int goNum = 0;//載貨總噸位
? ?int carNum = 0;//租車總數量
? ?int rentNum = 0;//租賃總天數
? ?String arrRenName="";//計算可載人車輛
? ?String arrHuoName="";//計算可載貨車輛
? ?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)+"輛車的序號");
? }else{
? System.out.println("請輸入租車天數");
? int is2= in.nextInt();
? rentNum=is2;
? }
? ? }else{
? ? i--;
? ? System.out.println("輸入錯誤,請從新輸入輛車的序號");
? ? }
? ? }
? ? 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("租車總價格:"+(total*rentNum)+" ? 租車總數量:"+carNum+" ? 租車總天數:"+rentNum);
}else{
System.out.println("參數錯誤");
}
}
}
2015-12-25
簡單的說幾點:
????1、二季的最后一節里有提到過,最好能分成多個類來實現,Car是父類,子類分為:載人的車,載貨的車,既能載人又能載貨的車。
? 2、提示用戶輸入是否要租車的時候,用戶輸入的不是0或1怎么處理,直接就死掉了。這個地方需要你再想想(還有客戶輸入要租賃的車的編號的時候也有這個問題,輸入的編號沒有對應的車)。
? 3、這個可能是我個人的想法,也不知道對不對,共同學習。我覺得,這些車的對象不是說你不來租車就沒有的,無論這個客戶是不是來租車的,這些車都是存在的,所以車的這些對象應當在詢問客戶時候要租車前就應該準備好的。
。。。。。隨著我們的學習肯定還會發現更多的問題,慢慢完善