实现汽车信息的java程序

实现一个Java程序,为一家租车公司保存车辆(汽车)数据。首先,创建一个名为VehicleData的类,该类包含保存汽车信息的字段、一个重载构造函数,每个字段的get和set方法强制汽车信息(字段)如下所示

1.型号

2.颜色(如银、红、蓝、白、黑)

3.年份(例如2020202022年)

4.发动机(如全电动、混合动力、汽油、柴油、汽油)

创建设置字段的默认构造函数。没有-个0.0无

创建一个重载构造函数,它需要所有四个字段的值: Model (字符串 )、Color (字符串)、Year (Int) 和Engine (字符串)

包括四个字段中每个字段的get和set方法

将此文件另存为VehicleData。JAVA

创建一个名为TestVehicleData的测试应用程序,以演示每个方法的 正确工作。将应用程序另存为TestVehicleData. JAVA
笔记为了证明每个方法都能正确工作,请使用默认构造函数和重载构造函数创建新对象。显示对象的值。然后使用setter方法存储新值,并使用getter方法显示更新后的值。你的应用程序应该打印如下内容

img


创建一个名为Rental的类,该类包括客户ID (customerld)、 租赁天数(daysNum)和车辆数据

创建一个默认构造函数,将客户ID设置为0,将租赁天数设置为0,并将VehicleData值2022设置为0

创建并重载为每个字段提供值的构造函数。还为每个字段提供get和set方

将文件另存为出租。爪哇

创建一个应用程序,演示每个方法是否正确工作,并将其另存为TestRent。JAVA

注意:为了证明每个方法都能正确工作,请使用默认构造函数和重载构造函数创建新对象:显示对象的值。然后使用setter方法存储新值,并使用getter方法显示更新后的值。你的应用程序应该打印如下内容

img

VehicleData 类定义如下:

public class VehicleData {

    private String model;

    private String color;

    private int year;

    private String engine;

    public VehicleData() {
        this.model = "none";
        this.color = "none";
        this.year = 0;
        this.engine = "none";
    }

    public VehicleData(String model, String color, int year, String engine) {
        this.model = model;
        this.color = color;
        this.year = year;
        this.engine = engine;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getEngine() {
        return engine;
    }

    public void setEngine(String engine) {
        this.engine = engine;
    }
}

TestVehicleData 类定义如下:

public class TestVehicleData {

    public static void main(String[] args) {
        VehicleData vehicleData1 = new VehicleData();

        VehicleData vehicleData2 = new VehicleData();
        vehicleData2.setModel("XC40");
        vehicleData2.setColor("red");
        vehicleData2.setYear(2021);
        vehicleData2.setEngine("all-electric");


        VehicleData vehicleData3 = new VehicleData();
        vehicleData3.setModel("XC60");
        vehicleData3.setColor("blue");
        vehicleData3.setYear(2022);
        vehicleData3.setEngine("petrol");

        for (VehicleData vehicleData : Arrays.asList(vehicleData1, vehicleData2, vehicleData3)) {
            System.out.println("The car is " + vehicleData.getModel() + " " + vehicleData.getColor() + " " + vehicleData.getYear() + " " + vehicleData.getEngine());
        }
    }
}

TestVehicleData 运行结果如下:

img

Rental 类定义如下:

public class Rental {

    private int customerld;

    private int daysNum;

    private VehicleData vehicleData;

    public Rental() {
        this.customerld = 0;
        this.daysNum = 0;
        this.vehicleData = new VehicleData();
        this.vehicleData.setYear(0);
    }

    public Rental(int customerld, int daysNum, VehicleData vehicleData) {
        this.customerld = customerld;
        this.daysNum = daysNum;
        this.vehicleData = vehicleData;
    }

    public int getCustomerld() {
        return customerld;
    }

    public void setCustomerld(int customerld) {
        this.customerld = customerld;
    }

    public int getDaysNum() {
        return daysNum;
    }

    public void setDaysNum(int daysNum) {
        this.daysNum = daysNum;
    }

    public VehicleData getVehicleData() {
        return vehicleData;
    }

    public void setVehicleData(VehicleData vehicleData) {
        this.vehicleData = vehicleData;
    }


}

TestRent 类定义如下:

public class TestRent {

    public static void main(String[] args) {
        Rental rental1 = new Rental();


        VehicleData vehicleData = new VehicleData();
        vehicleData.setModel("XC60");
        vehicleData.setColor("silver");
        vehicleData.setYear(2021);
        vehicleData.setEngine("hybrid");
        Rental rental2 = new Rental(12345, 5, vehicleData);


        vehicleData = new VehicleData();
        vehicleData.setModel("XC40");
        vehicleData.setColor("white");
        vehicleData.setYear(2018);
        vehicleData.setEngine("petrol");
        Rental rental3 = new Rental(34567, 2, vehicleData);


        for (Rental rental : Arrays.asList(rental1, rental2, rental3)) {
            System.out.println("Customer ID# " + rental.getCustomerld() + " has rent a car for: " + rental.getDaysNum() + " days");
            System.out.println("\tThe car is " + rental.getVehicleData().getModel() + " " + rental.getVehicleData().getColor() + " "
                    + rental.getVehicleData().getYear() + " " + rental.getVehicleData().getEngine());

        }

    }
}

TestRent 类运行结果如下:

img