继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java入门第二季 6-1综合练习(租车系统)

VigilanteZ
关注TA
已关注
手记 1
粉丝 7
获赞 75

RentCar类:输入租车数组,并实现租车过程

package com.imooc.RentCarSystem;
import java.util.*;

public class RentCar {

    Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        //生成一个引用变量数组存放可以出租的车辆
        ACar[] V = new ACar[6];
        V[0] = new SaloonCar("奥迪A4",500,4);
        V[1] = new SaloonCar("马自达6",400,4);
        V[2] = new PickUp("皮卡雪6",450,4,2);
        V[3] = new Bus("金龙",800,20);
        V[4] = new Van("松花江",400,4);
        V[5] = new Truck("依维柯",1000,20);

        RentCar rent = new RentCar();
        rent.startRent(V);
    }

    public void startRent(ACar[] cars) {
        //询问用户是否要租车
        System.out.println("欢迎使用租车系统");
        System.out.println("请问您是否要租车: 1 是;  0 否");
        if(input.nextInt() == 0) {
            return;
        }

        //输出可租用车辆概览
        System.out.println("您可租用的车辆类型及价目表:");
        System.out.println("序号   汽车名称            租金          容量");
        for(int i = 0; i < cars.length; i++) {
            System.out.print((i+1) + ".  ");
            cars[i].getDescription();
        }

        //获取用户租车需求
        System.out.println("请输入您要租用的汽车数量:");
        int num = input.nextInt();
        int[] indices = new int[num];
        for(int i = 0; i < indices.length; i++) {
            System.out.println("请输入租用的第" + (i+1) + "辆车辆的序号");
            indices[i] = (input.nextInt() - 1);
        }

        System.out.println("请输入租车天数:");
        int days = input.nextInt();

        this.printBill(cars, indices, days);
    }

    public void printBill(ACar[] cars,int[] indices, int days){

                System.out.println("您的账单:");
                int sumP = 0;
                int sumC = 0;
                int fee = 0;

                //输出可载人车辆的名称,以及载人数总和
                System.out.println("***可载人的车辆***");
                for(int index:indices) {
                    if(cars[index].getVolP() > 0) {
                        System.out.print(cars[index].getName() + " ");
                        sumP += cars[index].getVolP();
                    }
                }
                System.out.println("总载人数:" + sumP);

                //输出可载货车辆的名称,以及总载货量
                System.out.println("***可载货的车辆***");
                for(int index:indices) {
                    if(cars[index].getVolC() > 0) {
                        System.out.print(cars[index].getName() + " ");
                        sumC += cars[index].getVolC();
                    }
                }
                System.out.println("总载货量:" + sumC + "吨");

                //统计租车总价
                for(int index:indices) {
                    fee += cars[index].getRent()*days;
                }
                System.out.println("租车总价格:" + fee);
    }

}

ACar类:Abstract类,作为所有车辆类的超类

package com.imooc.RentCarSystem;

public abstract class ACar {
    private String name;//名称
    private int rent;//租金
    private int volP;//载人量
    private int volC;//载货量

    public ACar(String name, int rent) {
        this.name = name;
        this.rent = rent;
    }

    public abstract void getDescription();//此方法用来打印对象参数

    //下面都是参数的getter和setter
    public String getName() {
        return name;
    }

    public int getRent() {
        return rent;
    }

    public int getVolP() {
        return volP;
    }

    public int getVolC() {
        return volC;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setRent(int rent) {
        this.rent = rent;
    }

    public void setVolP(int volP) {
        this.volP = volP;
    }

    public void setVolC(int volC) {
        this.volC = volC;
    }
}

SaloonCar类:轿车(载人<5)

package com.imooc.RentCarSystem;

public class SaloonCar extends ACar {

    public SaloonCar(String name, int rent, int volP) {
        super(name, rent);
        if(volP <= 5 && volP > 0){
            super.setVolP(volP);
        } else {
            super.setVolP(5);
            System.out.println("输入的载人量不在指定范围内,载人量已被设为5");
        }
        setVolC(0);
    }

    public void getDescription() {
        System.out.println(super.getName() + "  "
                + super.getRent() + "元/一天    "
                + "载人:" + super.getVolP() + "人");
    }

}

Bus类:大巴(载人>10)

package com.imooc.RentCarSystem;

public class Bus extends ACar {

    public Bus(String name, int rent, int volP){
        super(name, rent);
        if(volP >= 10){
            super.setVolP(volP);
        } else {
            super.setVolP(10);
            System.out.println("输入的载人量不在指定范围内,载人量已被设为10");
        }
        super.setVolC(0);
    }

    public void getDescription() {
        System.out.println(super.getName() + "           "
                + super.getRent() + "元/一天    "
                + "载人:" + super.getVolP() + "人");
    }

}

PickUp类:皮卡车

package com.imooc.RentCarSystem;

public class PickUp extends ACar {

    public PickUp(String name, int rent, int volP, int volC) {
        super(name, rent);
        super.setVolP(volP);
        super.setVolC(volC);
    }

    public void getDescription() {
        System.out.println(super.getName() + "  "
                + super.getRent() + "元/一天    "
                + "载人:" + super.getVolP() + "人"
                + "  载货:" + super.getVolC() + "吨");
    }

}

Van类:小货车(载货量<5T)

package com.imooc.RentCarSystem;

public class Van extends ACar {

    public Van(String name, int rent, int volC) {
        super(name, rent);
        if(volC <= 5 && volC >0) {
            super.setVolC(volC);
        } else {
            super.setVolP(5);
            System.out.println("输入的载货量不在指定范围内,载货量已被设为5");
        }
        super.setVolP(0);
    }

    public void getDescription() {
        System.out.println(super.getName() + "       "
                + super.getRent() + "元/一天    "
                + "载货:" + super.getVolC() + "吨");
    }

}

Truck类:卡车(载货量5T~20T)

package com.imooc.RentCarSystem;

public class Truck extends ACar {

    public Truck(String name, int rent, int volC) {
        super(name, rent);
        if(volC > 5 && volC <= 20) {
            super.setVolC(volC);
        } else {
            super.setVolP(20);
            System.out.println("输入的载货量不在指定范围内,载货量已被设为20");
        }
        super.setVolP(0);
    }

    public void getDescription() {
        System.out.println(super.getName() + "       "
                + super.getRent() + "元/一天    "
                + "载货:" + super.getVolC() + "吨");
    }

}
打开App,阅读手记
30人推荐
发表评论
随时随地看视频慕课网APP