自己的剛寫的代碼 寫的不好 但是可以給幕友作為借鑒
好久沒有敲代碼了 變得生疏和腦子不夠用了 用慕課來重溫下知識(shí)點(diǎn)還是很好的 這是我在看了老師解題所給出的部分代碼后自己補(bǔ)上的 每個(gè)類都用橫線分隔開了 主要由5個(gè)類組成 一個(gè)父類Car 三個(gè)子類Truck、PickUp、PassengerCar 和一個(gè)主函數(shù)類Main組成 本人寫的代碼很爛 寫在這里只是給更多幕友一個(gè)參考的地方 也說不上參考就是給大家隨便看下?
-------------------------
package com.demo_h;
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;
}
}
-----------------------------------
package com.demo_h;
public class Truck extends Car {
private double cargoCapacity;
public Truck(String name,double rent,double cargoCapacity)
{
this.name = name;
this.rent = rent;
this.setCargoCapacity(cargoCapacity);
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
public double getCargoCapacity() {
return cargoCapacity;
}
}
--------------------------------
package com.demo_h;
public class PickUp extends Car {
private double peopleCapacity;
private double cargoCapacity;
public PickUp(String name,double rent,double peopleCapacity,double cargoCapacity)
{
this.name = name;
this.rent = rent;
this.setPeopleCapacity(peopleCapacity);
this.setCargoCapacity(cargoCapacity);
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
public double getCargoCapacity() {
return cargoCapacity;
}
public void setPeopleCapacity(double peopleCapacity) {
this.peopleCapacity = peopleCapacity;
}
public double getPeopleCapacity() {
return peopleCapacity;
}
}
---------------------------------
package com.demo_h;
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;
}
}
------------------------------------
package com.demo_h;
import java.util.Scanner;
public class Main {
/*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//用父類數(shù)組指向子類實(shí)例化對(duì)象,并且用構(gòu)造方法賦值
Car[] carsForRent = {new PassengerCar("奧迪", 500, 4),new PassengerCar("馬自達(dá)", 400, 4),
new PassengerCar("金龍", 800, 20),new PickUp("皮卡雪", 450, 4, 2),
new Truck("松花江", 400, 4),new Truck("依維柯", 1000, 20)};
System.out.println("****歡迎來到答答租車系統(tǒng)****");
System.out.println("你是否要租車:1是 ?0否");
Scanner scan = new Scanner(System.in);
String input = scan.next();
if(input.equals("1"))
{
System.out.println("你可租車的類型及其價(jià)目表:");
System.out.println("序號(hào)\t汽車名稱\t租金\t容量");
int i = 1;
//顯示所有的車型
for(Car currentCar:carsForRent)//foreach循環(huán)顯示數(shù)組內(nèi)容
{
if(currentCar instanceof PassengerCar)//判斷是否指向當(dāng)前子類對(duì)象,然后進(jìn)行向下轉(zhuǎn)型
{
System.out.println(i+"\t"+currentCar.getName()+"\t"+currentCar.getRent()
+"元/天\t載人"+(int)((PassengerCar) currentCar).getPeopleCapacity()+"人");
i++;
}
if(currentCar instanceof Truck)
{
System.out.println(i+"\t"+currentCar.getName()+"\t"+currentCar.getRent()
+"元/天\t載貨"+((Truck) currentCar).getCargoCapacity()+"噸");
i++;
}
if(currentCar instanceof PickUp)
{
System.out.println(i+"\t"+currentCar.getName()+"\t"+currentCar.getRent()
+"元/天\t載人"+(int)((PickUp) currentCar).getPeopleCapacity()+"人.載貨"+
((PickUp) currentCar).getCargoCapacity()+"噸");
i++;
}
}
System.out.println("請(qǐng)輸入您要汽車的數(shù)量");
int carNum = scan.nextInt();
int accout = 0;
int peopleNum = 0;
int cargoNum = 0;
Car[] carsForRent01 = new Car[carNum]; //此數(shù)組方便為打印車輛名字而建
if(carNum>0&&carNum<7)//限制汽車數(shù)量
{
for(int a=0;a<carNum;a++)
{
System.out.println("請(qǐng)輸入第"+(a+1)+"輛車的序號(hào)");
int num = scan.nextInt();
switch (num) { //用switch語句實(shí)現(xiàn)選擇以及對(duì)金額、載客量和數(shù)組的賦值
case 1:
accout = accout+500;
peopleNum = peopleNum+4;
carsForRent01[a] = carsForRent[0];
break;
case 2:
accout = accout+400;
peopleNum = peopleNum+4;
carsForRent01[a] = carsForRent[1];
break;
case 3:
accout = accout+450;
peopleNum = peopleNum+4;
cargoNum = cargoNum+2;
carsForRent01[a] = carsForRent[2];
break;
case 4:
accout = accout+800;
peopleNum = peopleNum+20;
carsForRent01[a] = carsForRent[3];
break;
case 5:
accout = accout+400;
cargoNum = cargoNum+4;
carsForRent01[a] = carsForRent[4];
break;
case 6:
accout = accout+1000;
cargoNum = cargoNum+20;
carsForRent01[a] = carsForRent[5];
break;
default:
System.err.println("輸入車輛序號(hào)不符合標(biāo)準(zhǔn)!");
break;
}
}
}
else
{
System.out.println("輸入汽車數(shù)量過大或則不符合標(biāo)準(zhǔn)!");
}
System.out.println("*****你的賬單*****");
System.out.println("---可載客的車有---");
for(Car currentCar:carsForRent01) //輸出可載客車的名字 注意進(jìn)行對(duì)象比較
{
if(currentCar instanceof PassengerCar)
{
System.out.print(currentCar.getName()+" ?");
}
}
System.out.println("共載人:"+peopleNum+"人");
System.out.println("---可載貨的車有---");
for(Car currentCar:carsForRent01)
{
if((currentCar instanceof Truck)||(currentCar instanceof PickUp))
{
System.out.print(currentCar.getName()+" ?");
}
}
System.out.println("共載貨:"+cargoNum+"噸");
System.out.println("租車總價(jià)格:"+accout+"¥");
}
else
{
System.out.println("退出系統(tǒng) 歡飲下次光臨!");
System.exit(-1);
}
}
}
2016-03-30
Car數(shù)組的類型呢?
int 還是String?
表示完全懵逼了,
下面感覺很難看懂,至少給個(gè)注釋呀。
跪謝。
2015-10-13
。。子類的構(gòu)造方法初始化都是錯(cuò)的吧?父類的私有成員需要調(diào)用方法設(shè)置,子類自己的私有成員才可以用(this。)的形式吧?!
2015-10-12
沒有寫租車天數(shù)
2015-08-11
應(yīng)該用arraylist方法收集客戶需要的車輛,如果要租兩輛奧迪,則該程序無法解決。
2015-07-16
您好,我的是jdk1.8.0_45,但在eclipse創(chuàng)建工程時(shí)如下,使用foreach仍提示source level需要1.5以上。請(qǐng)問應(yīng)該下
載那個(gè)版本的jdk呢,謝謝!
2015-07-14
謝謝您的分享!我的eclipse使用foreach時(shí)提示錯(cuò)誤,提示只有1.5以上的才能用,請(qǐng)問如何升級(jí)呢?