Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
方法有条件地返回每个第n个对象的和';Java ArrayList中的s属性_Java_Arraylist_Enums - Fatal编程技术网

方法有条件地返回每个第n个对象的和';Java ArrayList中的s属性

方法有条件地返回每个第n个对象的和';Java ArrayList中的s属性,java,arraylist,enums,Java,Arraylist,Enums,我正在构建一种方法,对名为parcels的arraylist应用“免费”折扣,该arraylist的类型为Parcel。折扣应适用于每四个“小”包裹-示例如下。地块对象具有成本(双精度)和类型等属性,这些属性属于地块类型ParcelType是一个Enum(小、中、大、xl) 下面是我应用折扣的方法,折扣是在名为Order的类中定义的。我希望该方法将每个有资格享受折扣的包裹的成本加总,然后返回该金额,以便我可以用另一种方法将其从订单总额中删除。我想我的部分问题是我可能需要映射或forEach包裹阵

我正在构建一种方法,对名为parcels的arraylist应用“免费”折扣,该arraylist的类型为
Parcel
。折扣应适用于每四个“小”包裹-示例如下。
地块
对象具有成本(双精度)和类型等属性,这些属性属于
地块类型
ParcelType
是一个
Enum
(小、中、大、xl)

下面是我应用折扣的方法,折扣是在名为
Order
的类中定义的。我希望该方法将每个有资格享受折扣的包裹的成本加总,然后返回该金额,以便我可以用另一种方法将其从订单总额中删除。我想我的部分问题是我可能需要映射或forEach包裹阵列列表才能访问包裹?任何协助都将不胜感激

public double smallParcelDiscount(ArrayList<Parcel> parcels){

        for (int i = 0; i < parcels.size(); i ++) {

            if (parcel % 4 == 0 && parcels.ParcelType.SMALL){
// sum the cost of each eligible small parcel i.e if there are 2 small 
//parcels valid for discount in an order containing 8 small parcels
// return the sum so that it can be subtracted from the total cost of an order
            }
        }
    }

包裹类型枚举:

public class Parcel {

    private double length;
    private double width;
    private double height;
    private ParcelType type;
    private double cost;
    private double weight;

    public Parcel(double length, double width, double height, ParcelType type, double cost, double weight) {
        this.length = length;
        this.width = width;
        this.height = height;
        this.type = type;
        this.cost = cost;
        this.weight = weight;
    }
public enum ParcelType {
    SMALL(1),
    MEDIUM(3),
    LARGE(6),
    XL(10),
    HEAVY(50);

    private final double weightLimit;

    ParcelType(double weightLimit){
        this.weightLimit = weightLimit;
    }

    public double getWeightLimit(){
        return weightLimit;
    }

}

订单类构造函数

public class Order {

    private ArrayList<Parcel> parcels;
    private Boolean speedyShipping;


    public Order() {
        this.parcels = new ArrayList<>();
        this.speedyShipping = false;
    }
公共类秩序{
私人ArrayList包裹;
私有布尔快速shipping;
公共秩序(){
this.parcels=新的ArrayList();
this.speedyShipping=false;
}
试试这个

public double smallParcelDiscount(ArrayList<Parcel> parcels) {
    return IntStream.range(0, parcels.size())
            .filter(n -> (n + 1) % 4 == 0) //take every fourth parcel one based
            .mapToObj(parcels::get)
            .filter(parcel -> ParcelType.SMALL.equals(parcel.getType()))
            .mapToDouble(Parcel::getCost)
            .sum();
}
公共双小型地块贴现(ArrayList地块){
return IntStream.range(0,parcels.size())
.filter(n->(n+1)%4==0)//每四个包裹取一个
.mapToObj(包裹::获取)
.filter(parcel->ParcelType.SMALL.equals(parcel.getType()))
.mapToDouble(包裹::getCost)
.sum();
}

&包裹[i].type==.ParcelType.SMALL
难道你不能使用一个带有过滤器的流来获取所有小包裹,然后使用过滤后的列表来获取每四个包裹的价值/成本吗?@DanielWosch你能在上下文中提供一个实现示例吗?我目前正在使用一个流来计算订单的总体成本,但有一个映射来映射第四个包裹吗e将每个成本属性的两倍计算成一个总和。我在我的手机上,这使得发布一些代码有点复杂。请看下面的答案,这个方向是这样的。按包裹类型的筛选不应该在第4次筛选之前进行吗?这很有效。在我接受这个答案之前,请修改包裹类型包裹成本到各自的getter?您不能像这样直接访问包裹,因为属性是私有的。需要调用getter。Daniel也是正确的,但我无法解释为什么这样做。我想过滤掉每4个,如果类型很小,则免费。感谢您的贡献。一旦您修改,我将接受此响应。请随时提供解决方案的任何进一步解释或其他代码注释。我意识到我没有在上面的代码示例中提供getter。它们只是
getCost()
getType()
@DanielWosch这取决于您是希望每4个小包裹或每4个小包裹都有折扣我用单元测试测试了您的解决方案,并将第4个小包裹换成了中等包裹,效果很好,因此我相信您的实现就是我要找的。谢谢您的时间。