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

JAVA入门第二季 6-1综合练习

标签:
Java
Car抽象父类
package com.dsx.demo;
public abstract class Car {
    //车辆序号
    private int index;
    //车辆名称
    private String name;
    //车辆租金
    private int price;
    //车辆类型
    private String type;
    //车辆载人容量
    private int manned;
    //车辆载货容量
    private int carry;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getName() {
        return name;
    }

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

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getManned() {
        return manned;
    }

    public void setManned(int manned) {
        this.manned = manned;
    }

    public int getCarry() {
        return carry;
    }

    public void setCarry(int carry) {
        this.carry = carry;
    }
 }
MannedCar子类继承父类Car(载人车)
package com.dsx.dada;

public class MannedCar extends Car {
    public MannedCar(int index, String name, int price, String type, int manned) {
        super.setIndex(index);
        super.setName(name);
        super.setPrice(price);
        super.setType(type);
        super.setManned(manned);
    }

    @Override
    public String toString() {
        return this.getIndex() + "\t"
                + this.getName() + "\t"
                + this.getPrice() + "元/天\t" + "载人:"
                + this.getManned() + "人";
    }
}
CarryCar子类继承父类Car(载货车)
package com.dsx.dada;

public class CarryCar extends Car {

    public CarryCar(int index, String name, int price, String type, int carry) {
        super.setIndex(index);
        super.setName(name);
        super.setPrice(price);
        super.setType(type);
        super.setCarry(carry);
    }

    @Override
    public String toString() {
        return this.getIndex() + "\t"
                + this.getName() + "\t"
                + this.getPrice() + "元/天\t" + "载货:"
                + this.getCarry() + "吨";
    }
}
MannedAndCarryCar子类继承父类Car(载人载货两用车)
package com.dsx.dada;
public class MannedAndCarryCar extends Car {

    public MannedAndCarryCar(int index, String name, int price, String type, int manned, int carry) {
        super.setIndex(index);
        super.setName(name);
        super.setPrice(price);
        super.setType(type);
        super.setManned(manned);
        super.setCarry(carry);
}

    @Override
    public String toString() {
        return this.getIndex() + "\t"
                + this.getName() + "\t"
                + this.getPrice() + "元/天\t" + "载货:"
                + this.getCarry() + "吨 载人:"
                + this.getManned() + "人";
    }
}
达达租车主程序类
package com.dsx.dada;
import java.util.*;
public class DadaMain {
    public static void main(String[] args) {
        //获取租车列表
        List<Car> carList = getCarList();
        System.out.println("欢迎使用达达租车系统");
        System.out.println("您是否要租车:1是 0否");
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        switch (num) {
            case 0:
                System.out.println("您已退出达达租车系统,欢迎再次使用");
                break;
            case 1:
                //显示租车列表
                displayCarList(carList);
                //操作系统
                Map<String, Object> map = operation(carList);
                //显示账单
                displayBill(map);
                break;
        }
    }

    public static List<Car> getCarList() {
        List<Car> carArray = new ArrayList();
        Car mannedCar = new MannedCar(1, "奥迪A4", 500, "manned", 4);
        Car mannedCar_s = new MannedCar(2, "马自达6", 400, "manned", 4);
        Car mannedAndCarry = new MannedAndCarryCar(3, "皮卡雪6", 450, "mannedAndCarry", 4, 2);
        Car mannedCar_t = new MannedCar(4, "金龙", 800, "manned", 20);
        Car Carry = new CarryCar(5, "松花江", 400, "carry", 4);
        Car Carry_s = new CarryCar(6, "依维柯", 1000, "carry", 20);
        carArray.add(mannedCar);
        carArray.add(mannedCar_s);
        carArray.add(mannedAndCarry);
        carArray.add(mannedCar_t);
        carArray.add(Carry);
        carArray.add(Carry_s);
        return carArray;
    }

    public static void displayCarList(List<Car> param) {
        System.out.println("您可租车的类型及其价目表:");
        System.out.println("序号\t汽车名称\t租金\t容量");
        for (Car car : param) {
            System.out.println(car);
        }
    }

    public static Map<String, Object> operation(List<Car> param) {
        //定义载人车列表
        List<String> mannedCarList = new ArrayList<String>();
        //定义载货车列表
        List<String> CarryCarList = new ArrayList<String>();
        //定义总载人数
        int mannedNum = 0;
        //定义总载货数
        int carryNum = 0;
        //定义总平均价格
        int priceNum = 0;
        System.out.print("请输入你要租汽车的数量:");
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        for (int i = 0; i < num; i++) {
            System.out.println("请输入第" + (i + 1) + "辆车的序号");
            int carIndex = scanner.nextInt();
            Car car = param.get(carIndex - 1);
            //获取车辆类型
            String type = car.getType();
            //获取车辆名称
            String name = car.getName();
            //获取车辆价格
            int price = car.getPrice();
            System.out.println("您租的车辆信息如下:");
            System.out.println("序号\t汽车名称\t租金\t容量");
            System.out.println(car);

            if ("manned".equals(type) || "mannedAndCarry".equals(type)) {
                mannedCarList.add(name);
                mannedNum = car.getManned() + mannedNum;
            }
            if ("carry".equals(type) || "mannedAndCarry".equals(type)) {
                CarryCarList.add(name);
                carryNum = carryNum + car.getCarry();
            }
            priceNum = priceNum + price;
        }
        System.out.println("请输入租车天数:");
        int day = scanner.nextInt();
        int sum = day * priceNum;
        Map<String, Object> map = new HashMap();
        map.put("mannedCarList", mannedCarList);
        map.put("carryCarList", CarryCarList);
        map.put("mannedNum", mannedNum);
        map.put("carryNum", carryNum);
        map.put("sumPrice", sum);
        return map;
    }

    public static void displayBill(Map<String, Object> map) {
        System.out.println("您的账单:");
        System.out.println("***可载人的车有:");
        List<String> mannedCarList = (List<String>) map.get("mannedCarList");
        for (String mannedCar : mannedCarList) {
            System.out.print(mannedCar + "\t");
        }
        System.out.println("共载人:" + map.get("mannedNum") + "人");
        System.out.println("***可载货的车有:");
        List<String> carryCarList = (List<String>) map.get("carryCarList");
        for (String carryCar : carryCarList) {
            System.out.print(carryCar + "\t");
        }
        System.out.println("共载货:" + map.get("carryNum") + "吨");
        System.out.println("***租车总价格:" + map.get("sumPrice") + "元");
        System.out.println("租车成功,欢迎再次使用");
    }
}
点击查看更多内容
10人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
1
获赞与收藏
13

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消