首先,我们需要创建车辆类Vehicle,包含车型、租金、容量等属性,以及计算租金的方法calculateRent()。

public class Vehicle { private String type; //车型 private double rent; //租金 private int capacity; //容量(人数或吨数)

public Vehicle(String type, double rent, int capacity) {
    this.type = type;
    this.rent = rent;
    this.capacity = capacity;
}

public String getType() {
    return type;
}

public double getRent() {
    return rent;
}

public int getCapacity() {
    return capacity;
}

public double calculateRent(int days) {
    return rent * days;
}

}

接下来,我们需要创建租车系统类RentalSystem,包含主要的逻辑。在程序启动时,我们需要初始化车辆列表,然后等待用户输入并根据用户选择计算租金。

import java.util.ArrayList; import java.util.Scanner;

public class RentalSystem { private ArrayList vehicleList;

public RentalSystem() {
    vehicleList = new ArrayList<>();
    vehicleList.add(new Vehicle("奥迪", 500, 4));
    vehicleList.add(new Vehicle("马自达", 500, 4));
    vehicleList.add(new Vehicle("金龙", 800, 20));
    vehicleList.add(new Vehicle("松花江", 400, 4));
    vehicleList.add(new Vehicle("依维柯", 1000, 20));
    vehicleList.add(new Vehicle("皮卡", 450, 2));
}

public void run() {
    Scanner scanner = new Scanner(System.in);
    while (true) {
        System.out.println("欢迎使用答答租车系统,租车请按1,退出请按0");
        int choice = scanner.nextInt();
        if (choice == 0) {
            break;
        } else if (choice == 1) {
            System.out.println("您可租车的类型及价格表:");
            System.out.println("序号\t车型\t租金\t容量");
            for (int i = 0; i < vehicleList.size(); i++) {
                Vehicle vehicle = vehicleList.get(i);
                System.out.printf("%d\t%s\t%.0f元/天\t%d%s\n", i + 1, vehicle.getType(), vehicle.getRent(),
                        vehicle.getCapacity(), (vehicle.getCapacity() > 4 ? "吨" : "人"));
            }
            System.out.println("请输入您要租车的种类数:");
            int num = scanner.nextInt();
            ArrayList<Vehicle> selectedVehicles = new ArrayList<>();
            double totalRent = 0;
            for (int i = 0; i < num; i++) {
                System.out.printf("请输入您要租车的第%d种车的序号:\n", i + 1);
                int index = scanner.nextInt() - 1;
                if (index < 0 || index >= vehicleList.size()) {
                    System.out.println("输入有误,请重新输入");
                    i--;
                    continue;
                }
                Vehicle selectedVehicle = vehicleList.get(index);
                selectedVehicles.add(selectedVehicle);
                System.out.printf("已选中%s,租金为%.0f元/天\n", selectedVehicle.getType(), selectedVehicle.getRent());
            }
            System.out.println("请输入您要租车的天数:");
            int days = scanner.nextInt();
            for (Vehicle vehicle : selectedVehicles) {
                totalRent += vehicle.calculateRent(days);
            }
            int totalPeople = selectedVehicles.stream().filter(vehicle -> vehicle.getCapacity() <= 4)
                    .mapToInt(Vehicle::getCapacity).sum();
            int totalGoods = selectedVehicles.stream().filter(vehicle -> vehicle.getCapacity() > 4)
                    .mapToInt(Vehicle::getCapacity).sum();
            System.out.printf("可以载人的车有:%d共载人:%d人\n", selectedVehicles.stream()
                    .filter(vehicle -> vehicle.getCapacity() <= 4).count(), totalPeople);
            System.out.printf("可以载货的车有:%d共载货:%d吨\n", selectedVehicles.stream()
                    .filter(vehicle -> vehicle.getCapacity() > 4).count(), totalGoods);
            System.out.printf("您总共需付费:%.0f元\n", totalRent);
        } else {
            System.out.println("输入有误,请重新输入");
        }
    }
    System.out.println("欢迎下次使用答答租车系统!");
}

public static void main(String[] args) {
    RentalSystem rentalSystem = new RentalSystem();
    rentalSystem.run();
}

}

在主方法中,我们创建一个RentalSystem实例并调用它的run()方法来启动程序。

现在,我们可以运行程序并测试它的功能了。

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

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

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