欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)租車系統(tǒng)

 更新時(shí)間:2019年01月29日 09:38:31   作者:chao2016  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)租車系統(tǒng),以及遇到的兩個(gè)問題解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

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

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

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

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

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

運(yùn)行效果圖:

代碼如下:

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, "馬自達(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("序號\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+"天單項(xiàng)費(fèi)用:"+selected[i]*cars[i].getFee()*daysNum+"元");
 total += selected[i]*cars[i].getFee()*daysNum;
 }
 }
 System.out.println("租車總費(fèi)用:" + total + "元" + "\n" + "歡迎下次光臨!------------------------");
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論