日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

用java编写租车系统代码_java实现租车系统

發布時間:2023/12/3 windows 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用java编写租车系统代码_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编写租车系统代码_java实现租车系统的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。