实现代码如下:

// 定义车类 class Car { private String name; // 车名 private int rent; // 租金 private int capacity; // 容量

// 构造函数
public Car(String name, int rent, int capacity) {
    this.name = name;
    this.rent = rent;
    this.capacity = capacity;
}

// 获取车名
public String getName() {
    return name;
}

// 获取租金
public int getRent() {
    return rent;
}

// 获取容量
public int getCapacity() {
    return capacity;
}

}

// 客车类 class PassengerCar extends Car { private int passenger; // 载人数

// 构造函数
public PassengerCar(String name, int rent, int capacity, int passenger) {
    super(name, rent, capacity);
    this.passenger = passenger;
}

// 获取载人数
public int getPassenger() {
    return passenger;
}

}

// 货车类 class GoodsCar extends Car { private int cargo; // 载货量

// 构造函数
public GoodsCar(String name, int rent, int capacity, int cargo) {
    super(name, rent, capacity);
    this.cargo = cargo;
}

// 获取载货量
public int getCargo() {
    return cargo;
}

}

// 皮卡类 class Pickup extends Car { private int passenger; // 载人数 private int cargo; // 载货量

// 构造函数
public Pickup(String name, int rent, int passenger, int cargo) {
    super(name, rent, passenger+cargo);
    this.passenger = passenger;
    this.cargo = cargo;
}

// 获取载人数
public int getPassenger() {
    return passenger;
}

// 获取载货量
public int getCargo() {
    return cargo;
}

}

// 答答租车系统类 class CarRentalSystem { private static final Car[] CARS = { new PassengerCar("奥迪", 500, 4, 4), new PassengerCar("马自达", 500, 4, 4), new PassengerCar("金龙", 800, 20, 20), new GoodsCar("松花江", 400, 4, 4), new GoodsCar("依维柯", 1000, 20, 20), new Pickup("皮卡", 450, 4, 2) }; // 所有车辆信息

private int[] selectedCars; // 已选车辆的序号
private int rentDays;       // 租车天数

// 构造函数
public CarRentalSystem(int[] selectedCars, int rentDays) {
    this.selectedCars = selectedCars;
    this.rentDays = rentDays;
}

// 计算总租金
public int getTotalRent() {
    int totalRent = 0;
    for (int i : selectedCars) {
        totalRent += CARS[i-1].getRent() * rentDays;
    }
    return totalRent;
}

// 获取可载人车辆数和总载人数
public int[] getPassengerCars() {
    int count = 0;
    int totalPassenger = 0;
    for (int i : selectedCars) {
        if (CARS[i-1] instanceof PassengerCar) {
            count++;
            totalPassenger += ((PassengerCar) CARS[i-1]).getPassenger();
        }
    }
    return new int[] {count, totalPassenger};
}

// 获取可载货车辆数和总载货量
public int[] getGoodsCars() {
    int count = 0;
    int totalCargo = 0;
    for (int i : selectedCars) {
        if (CARS[i-1] instanceof GoodsCar) {
            count++;
            totalCargo += ((GoodsCar) CARS[i-1]).getCargo();
        }
    }
    return new int[] {count, totalCargo};
}

// 获取皮卡的载人数和载货量
public int[] getPickup() {
    int passenger = 0;
    int cargo = 0;
    for (int i : selectedCars) {
        if (CARS[i-1] instanceof Pickup) {
            passenger += ((Pickup) CARS[i-1]).getPassenger();
            cargo += ((Pickup) CARS[i-1]).getCargo();
        }
    }
    return new int[] {passenger, cargo};
}

// 输出租车信息
public void printRentInfo() {
    int[] passengerCars = getPassengerCars();
    int[] goodsCars = getGoodsCars();
    int[] pickup = getPickup();

    System.out.println("可以载人的车有:" + passengerCars[0] + ",共载人:" + passengerCars[1] + "人");
    System.out.println("可以载货的车有:" + goodsCars[0] + ",共载货:" + goodsCars[1] + "吨");

    if (pickup[0] > 0) {
        System.out.println("可以租用的皮卡,载人:" + pickup[0] + "人,载货:" + pickup[1] + "吨");
    }

    System.out.println("您总共需付费:" + getTotalRent() + "元");
}

}

// 测试类 public class Test { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);

    System.out.println("欢迎使用答答租车系统,租车请按1,退出请按0");

    int choice = scanner.nextInt();
    while (choice != 0) {
        System.out.println("您可租车的类型及价格表:");
        System.out.println("序号\t车型\t租金\t容量");
        for (int i = 0; i < CarRentalSystem.CARS.length; i++) {
            Car car = CarRentalSystem.CARS[i];
            System.out.println((i+1) + "\t" + car.getName() + "\t" + car.getRent() + "元/天\t" + car.getCapacity());
        }

        System.out.println("请输入您要租车的种类数:");
        int count = scanner.nextInt();
        int[] selectedCars = new int[count];
        for (int i = 0; i < count; i++) {
            System.out.println("请输入您要租车的第" + (i+1) + "种车的序号:");
            selectedCars[i] = scanner.nextInt();
        }

        System.out.println("请输入您要租车的天数:");
        int rentDays = scanner.nextInt();

        CarRentalSystem carRentalSystem = new CarRentalSystem(selectedCars, rentDays);
        carRentalSystem.printRentInfo();

        System.out.println("租车请按1,退出请按0");
        choice = scanner.nextInt();
    }
}

}

用java制作该项目用类和对象的编程思想来完成用新手编写的方式不使用复杂的函数客车货车皮卡分别一个类歡迎使用答答租車系統租車請按1退出請按0您可租車的類型及價目表序號 直型 租金	容量	1 奥迪	500元天	4人	2 馬自達 500元天	4人	3 金龍 800元天	20人	4 松花江 400元天	4吨	5 依維柯	1000元天	20吨	6 皮卡	450元天	4人2吨	请输入您要租车的种类数2请输入

原文地址: http://www.cveoy.top/t/topic/KT5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录