溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java實現(xiàn)租車系統(tǒng)的示例分析

發(fā)布時間:2021-04-15 11:43:53 來源:億速云 閱讀:218 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關java實現(xiàn)租車系統(tǒng)的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

用JAVA編寫了一個租車系統(tǒng),過程中主要遇到的兩個問題:

1、輸出數(shù)組信息問題:

在得到cars[]數(shù)組后,要生成租車信息表,目前有兩種思路:一是用循環(huán)輸出;二是用Arrays.toString()輸出數(shù)組信息。

用toString()方法輸出數(shù)組輸出……@……形式的哈希碼地址,這里需要對toString()方法進行重寫,在數(shù)組涉及到的類中進行重寫。

不過用第二種方法輸出的其實還是一個數(shù)組,形式如圖所示。那么問題來了——還有沒有更好的輸出方法呢?

2、父類方法不能訪問子類成員變量:

本來在父類Car中寫好的getPersonCapacity()和getGoodCapacity()方法似乎不能訪問子類中的personCapacity和goodCapacity 這兩個成員變量,導致調(diào)用參數(shù)時始終為0;所以在各子類方法中又獨立加上了前面兩個方法,問題得以解決。

運行效果圖:

java實現(xiàn)租車系統(tǒng)的示例分析

代碼如下:

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){ //構(gòu)造方法
 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("歡迎使用答答租車系統(tǒng):");
 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用來保存客戶選中了什么車型,以及每種車型的輛數(shù),與car數(shù)組是對應關系
 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("請輸入租車天數(shù):");
 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實現(xiàn)租車系統(tǒng)的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI