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 price;
    } else {
        return 0;
    }
}

public double getWeight() {
    return weight;
}

public double getOriginalPrice() {
    return weight * price;
}

public int getWarranty() {
    return warranty;
}

}

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 getOriginalPrice() {
    return c1.getOriginalPrice() + c2.getOriginalPrice() + c3.getOriginalPrice() + c4.getOriginalPrice();
}

public double getLossPrice(int n) {
    double loss = 0;
    if (n > c1.getWarranty()) {
        loss += c1.getPrice(n) * c1.getWeight();
    }
    if (n > c2.getWarranty()) {
        loss += c2.getPrice(n) * c2.getWeight();
    }
    if (n > c3.getWarranty()) {
        loss += c3.getPrice(n) * c3.getWeight();
    }
    if (n > c4.getWarranty()) {
        loss += c4.getPrice(n) * c4.getWeight();
    }
    return loss;
}

}

public class Prog1 { public static void main(String[] args) { Cargo c1 = new Cargo("apple", 10, 2, 7); Cargo c2 = new Cargo("orange", 8, 1.5, 5); Cargo c3 = new Cargo("banana", 15, 3, 10); Cargo c4 = new Cargo("watermelon", 20, 1, 3);

    Train train = new Train(c1, c2, c3, c4);

    System.out.println("Total weight: " + train.totalWeight() + " tons");
    System.out.println("Original price: " + train.getOriginalPrice() + "万元");
    System.out.println("Loss price after 5 days: " + train.getLossPrice(5) + "万元");
    System.out.println("Loss price after 10 days: " + train.getLossPrice(10) + "万元");
}
编写货箱类Cargo名字nalme属性货物重量weight吨价格price万元吨保质期warranty 天编写构造方法 Cargo String name double weight double price int warra nty编写价格计算方法double getPriceint n如果n不大于保质期则返回货物价格反之返回编写一个火车类Train其成员包含4节货箱补全相关代码使得main方

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

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