手记

Java入门第二季6.1 达达租车系统,使用到继承、重写、多态、toString函数

为了方便理解,画了个UML图,可以参考一下

定义Car类型:载人+载货

public class Car {
    int id;
    String name;
    int seat;
    double load;
    double price;
    public Car(int id,String name,int seat,double load,double price){
        this.id = id;
        this.name = name;
        this.seat = seat;
        this.load = load;
        this.price = price;
    }
    public String toString(){
        return id+"\t"+name+"\t"+seat+"座\t"+load+"吨\t"+price+"元";
    }
}

定义LoadCar继承Car,重写构造方法(载货,则座位=0)

public class LoadCar extends Car {
    public LoadCar(int id,String name,double load,double price){
        super(id,name,0,load,price);
    }
    public String toString(){
        return id+"\t"+name+"\t"+seat+"\t"+load+"\t"+price;
    }
}

定义SeatCar继承Car,重写构造方法(载人,则载货=0)

public class SeatCar extends Car {
    public SeatCar(int id,String name,int seat,double price){
        super(id,name,seat,0,price);
    }
    public String toString(){
        return id+"\t"+name+"\t"+seat+"\t"+load+"\t"+price;
    }
}

实现(主要就是业务流程):

public class Initail {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("您是否要租车?1、是;2、否");
        Scanner in = new Scanner(System.in);
        if(in.nextInt()==1){
            Car[] cars = new Car[5];
            cars[0]=new SeatCar(1,"奥迪A8",4,400);
            cars[1]=new SeatCar(2,"特斯拉",4,500);
            cars[2]=new Car(3,"皮卡丘",4,2,200);
            cars[3]=new LoadCar(4,"松花江",10,200);
            cars[4]=new LoadCar(5,"黑龙江",20,300);
            System.out.println("现有5种车型可供您选择:");
            System.out.println("序号"+"\t"+"车型"+"\t"+"座位(座)"+"\t"+"载重(吨)"+"\t"+"租金/天");
            for(Car c : cars){
                System.out.println(c);
            }
            int totalSeat = 0;
            double totalLoad = 0;
            double totalPrice = 0;
            String loadCarList = "";
            String seatCarList = "";
            System.out.println("请输入您要租车的数量");
            int rentNum = in.nextInt();
            for(int i=0;i<rentNum;i++){
                System.out.println("你要选择的第"+(i+1)+"辆车的序号");
                int rentId = in.nextInt();
                if(rentId<5){
                    totalSeat = totalSeat + cars[rentId-1].seat;
                    totalLoad = totalLoad + cars[rentId-1].load;
                    totalPrice = totalPrice + cars[rentId-1].price;
                    if(cars[rentId-1].seat!=0){
                        seatCarList = seatCarList + cars[rentId-1] + "\n";                      
                    }else if(cars[rentId-1].load!=0){
                        loadCarList = loadCarList + cars[rentId-1] + "\n";                      
                    }
                }else{
                    System.out.println("不好意思,请选择现有的车辆(序号1-5)");
                }
            }
            System.out.println("请输入您要租车的天数");
            int rentDays = in.nextInt();
            System.out.println("/********************** 以下是您此次的租车账单 **********************/");
            if(seatCarList==""){
                System.out.println("没有可载人的车");
            }else{
                System.out.println("可载人的车辆有\n"+seatCarList);
                System.out.println("总座位数"+totalSeat);
            }
            if(loadCarList==""){
                System.out.println("没有可载货的车");
            }else{
                System.out.println("可载人的车辆有\n"+seatCarList);
                System.out.println("总载货量"+totalLoad);
            }
            System.out.println("总价格:"+totalPrice*rentDays+"元");
        }else{
            System.out.println("感谢您的使用");
        }
    }

}
29人推荐
随时随地看视频
慕课网APP

热门评论

那个,输入租车数量哪里有点问题,不管你输入多少他都默认是一辆

就是那个总价格需要自己修改下

totalSeat = totalSeat + cars[rentId-1].seat;
            totalLoad = totalLoad + cars[rentId-1].load;
            totalPrice = totalPrice + cars[rentId-1].price;

什么意思

???

查看全部评论