您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關java實現租車系統的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
用JAVA編寫了一個租車系統,過程中主要遇到的兩個問題:
1、輸出數組信息問題:
在得到cars[]數組后,要生成租車信息表,目前有兩種思路:一是用循環輸出;二是用Arrays.toString()輸出數組信息。
用toString()方法輸出數組輸出……@……形式的哈希碼地址,這里需要對toString()方法進行重寫,在數組涉及到的類中進行重寫。
不過用第二種方法輸出的其實還是一個數組,形式如圖所示。那么問題來了——還有沒有更好的輸出方法呢?
2、父類方法不能訪問子類成員變量:
本來在父類Car中寫好的getPersonCapacity()和getGoodCapacity()方法似乎不能訪問子類中的personCapacity和goodCapacity 這兩個成員變量,導致調用參數時始終為0;所以在各子類方法中又獨立加上了前面兩個方法,問題得以解決。
運行效果圖:
代碼如下:
package rentCarSys; /* * 總共有三種車型:載人Auto,載貨Van,載人載貨Pickup * Car 為這三種車型的父類 * 有4種屬性: * 編號 = number * 品牌 = brand * 租金/天 = fee * 載人容量 = personCapacity * 載貨容量 = goodCapacity */ public class Car { int number; String brand; double fee; int personCapacity; double goodCapacity; public Car(int number, String brand, double fee){ //構造方法 this.number = number; this.brand = brand; this.fee = fee; } public int getNumber(){ return number; } public String getBrand(){ return brand; } public double getFee(){ return fee; } public int getPersonCapacity(){ return personCapacity; } public double getGoodCapacity(){ return goodCapacity; } }
package rentCarSys; /* * Auto為載人汽車,除了Car中的屬性之外還有載人容量 personCapacity */ public class Auto extends Car{ private int personCapacity; public Auto(int number, String brand, double fee, int personCapacity) { super(number, brand, fee); this.personCapacity = personCapacity; } public int getPersonCapacity() { return personCapacity; } @Override public String toString() { return number + "\t" + brand + "\t" + fee + "元/天\t" + personCapacity + "人\n"; } }
package rentCarSys; /* * Van為載貨汽車,除了Car中的屬性之外還有載貨容量 goodCapacity */ public class Van extends Car{ private double goodCapacity; public Van(int number, String brand, double fee, double goodCapacity) { super(number, brand, fee); this.goodCapacity = goodCapacity; } public double getGoodCapacity(){ return goodCapacity; } public String toString() { return number + "\t" + brand + "\t" + fee + "元/天\t" + goodCapacity + "噸" + "\n"; } }
package rentCarSys; /* * Pickup為載人載貨汽車,除了Car中的屬性之外還有載人容量 personCapacity,載貨容量goodCapacity */ public class Pickup extends Car{ private int personCapacity; private double goodCapacity; public Pickup(int number, String brand, double fee, int personCapacity, double goodCapacity) { super(number, brand, fee); this.personCapacity = personCapacity; this.goodCapacity = goodCapacity; } public int getPersonCapacity() { return personCapacity; } public double getGoodCapacity(){ return goodCapacity; } @Override public String toString() { return number + "\t" + brand + "\t" + fee + "元/天\t" + personCapacity + "人\t" + goodCapacity + "噸\n"; } }
package rentCarSys; import java.util.Arrays; import java.util.Scanner; public class Login { public static void main(String[] args){ Scanner input = new Scanner(System.in); Car[] cars = new Car[6]; System.out.print("歡迎使用答答租車系統:"); System.out.print("您是否要租車?1、是 2、否(請輸入1或2)"); int input1 = input.nextInt(); if (input1 == 1){ System.out.println("下面是所有車的信息:"); cars[0] = new Auto(1, "奧迪A4", 500.0, 4); cars[1] = new Auto(2, "馬自達6", 400.0, 4); cars[2] = new Pickup(3, "皮卡雪6", 450.0, 4, 2); cars[3] = new Auto(4, "金龍", 800.0, 20); cars[4] = new Van(5, "松花江", 400.0, 4); cars[5] = new Van(6, "依維柯", 1000.0, 20); System.out.println("序號\t" + "汽車名稱\t" + "租金\t\t" + "容量(載人/載貨)"); System.out.println(Arrays.toString(cars)); // for(int i = 0; i < cars.length; i++){ // System.out.println("編號:"+ (i+1) +" 品牌:"+ cars[i].getBrand() // +" 租金:"+ cars[i].getFee() +"/天 載客量:"+ cars[i].getPersonCapacity()+"人" // +" 載貨量:"+ cars[i].getGoodCapacity()+"噸" ); // } }else{ System.out.println("謝謝使用,再見!"); } System.out.print("請輸入你要租幾種車:"); int rentNum = input.nextInt(); //selected用來保存客戶選中了什么車型,以及每種車型的輛數,與car數組是對應關系 int[] selected = new int[6]; for (int i = 1; i <= rentNum; i++){ System.out.println("請輸入第" + i + "種車型的序號:" ); int nums = input.nextInt() - 1; System.out.println(cars[nums].getBrand() +"總共需要多少輛:"); int num = input.nextInt(); selected[nums] = num; } System.out.println("請輸入租車天數:"); int daysNum = input.nextInt(); System.out.println("您的賬單:--------------------------"); double total = 0; for (int i = 0; i < cars.length; i++){ if (selected[i] !=0 ){ System.out.println(selected[i] + "輛" + cars[i].getBrand() + " 總共載客量:"+selected[i]*cars[i].getPersonCapacity()+"人"+ " 總共載貨量:"+selected[i]*cars[i].getGoodCapacity()+"噸"+ " "+daysNum+"天單項費用:"+selected[i]*cars[i].getFee()*daysNum+"元"); total += selected[i]*cars[i].getFee()*daysNum; } } System.out.println("租車總費用:" + total + "元" + "\n" + "歡迎下次光臨!------------------------"); } }
關于“java實現租車系統的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。