首先,我们需要设计一个车辆类Car,其中包含车辆的型号、租金、容量等属性,以及能够获取这些属性的方法。

public class Car {
    private String model; // 型号
    private double rent; // 租金
    private int capacity; // 容量

    public Car(String model, double rent, int capacity) {
        this.model = model;
        this.rent = rent;
        this.capacity = capacity;
    }

    public String getModel() {
        return model;
    }

    public double getRent() {
        return rent;
    }

    public int getCapacity() {
        return capacity;
    }
}

接下来,我们需要设计一个租车系统类CarRentalSystem,其中包含获取可租车辆列表、租车、计算租金等方法。

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

public class CarRentalSystem {
    private List<Car> cars;

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

    public void start() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎使用答答租车系统,租车请按1,退出请按0");
        int option = scanner.nextInt();
        if (option == 0) {
            System.out.println("谢谢使用,再见!");
            return;
        }
        System.out.println("您可租车的类型及价格表:");
        System.out.println("序号\t型号\t租金\t容量");
        for (int i = 0; i < cars.size(); i++) {
            System.out.println((i+1) + "\t" + cars.get(i).getModel() + "\t" + cars.get(i).getRent() + "元/天\t" + cars.get(i).getCapacity());
        }
        System.out.println("请输入您要租车的种类数:");
        int num = scanner.nextInt();
        List<Integer> choices = new ArrayList<>();
        for (int i = 1; i <= num; i++) {
            System.out.println("请输入您要租车的第" + i + "种车的序号:");
            int choice = scanner.nextInt();
            choices.add(choice);
        }
        System.out.println("请输入您要租车的天数:");
        int days = scanner.nextInt();
        double totalRent = 0;
        int totalCapacity = 0;
        System.out.print("您租的车有:");
        for (int choice : choices) {
            Car car = cars.get(choice-1);
            System.out.print(car.getModel() + " ");
            totalRent += car.getRent() * days;
            totalCapacity += car.getCapacity();
        }
        System.out.println("\n可以载人的车有:" + getTotalNumOfCarsByCapacity(4) + "共载人:" + getTotalCapacityByCapacity(4));
        System.out.println("可以载货的车有:" + getTotalNumOfCarsByCapacity(2) + "共载货:" + getTotalCapacityByCapacity(2));
        System.out.println("您总共需付费:" + totalRent + "元");
        start();
    }

    private int getTotalNumOfCarsByCapacity(int capacity) {
        int count = 0;
        for (Car car : cars) {
            if (car.getCapacity() == capacity) {
                count++;
            }
        }
        return count;
    }

    private int getTotalCapacityByCapacity(int capacity) {
        int totalCapacity = 0;
        for (Car car : cars) {
            if (car.getCapacity() == capacity) {
                totalCapacity += capacity;
            }
        }
        return totalCapacity;
    }
}

最后,在主函数中调用CarRentalSystem的start()方法即可开始租车系统。

public class Main {
    public static void main(String[] args) {
        CarRentalSystem carRentalSystem = new CarRentalSystem();
        carRentalSystem.start();
    }
}
用java制作该项目用类和对象的编程思想来完成yongxinshou的方式歡迎使用答答租車系統租車請按1退出請按0您可租車的類型及價目表序號 直型 租金	容量	1 奥迪	500元天	4人	2 馬自達 500元天	4人	3 金龍 800元天	20人	4 松花江 400元天	4吨	5 依維柯	1000元天	20吨	6 皮卡	450元天	4人2吨	请输入您要租车的种类数2请输入您要租车的第1种车的序号请

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

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