为了账号安全,请及时绑定邮箱和手机立即绑定

整理了一下,把多个对象保存起来,这样后期如果需要统计载人量或者载货量相关的也可以。

package com.car;

public class Car {
    protected String carName;   //汽车名称
    protected Double rent;      //租金

    public Car(String carName, double rent){
        this.carName = carName;
        this.rent = rent;
    }

    public String getCarName(){
        return this.carName;
    }

    public Double getRent(){
        return this.rent;
    }
}


AManned

package com.car;

/**
 * 载人汽车
 */
public class AManned extends Car{
    protected int personCount;
    public AManned(String carName, double rent, int personCount){
        super(carName, rent);
        this.personCount = personCount;
    }

    public int getPersonCount(){
        return this.personCount;
    }
}


ATruck:

package com.car;

/**
 * 货车
 */
public class ATruck extends Car{
    protected int productCount;
    public ATruck(String carName, double rent, int productCount){
        super(carName, rent);
        this.productCount = productCount;
    }

    public int getProductCount(){
        return this.productCount;
    }
}


AMulti:

package com.car;

/**
 * 多功能汽车
 */
public class AMulti extends Car{
    protected int productCount;
    protected int personCount;
    public AMulti(String carName, double rent, int personCount,  int productCount){
        super(carName, rent);
        this.productCount = productCount;
        this.personCount = personCount;
    }

    public int getPersonCount(){
        return this.personCount;
    }

    public int getProductCount(){
        return this.productCount;
    }
}


index:

package com.car;
import java.util.Scanner;

public class index {
    public static void main(String[] args){
        //引导语
        System.out.println("欢迎使用租车系统");
        System.out.println("您是否要租车?1-是,0-否");
        Scanner input = new Scanner(System.in);
        int isRent = input.nextInt();       //是否租车
        if (isRent == 1) {
            System.out.println("您可租车的类型和价目表:");
            System.out.println("序号 汽车名称 租金 容量");

            //声明所有汽车并显示
            Car[] cars = {
                    new AManned("奥迪",500, 4),
                    new AManned("马自达",400, 4),
                    new AMulti("皮卡雪",450, 4, 2),
                    new AManned("金龙",800, 20),
                    new ATruck("松花江", 400, 4),
                    new ATruck("依维柯",1000, 20)
            };
            for (int i = 0; i < cars.length; i++) {
                int index = i+1;
                if (cars[i] instanceof AManned) {
                    //载人
                    AManned car = (AManned) cars[i];
                    System.out.println(
                            index + " "+
                            car.getCarName()+ " "+
                            car.getRent()+ "元/天 "+
                            "载人:"+ car.getPersonCount()+ "人 "
                    );
                } else if(cars[i] instanceof ATruck) {
                    //载货
                    ATruck car = (ATruck) cars[i];
                    System.out.println(
                            index +" "+
                            car.getCarName()+ " " +
                            car.getRent()+ "元/天 " +
                            "载货:"+ car.getProductCount()+"吨"
                    );
                } else {
                    AMulti car = (AMulti) cars[i];
                    System.out.println(
                            index+ " "+
                            car.getCarName()+ " " +
                            car.getRent()+ "元/天 " +
                            "载人:"+car.getPersonCount()+ "人 "+
                            "载货:"+car.getProductCount()+"吨");
                }
            }

            //选择要租的车,并记录下来
            System.out.println("请选择要租车的数量:");
            int carsCount = input.nextInt();
            Car[] chooseCar = new Car[carsCount];
            for (int j = 0; j < carsCount; j++) {
                int cycle = j+1;
                System.out.println("请选择第"+ cycle +"辆车:");
                int carNum = input.nextInt();
                chooseCar[j] = cars[carNum-1];
            }

            //选择租车天数,并计算出租金
            System.out.println("请输入要租的天数:");
            int days = input.nextInt();
            double singleSum = 0;
            double sum = 0;
            for (Car aCar: chooseCar) {
                singleSum += aCar.getRent();
            }
            sum = singleSum * days;
            System.out.println("您的租金是:"+sum);

        }
    }
}


正在回答

13 回答

AManned car = (AManned) cars[i];

楼主,这个是干什么的啊,没看懂!

是调用数组里的对象吗

0 回复 有任何疑惑可以回复我~
#1

Minenami

强制类型转换
2020-01-14 回复 有任何疑惑可以回复我~

大佬,请收下我的膝盖!

1 回复 有任何疑惑可以回复我~
首页上一页12下一页尾页

举报

0/150
提交
取消

整理了一下,把多个对象保存起来,这样后期如果需要统计载人量或者载货量相关的也可以。

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信