手记

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

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人推荐
随时随地看视频
慕课网APP

热门评论

您好!在学习你的代码时,输入”public static List<Car> getCarList() {”,出现提示“Illegal modifier for parameter getCarList; only final is permitted”是为什么呢?

使用LISt的哪里不是很弄得很明白

查看全部评论