包裹分箱 算法

包裹 分箱简单算法 算出临界值 分箱数量加1
/**
*预估分箱算法
* @return
*/
  private Tuple2<BigDecimal,Integer>  calculateFareAndNums(List<BigDecimal> list,PxChannelPrice price){
if(list==null||list.isEmpty()){
Tuple2.of(BigDecimal.ZERO,0);
}
Collections.sort((List<BigDecimal>) list, new Comparator<BigDecimal>() {
@Override
public int compare(BigDecimal o1, BigDecimal o2) {
return o2.compareTo(o1);
}
});
BigDecimal taxSum = BigDecimal.ZERO;//税费
BigDecimal taxLmt = new BigDecimal(50);//税费限制
int bagCnt = 0;//分包数量
for (int i = 0;i<list.size();i++){
BigDecimal decimal = list.get(i);

if(decimal.compareTo(taxLmt)>-1){
bagCnt++;
}else {
if(taxSum.add(decimal).compareTo(taxLmt)>-1){
bagCnt++;
taxSum = decimal;
}else{
taxSum = taxSum.add(decimal);

}
}
}
if(taxSum.compareTo(BigDecimal.ZERO)==1){
bagCnt++;
}
return Tuple2.of(new BigDecimal(bagCnt).multiply(price.getInitialWeightPrice()),bagCnt);
}


public class Tuple2<V1, V2> {

private final V1 v1;
private final V2 v2;

public Tuple2(V1 t1, V2 t2) {
this.v1 = t1;
this.v2 = t2;
}

public static <V1, V2> Tuple2<V1, V2> of(V1 v1, V2 v2) {
return new Tuple2<>(v1, v2);
}

public V1 _1() {
return v1;
}

public V2 _2() {
return v2;
}

@Override
public String toString() {
return "(" + v1 + "," + v2 + ')';
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

Tuple2<?, ?> tuple2 = (Tuple2<?, ?>) o;

if (!v1.equals(tuple2.v1))
return false;
return v2.equals(tuple2.v2);

}

@Override
public int hashCode() {
int result = v1.hashCode();
result = 31 * result + v2.hashCode();
return result;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值