Java怎么统计每个项目下的每个类别的数据

为了演示如何在Java中统计每个项目下的每个类别的数据,我们可以考虑一个简单的场景:假设我们有一个电商系统,需要统计每个商品分类在每个店铺下的销售数量。这里我们将使用Java的集合框架,如HashMapArrayList,来存储和统计数据。

1.使用Java的集合框架HashMapArrayList来存储和统计数据

首先,我们需要定义几个类来模拟这个场景:Shop(店铺),Category(商品分类),以及一个SaleItem(销售项),用于表示某个店铺下某个分类的商品销售情况。

import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
class Shop {  
    private String name;  
  
    public Shop(String name) {  
        this.name = name;  
    }  
  
    // getter and setter  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    @Override  
    public String toString() {  
        return "Shop{" +  
                "name='" + name + '\'' +  
                '}';  
    }  
}  
  
class Category {  
    private String name;  
  
    public Category(String name) {  
        this.name = name;  
    }  
  
    // getter and setter  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    @Override  
    public String toString() {  
        return "Category{" +  
                "name='" + name + '\'' +  
                '}';  
    }  
}  
  
class SaleItem {  
    private Shop shop;  
    private Category category;  
    private int quantity;  
  
    public SaleItem(Shop shop, Category category, int quantity) {  
        this.shop = shop;  
        this.category = category;  
        this.quantity = quantity;  
    }  
  
    // getters  
    public Shop getShop() {  
        return shop;  
    }  
  
    public Category getCategory() {  
        return category;  
    }  
  
    public int getQuantity() {  
        return quantity;  
    }  
}  
  
public class SalesStatistics {  
  
    public static void main(String[] args) {  
        // 示例数据  
        Shop shop1 = new Shop("店铺A");  
        Shop shop2 = new Shop("店铺B");  
        Category category1 = new Category("电子产品");  
        Category category2 = new Category("服装");  
  
        List<SaleItem> sales = new ArrayList<>();  
        sales.add(new SaleItem(shop1, category1, 10));  
        sales.add(new SaleItem(shop1, category2, 5));  
        sales.add(new SaleItem(shop2, category1, 8));  
        sales.add(new SaleItem(shop2, category2, 12));  
  
        // 统计每个店铺下的每个类别的销售数量  
        Map<Shop, Map<Category, Integer>> statistics = new HashMap<>();  
  
        for (SaleItem item : sales) {  
            Shop shop = item.getShop();  
            Category category = item.getCategory();  
            int quantity = item.getQuantity();  
  
            // 如果店铺尚未在统计中,则初始化该店铺的统计信息  
            if (!statistics.containsKey(shop)) {  
                statistics.put(shop, new HashMap<>());  
            }  
  
            // 更新统计信息  
            Map<Category, Integer> categoryStats = statistics.get(shop);  
            categoryStats.put(category, categoryStats.getOrDefault(category, 0) + quantity);  
        }  
  
        // 打印统计结果  
        for (Map.Entry<Shop, Map<Category, Integer>> entry : statistics.entrySet()) {  
            Shop shop = entry.getKey();  
            System.out.println("店铺: " + shop.getName());  
            for (Map.Entry<Category, Integer> categoryEntry : entry.getValue().entrySet()) {  
                Category category = categoryEntry.getKey();  
                Integer quantity = categoryEntry.getValue();  
                System.out.println("  分类: " + category.getName() + ", 数量: " + quantity);  
            }  
        }  
    }  
}

这个示例中,我们首先定义了三个类:ShopCategorySaleItem。然后,在SalesStatistics类的main方法中,我们创建了一些示例数据,并使用嵌套的HashMap来统计每个店铺下每个类别的销售数量。最后,我们遍历这个嵌套。

2. 使用Java的Map接口及其实现类来完成这项任务示例

我将提供一个稍微不同但同样详细的代码示例,用于统计每个项目(在这个例子中,我们可以将其视为“产品”)下的每个类别(比如“电子产品”下的“手机”和“电脑”)的销售数据。我们将使用Java的Map接口及其实现类(如HashMap)来完成这项任务。

