今天用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;所以在各子類方法中又獨立加上了前面兩個方法,問題得以解決。
運行效果圖:
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
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; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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" ; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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" ; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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" ; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
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+ "天單項費用:" +selected[i]*cars[i].getfee()*daysnum+ "元" ); total += selected[i]*cars[i].getfee()*daysnum; } } system.out.println( "租車總費用:" + total + "元" + "\n" + "歡迎下次光臨!------------------------" ); } } |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/chao2016/article/details/50532660