我有一个Products类列表,它具有以下属性:
public class Products{
private int month;
private int year;
private int sku;
private String detail;
private Double quantity;
private Double price;
private Double discount;
private Double %discount;
// getters and setters
static List<Products> sumatoriaVentas(List<Products> itemList) {
List<Product> tempListP = new ArrayList<Product>();
Collections.sort(itemList, Comparator.comparing(Product::getYear)
.thenComparing(Product::getMonth).thenComparing(Product::getSku));
Integer itemCode = null;
double quantiti = 0;
double price = 0;
double discount = 0;
double %discount = 0;
Products itemObj = null;
Integer itemMonth= null;
Integer itemYear= null;
for (int i = 0; i < itemList.size(); i++) {
if (itemCode == null || (itemCode.equals(itemList.get(i).getSku()) && itemMonth.equals(itemList.get(i).getMonth()) && itemMonth.equals(itemList.get(i).getYear()) )) {
quantiti = quantiti + itemList.get(i).getquantiti();
price = price + itemList.get(i).getprice();
discount = discount + itemList.get(i).getdiscount();
%discount = %discount + itemList.get(i).get%discount();
itemMonth = itemList.get(i).getMonth();
itemYea = itemList.get(i).getYear();
} else {
itemObj = new Product( itemList.get(i).getMonth(), itemList.get(i).getYear(), itemCode, "" , quantiti, price,discount,%discount);
if (tempListP.contains(itemObj)) {
tempListP.remove(itemObj);
}
tempListP.add(itemObj);
quantiti = 0;
quantiti = quantiti + itemList.get(i).getquantiti();
price = 0;
price = price + itemList.get(i).getprice();
discount = 0;
discount = discount + itemList.get(i).getdiscount();
%discount = 0;
%discount = %discount + itemList.get(i).get%discount();
itemMonth = itemList.get(i).getMonth();
itemYea = itemList.get(i).getYear();
}
itemCode = itemList.get(i).getSku();
if (i == itemList.size() - 1) {
itemObj = new Product( itemList.get(i).getMonth(), itemList.get(i).getYear(), itemCode, "" , quantiti, price,discount,%discount);
tempListP.add(itemObj);
}
}
return itemList;
}
}
主要的
public static void main(String[] args) {
List<Products> items = new ArrayList<Products>();
items.add(new Products(8,2020,10203040,"Tshirt",1,10.00,1.00,0.1));
items.add(new Products(9,2020,10203040,"Tshirt",2,20.00,2.00,0.2));
items.add(new Products(3,2021,10203040,"Tshirt",3,30.00,3.00,0.3));
items.add(new Products(7,2020,50607080,"Tshirt",4,40.00,4.00,0.4));
items.add(new Products(8,2020,10203040,"Tshirt",5,50.00,5.00,0.5));
items.add(new Products(8,2021,10203040,"Tshirt",8,80.00,8.00,0.8));
Products.sumProduct(items);
输入
月 | 年份 | 货号 | 详情 | 数量 | 价格 | 折扣 | % 折扣
8 | 2020 | 10203040 | T 恤 | 1 | 10 | 1 | 0.1
9 | 2020 | 10203040 | T 恤 | 2 | 20 | 2 | 0.2
3 | 2021 | 10203040 | T 恤 | 3 | 30 | 3 | 0.3
7 | 2020 | 50607080 | T 恤 | 4 | 40 | 4 | 0.4
8 | 2020 | 10203040 | T 恤 | 5 | 50 | 5 | 0.5
8 | 2021 | 10203040 | T 恤 | 8 | 80 | 8 | 0.8
最终结果应该是这样的:
输出:
月 | 年份 | 货号 | 详情 | 数量 | 价格 | 折扣 | % 折扣
7 | 2020 | 50607080 | T 恤 | 4 | 40 | 4 | 0.4
8 | 2020 | 10203040 | T 恤 | 6 | 60 | 6 | 0.6
9 | 2020 | 10203040 | T 恤 | 2 | 20 | 2 | 0.2
3 | 2021 | 10203040 | T 恤 | 3 | 30 | 3 | 0.3
8 | 2021 | 10203040 | T 恤 | 8 | 80 | 8 | 0.8
我想创建一个函数,该函数遍历产品列表并对具有相同月份、年份和 sku 的项目的值求和,并返回数量、价格、折扣、折扣百分比的总和列表。
但是我如何将上面的代码链接起来计算和求和呢?特别是对于超过 3 个字段(月、年和 sku)的分组。有一个更好的方法吗?