import java.util.HashMap;  
import java.util.Map;  
  
class ProductCategory {  
    private String productName; // 产品名(这里可以视为项目)  
    private String category; // 类别  
  
    public ProductCategory(String productName, String category) {  
        this.productName = productName;  
        this.category = category;  
    }  
  
    // Getter 和 Setter 省略,为了简洁  
    public String getProductName() {  
        return productName;  
    }  
  
    public String getCategory() {  
        return category;  
    }  
  
    @Override  
    public String toString() {  
        return "ProductCategory{" +  
                "productName='" + productName + '\'' +  
                ", category='" + category + '\'' +  
                '}';  
    }  
}  
  
class SaleRecord {  
    private ProductCategory productCategory;  
    private int quantity; // 销售数量  
  
    public SaleRecord(ProductCategory productCategory, int quantity) {  
        this.productCategory = productCategory;  
        this.quantity = quantity;  
    }  
  
    // Getter 省略,为了简洁  
    public ProductCategory getProductCategory() {  
        return productCategory;  
    }  
  
    public int getQuantity() {  
        return quantity;  
    }  
}  
  
public class SalesStatisticsExample {  
  
    public static void main(String[] args) {  
        // 创建一些销售记录  
        SaleRecord record1 = new SaleRecord(new ProductCategory("iPhone", "手机"), 100);  
        SaleRecord record2 = new SaleRecord(new ProductCategory("MacBook", "电脑"), 50);  
        SaleRecord record3 = new SaleRecord(new ProductCategory("Galaxy S21", "手机"), 80);  
        SaleRecord record4 = new SaleRecord(new ProductCategory("ThinkPad", "电脑"), 70);  
  
        // 存储销售统计数据的Map  
        // Key: ProductName (产品名),Value: 另一个Map,其中Key: Category (类别),Value: 销售数量  
        Map<String, Map<String, Integer>> salesStatistics = new HashMap<>();  
  
        // 填充销售统计数据  
        addSaleRecord(salesStatistics, record1);  
        addSaleRecord(salesStatistics, record2);  
        addSaleRecord(salesStatistics, record3);  
        addSaleRecord(salesStatistics, record4);  
  
        // 打印销售统计数据  
        printSalesStatistics(salesStatistics);  
    }  
  
    // 辅助方法:向销售统计数据中添加销售记录  
    private static void addSaleRecord(Map<String, Map<String, Integer>> statistics, SaleRecord record) {  
        ProductCategory productCategory = record.getProductCategory();  
        String productName = productCategory.getProductName();  
        String category = productCategory.getCategory();  
  
        // 如果产品尚未在统计中,则初始化该产品的统计信息  
        if (!statistics.containsKey(productName)) {  
            statistics.put(productName, new HashMap<>());  
        }  
  
        // 更新统计信息  
        Map<String, Integer> categoryStats = statistics.get(productName);  
        categoryStats.put(category, categoryStats.getOrDefault(category, 0) + record.getQuantity());  
    }  
  
    // 辅助方法:打印销售统计数据  
    private static void printSalesStatistics(Map<String, Map<String, Integer>> statistics) {  
        for (Map.Entry<String, Map<String, Integer>> entry : statistics.entrySet()) {  
            String productName = entry.getKey();  
            Map<String, Integer> categoryStats = entry.getValue();  
  
            System.out.println("产品: " + productName);  
            for (Map.Entry<String, Integer> categoryEntry : categoryStats.entrySet()) {  
                String category = categoryEntry.getKey();  
                int quantity = categoryEntry.getValue();  
                System.out.println("  类别: " + category + ", 数量: " + quantity);  
            }  
        }  
    }  
}

在这个例子中,我们定义了两个辅助类ProductCategorySaleRecord,分别用于表示产品类别和销售记录。然后,在SalesStatisticsExample类中,我们创建了一些销售记录,并使用嵌套的HashMap来统计每个产品下的每个类别的销售数量。

posted @ 2024-07-12 11:17  TechSynapse  阅读(20)  评论(0编辑  收藏  举报