public class Cargo { private String name; private double weight; private double price; private int warranty;

public Cargo(String name, double weight, double price, int warranty) {
    this.name = name;
    this.weight = weight;
    this.price = price;
    this.warranty = warranty;
}

public double getPrice(int n) {
    if (n <= warranty) {
        return weight * price;
    } else {
        return 0;
    }
}

public String getName() {
    return name;
}

public double getWeight() {
    return weight;
}

public double getPrice() {
    return price;
}

public int getWarranty() {
    return warranty;
}

}

public class Train { private Cargo c1; private Cargo c2; private Cargo c3; private Cargo c4;

public Train(Cargo c1, Cargo c2, Cargo c3, Cargo c4) {
    this.c1 = c1;
    this.c2 = c2;
    this.c3 = c3;
    this.c4 = c4;
}

public double totalWeight() {
    return c1.getWeight() + c2.getWeight() + c3.getWeight() + c4.getWeight();
}

public double originalPrice() {
    return c1.getPrice() + c2.getPrice() + c3.getPrice() + c4.getPrice();
}

public double lossPrice(int n) {
    double loss = 0;
    if (c1.getWarranty() < n) {
        loss += c1.getPrice(n - c1.getWarranty());
    }
    if (c2.getWarranty() < n) {
        loss += c2.getPrice(n - c2.getWarranty());
    }
    if (c3.getWarranty() < n) {
        loss += c3.getPrice(n - c3.getWarranty());
    }
    if (c4.getWarranty() < n) {
        loss += c4.getPrice(n - c4.getWarranty());
    }
    return loss;
}

}

public class Progl { public static void main(String[] args) { Train t = new Train(new Cargo('鱼肉', 150, 8, 7), new Cargo('白菜', 100, 1.1, 10), new Cargo('荔枝', 100, 10, 3), new Cargo('大米', 200, 2, 180)); System.out.println('货物总重量:' + t.totalWeight() + '吨'); System.out.println('出发时货物总价格:' + t.originalPrice() + '万元'); System.out.println('发出5天后,损失货物价格:' + t.lossPrice(5) + '万元'); System.out.println('发出10天后,损失货物价格:' + t.lossPrice(10) + '万元'); } }

Java 货箱类和火车类实现 - 货物运输价格计算与损失统计

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

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