Car.java 所有汽車的父類
package com.imooc; public class Car { protected String name; protected double rent; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getRent() { return rent; } public void setRent(double rent) { this.rent = rent; } }
passengerCar.java 客車
package com.imooc; public class passengerCar extends Car { private double peopleCapacity; public passengerCar(String name,double rent,double peoplecapacity){ this.name=name; this.rent=rent; this.peopleCapacity=peoplecapacity; } public double getPeopleCapacity() { return peopleCapacity; } public void setPeopleCapacity(double peopleCapacity) { this.peopleCapacity = peopleCapacity; } }
PickUp.java 皮卡
package com.imooc; public class PickUp extends Car { private double cargoCapacity; private double peopleCapacity; public PickUp(String name,double rent,double cargoCapacity,double peopleCapacity){ this.name=name; this.rent=rent; this.cargoCapacity=cargoCapacity; this.peopleCapacity=peopleCapacity; } public double getCargoCapacity() { return cargoCapacity; } public void setCargoCapacity(double cargoCapacity) { this.cargoCapacity = cargoCapacity; } public double getPeopleCapacity() { return peopleCapacity; } public void setPeopleCapacity(double peopleCapacity) { this.peopleCapacity = peopleCapacity; } }
Trunk.java 貨車
package com.imooc; public class Trunk extends Car { private double cargoCapacity; public Trunk(String name,double rent,double cargoCapacity){ this.name=name; this.rent=rent; this.cargoCapacity=cargoCapacity; } public double getCargoCapacity() { return cargoCapacity; } public void setCargoCapacity(double cargoCapacity) { this.cargoCapacity = cargoCapacity; } }
Test.java 測試類
package com.imooc; import java.util.Scanner; public class Test{ public static void main(String[] args) { Car[] carsForRent={new passengerCar("奧迪A4",500,4),new passengerCar("馬自達6", 400, 4),new PickUp("皮卡雪6", 450,2, 4),new passengerCar("金龍", 800, 20),new Trunk("松花江", 400, 4),new Trunk("依維柯", 1000, 20)}; System.out.println("歡迎使用答答租車系統:"); System.out.println("你是否需要租車:1是 0否"); Scanner scan=new Scanner(System.in); String input=scan.next(); if(input.equals("1")){ System.out.println("你可租車的類型及其價目表:"); System.out.println("序號\t汽車名稱\t租金\t\t容量"); int i=1; for(Car currentCar:carsForRent){ if(currentCar instanceof passengerCar){ System.out.println("" + i +"\t"+ currentCar.getName() + "\t" + currentCar.getRent() + "元/天\t" + ((passengerCar)currentCar).getPeopleCapacity()+"人"); } if(currentCar instanceof PickUp){ System.out.println("" + i +"\t"+ currentCar.getName() + "\t" + currentCar.getRent() + "元/天\t" + ((PickUp)currentCar).getPeopleCapacity()+"人 "+((PickUp)currentCar).getCargoCapacity()+"噸"); } if(currentCar instanceof Trunk){ System.out.println("" + i +"\t"+ currentCar.getName() + "\t" + currentCar.getRent() + "元/天\t" + ((Trunk)currentCar).getCargoCapacity()+"噸"); } i++; } System.out.println("請輸入您要租汽車的數量:"); int rentNum=scan.nextInt(); int[] carsRent=new int[rentNum]; for(int j=0;j<rentNum;j++){ System.out.println("請輸入第"+(j+1) + "輛車的序號:"); carsRent[j]=scan.nextInt(); } System.out.println("請輸入租車天數:"); int daysRent=scan.nextInt(); int totalPeopley=0; int totalCargoy=0; double totalMoney=0; String carsForPeople=""; String carsForCargo=""; for(int j=0;j<rentNum;j++){ totalMoney+=carsForRent[carsRent[j]-1].getRent(); if(carsForRent[carsRent[j]-1] instanceof passengerCar){ totalPeopley+=((passengerCar)carsForRent[carsRent[j]-1]).getPeopleCapacity(); carsForPeople+=carsForRent[carsRent[j]-1].getName()+"\t"; } if(carsForRent[carsRent[j]-1] instanceof PickUp){ totalPeopley+=((PickUp)carsForRent[carsRent[j]-1]).getPeopleCapacity(); totalCargoy+=((PickUp)carsForRent[carsRent[j]-1]).getCargoCapacity(); carsForPeople+=carsForRent[carsRent[j]-1].getName()+"\t"; carsForCargo+=carsForRent[carsRent[j]-1].getName()+"\t"; } if(carsForRent[carsRent[j]-1] instanceof Trunk){ totalCargoy+=((Trunk)carsForRent[carsRent[j]-1]).getCargoCapacity(); carsForCargo+=carsForRent[carsRent[j]-1].getName()+"\t"; } } totalMoney=totalMoney*daysRent; System.out.println("您的賬單:"); System.out.println("***可載人的車有:"); System.out.println(carsForPeople +"\t共載人:" +totalPeopley+"人" ); System.out.println("***載貨的車有:"); System.out.println(carsForCargo +"\t共貨:" +totalCargoy+"噸" ); System.out.println("***租車總價格:"+totalMoney+"元"); scan.close(); } } }
2016-04-26
totalMoney+=carsForRent[carsRent[j]-1].getRent();
這里為什么減一啊???好難理解的程序
2015-01-28
3Q,學習了很多
2014-12-15
厲害!沒想到可以這樣定義類。
2014-10-28
我想請教幾個問題:
1.第8~10行,
定義這個數組的時候,可以這么用嗎?它的成員是小括號里的漢字嗎?
2.第19行,
for循環的這種用法沒有見過,請問是什么意思啊,carsForRent是什么東西啊
3.49~62行,比如
這些都看不大懂,請明白的人解釋一下,菜鳥在此先謝過了
2014-09-27
mark
2014-09-05
如果我選兩輛2好車,那打印出來的結果還對嗎?還有,用foreach還for()循環比較好呢 那個類的聲明學習了!
2014-09-05
mark
2014-09-03
good你可以發到我們郵箱中哦~