计算货车租金

1.创建租金lease类

(1)创建车的对象,放在数组中
MotoVehicle[] motos = new MotoVehicle[4];
motos[0] = new Car(“宝马550i”,“京NY28588”);
motos[1] = new Car(“宝马550i”,“京NNN328”);
motos[2] = new Car(“别克林荫大道”,“京NY28588”);
motos[3] = new Bus(“金龙”,34);
(2) 循环调用calcRent()方法,计算总租金
int total = 0;
for(int i = 0; i <motos.length; i++)
{
total += motos[i].calRent(days);
}
System.out.println(“总租金” + total);

2.创建MotoVehicle类,实现calcRent ()方法 (父类)

public abstract class MotoVehicle {
public static int length;
private String number; //车牌号
private String brand; //品牌

abstract int calRent(int days);       //计算租金

public MotoVehicle()
{
}
public MotoVehicle(String number, String brand)
{
    this.number = number;
    this.brand  = brand;
}

public void setNumber(String number) {
    this.number = number;
}
public String getNumber() {
    return number;
}


public void setBrand(String brand) {
    this.brand = brand;
}
public String getBrand() {
    return brand;
}

}

3.汽车类 (子类)

public abstract class MotoVehicle {
public static int length;
private String number; //车牌号
private String brand; //品牌

abstract int calRent(int days);       //计算租金

public MotoVehicle()
{
}
public MotoVehicle(String number, String brand)
{
    this.number = number;
    this.brand  = brand;
}

public void setNumber(String number) {
    this.number = number;
}
public String getNumber() {
    return number;
}


public void setBrand(String brand) {
    this.brand = brand;
}
public String getBrand() {
    return brand;
}

}
4.卡车类 (子类)
public class Bus extends MotoVehicle{
private int dunwei; //吨位
private String name;

public Bus(String name,int dunwei)
{   
	this.name = name;
    this.dunwei = dunwei;
}

@Override
int calRent(int days) {
    return 50*dunwei*days;
}

}

好的,我了解任务要求了,下面是我的代码实现: 首先,我们需要定义一个车辆类Vehicle,该类包含车辆名称、车辆类型、租车天数等属性和计算租金的抽象方法。 ```java public abstract class Vehicle { protected String name; // 车辆名称 protected String type; // 车辆类型 protected int days; // 租车天数 public Vehicle(String name, String type, int days) { this.name = name; this.type = type; this.days = days; } // 计算租金的抽象方法 public abstract double calculateRent(); } ``` 接下来,我们需要定义三个具体的车辆类,分别是货车类、大型客车类和小型汽车类。这三个类都继承自Vehicle类,并实现calculateRent方法。 ```java // 货车类 public class Truck extends Vehicle { private double load; // 载重量(单位:吨) public Truck(String name, int days, double load) { super(name, "货车", days); this.load = load; } // 计算租金 @Override public double calculateRent() { return load * 1000; } } // 大型客车类 public class Bus extends Vehicle { private int seats; // 车内座位数 public Bus(String name, int days, int seats) { super(name, "大型客车", days); this.seats = seats; } // 计算租金 @Override public double calculateRent() { return seats * 50; } } // 小型汽车类 public class Car extends Vehicle { private int level; // 车辆等级 private int year; // 折旧年数 public Car(String name, int days, int level, int year) { super(name, "小型汽车", days); this.level = level; this.year = year; } // 计算租金 @Override public double calculateRent() { return 200 * level / Math.sqrt(year); } } ``` 最后,我们需要定义一个租车公司类CarRentCompany,该类提供静态函数rentVehicles,能够给定一组待租车辆,计算租金总额。 ```java public class CarRentCompany { // 计算租金总额 public static double rentVehicles(Vehicle[] vehicles) { double totalRent = 0; for (Vehicle vehicle : vehicles) { totalRent += vehicle.calculateRent() * vehicle.days; } return totalRent; } } ``` 使用示例: ```java public class Test { public static void main(String[] args) { Vehicle[] vehicles = new Vehicle[] { new Truck("东风小霸王", 5, 3.5), new Bus("金龙客车", 3, 45), new Car("宝马320i", 7, 3, 3), new Car("奥迪A6L", 4, 4, 2) }; double totalRent = CarRentCompany.rentVehicles(vehicles); System.out.println("总租金:" + totalRent); } } ``` 输出结果: ``` 总租金:24050.0 ``` 以上就是我的代码实现,希望可以帮助你完成本关任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值