這篇文章將為大家詳細(xì)講解有關(guān)java實(shí)現(xiàn)租車(chē)系統(tǒng)的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
在張家口等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專(zhuān)注、極致的服務(wù)理念,為客戶(hù)提供做網(wǎng)站、成都做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需定制制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),營(yíng)銷(xiāo)型網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站建設(shè),張家口網(wǎng)站建設(shè)費(fèi)用合理。
用JAVA編寫(xiě)了一個(gè)租車(chē)系統(tǒng),過(guò)程中主要遇到的兩個(gè)問(wèn)題:
1、輸出數(shù)組信息問(wèn)題:
在得到cars[]數(shù)組后,要生成租車(chē)信息表,目前有兩種思路:一是用循環(huán)輸出;二是用Arrays.toString()輸出數(shù)組信息。
用toString()方法輸出數(shù)組輸出……@……形式的哈希碼地址,這里需要對(duì)toString()方法進(jìn)行重寫(xiě),在數(shù)組涉及到的類(lèi)中進(jìn)行重寫(xiě)。
不過(guò)用第二種方法輸出的其實(shí)還是一個(gè)數(shù)組,形式如圖所示。那么問(wèn)題來(lái)了——還有沒(méi)有更好的輸出方法呢?
2、父類(lèi)方法不能訪(fǎng)問(wèn)子類(lèi)成員變量:
本來(lái)在父類(lèi)Car中寫(xiě)好的getPersonCapacity()和getGoodCapacity()方法似乎不能訪(fǎng)問(wèn)子類(lèi)中的personCapacity和goodCapacity 這兩個(gè)成員變量,導(dǎo)致調(diào)用參數(shù)時(shí)始終為0;所以在各子類(lèi)方法中又獨(dú)立加上了前面兩個(gè)方法,問(wèn)題得以解決。
運(yùn)行效果圖:
代碼如下:
package rentCarSys; /* * 總共有三種車(chē)型:載人Auto,載貨Van,載人載貨Pickup * Car 為這三種車(chē)型的父類(lèi) * 有4種屬性: * 編號(hào) = 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為載人汽車(chē),除了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為載貨汽車(chē),除了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為載人載貨汽車(chē),除了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("歡迎使用答答租車(chē)系統(tǒng):"); System.out.print("您是否要租車(chē)?1、是 2、否(請(qǐng)輸入1或2)"); int input1 = input.nextInt(); if (input1 == 1){ System.out.println("下面是所有車(chē)的信息:"); cars[0] = new Auto(1, "奧迪A4", 500.0, 4); cars[1] = new Auto(2, "馬自達(dá)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("序號(hào)\t" + "汽車(chē)名稱(chēng)\t" + "租金\t\t" + "容量(載人/載貨)"); System.out.println(Arrays.toString(cars)); // for(int i = 0; i < cars.length; i++){ // System.out.println("編號(hào):"+ (i+1) +" 品牌:"+ cars[i].getBrand() // +" 租金:"+ cars[i].getFee() +"/天 載客量:"+ cars[i].getPersonCapacity()+"人" // +" 載貨量:"+ cars[i].getGoodCapacity()+"噸" ); // } }else{ System.out.println("謝謝使用,再見(jiàn)!"); } System.out.print("請(qǐng)輸入你要租幾種車(chē):"); int rentNum = input.nextInt(); //selected用來(lái)保存客戶(hù)選中了什么車(chē)型,以及每種車(chē)型的輛數(shù),與car數(shù)組是對(duì)應(yīng)關(guān)系 int[] selected = new int[6]; for (int i = 1; i <= rentNum; i++){ System.out.println("請(qǐng)輸入第" + i + "種車(chē)型的序號(hào):" ); int nums = input.nextInt() - 1; System.out.println(cars[nums].getBrand() +"總共需要多少輛:"); int num = input.nextInt(); selected[nums] = num; } System.out.println("請(qǐng)輸入租車(chē)天數(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+"天單項(xiàng)費(fèi)用:"+selected[i]*cars[i].getFee()*daysNum+"元"); total += selected[i]*cars[i].getFee()*daysNum; } } System.out.println("租車(chē)總費(fèi)用:" + total + "元" + "\n" + "歡迎下次光臨!------------------------"); } }
關(guān)于“java實(shí)現(xiàn)租車(chē)系統(tǒng)的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
本文名稱(chēng):java實(shí)現(xiàn)租車(chē)系統(tǒng)的示例分析
文章來(lái)源:http://chinadenli.net/article32/giehpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、域名注冊(cè)、動(dòng)態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、網(wǎng)站制作、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)