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 這兩個成員變量,導(dǎo)致調(diào)用參數(shù)時始終為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){ //構(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ù)組是對應(yīng)關(guān)系
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" + "歡迎下次光臨!------------------------");
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java圖片滑動驗證(登錄驗證)原理與實現(xiàn)方法詳解
這篇文章主要介紹了java圖片滑動驗證(登錄驗證)原理與實現(xiàn)方法,結(jié)合實例形式詳細分析了java圖片滑動登錄驗證的相關(guān)原理、實現(xiàn)方法與操作技巧,需要的朋友可以參考下2019-09-09
SpringBoot Admin健康檢查功能的實現(xiàn)
admin主要就是告訴運維人員,服務(wù)出現(xiàn)異常,然后進行通知(微信、郵件、短信、釘釘?shù)龋┛梢苑浅?焖偻ㄖ竭\維人員,相當(dāng)報警功能,接下來通過本文給大家介紹SpringBoot Admin健康檢查的相關(guān)知識,一起看看吧2021-06-06
Spring Shell應(yīng)用程序開發(fā)流程解析
這篇文章主要介紹了Spring Shell應(yīng)用程序開發(fā)流程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10
詳解Spring學(xué)習(xí)總結(jié)——Spring實現(xiàn)AOP的多種方式
這篇文章主要介紹了詳解Spring學(xué)習(xí)總結(jié)——Spring實現(xiàn)AOP的多種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
Spring?Boot配置內(nèi)容加密實現(xiàn)敏感信息保護
之前我們講過的配置相關(guān)知識都是Spring?Boot原生就提供的,而今天我們將介紹的功能并非Spring?Boot原生就支持,但卻非常有用:配置內(nèi)容的加密2021-11-11

