From 71af368eaaac8b927f86736075f3b69d814be91c Mon Sep 17 00:00:00 2001 From: hirushi1999 Date: Tue, 14 Oct 2025 09:50:11 +0530 Subject: [PATCH 1/2] Product Analysis using java streams --- Beginner/ProductAnalysis.java | 90 +++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Beginner/ProductAnalysis.java diff --git a/Beginner/ProductAnalysis.java b/Beginner/ProductAnalysis.java new file mode 100644 index 0000000..cf50e95 --- /dev/null +++ b/Beginner/ProductAnalysis.java @@ -0,0 +1,90 @@ +import java.util.*; +import java.util.stream.Collectors; +/** + * Program Title: Count how many products are in each category + * Author: [hirushi1999] + * Date: 2025-10-14 + * Demonstrates how to use Java Streams to perform data analysis on a list of products. + * Scenario: + * - A store sells products in various categories (Fruits, Vegetables, Dairy, Meat). + * - Each product has a price, quantity sold, and availability status. + * Task: + * 1. Count how many products are in each category. + */ +class Product { + private String name; + private String category; + private double price; + private int quantitySold; + private boolean inStock; + + public Product(String name, String category, double price, int quantitySold, boolean inStock) { + this.name = name; + this.category = category; + this.price = price; + this.quantitySold = quantitySold; + this.inStock = inStock; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public int getQuantitySold() { + return quantitySold; + } + + public void setQuantitySold(int quantitySold) { + this.quantitySold = quantitySold; + } + + public boolean isInStock() { + return inStock; + } + + public void setInStock(boolean inStock) { + this.inStock = inStock; + } +} + +public class ProductAnalysis { + public static void main(String[] args) { + List productList = Arrays.asList( + new Product("Apple", "Fruits", 120.0, 50, true), + new Product("Banana", "Fruits", 80.0, 80, true), + new Product("Carrot", "Vegetables", 60.0, 70, true), + new Product("Broccoli", "Vegetables", 90.0, 40, false), + new Product("Milk", "Dairy", 150.0, 30, true), + new Product("Cheese", "Dairy", 500.0, 15, true), + new Product("Yogurt", "Dairy", 200.0, 25, false), + new Product("Chicken", "Meat", 800.0, 20, true), + new Product("Beef", "Meat", 1200.0, 10, true), + new Product("Fish", "Meat", 950.0, 18, false)); + + System.out.println("1. Count how many products are in each category"); + // Group products by category and count them + Map map = productList.stream().collect(Collectors.groupingBy(Product::getCategory,Collectors.counting())); + // Display results + map.forEach((cate, count) -> System.out.println(cate + ": " + count)); + } +} From c5ffc5022b641851e56789703cf7356df5fa0961 Mon Sep 17 00:00:00 2001 From: Bisrut Pyne Date: Thu, 16 Oct 2025 00:50:34 +0530 Subject: [PATCH 2/2] Update ProductAnalysis.java Include the Time and Space Complexity. --- Beginner/ProductAnalysis.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Beginner/ProductAnalysis.java b/Beginner/ProductAnalysis.java index cf50e95..30d8779 100644 --- a/Beginner/ProductAnalysis.java +++ b/Beginner/ProductAnalysis.java @@ -5,6 +5,8 @@ * Author: [hirushi1999] * Date: 2025-10-14 * Demonstrates how to use Java Streams to perform data analysis on a list of products. + * Time Complexity: O(n) (Linear time) + * Space Complexity: O(k) (Where k is the number of distinct categories) * Scenario: * - A store sells products in various categories (Fruits, Vegetables, Dairy, Meat). * - Each product has a price, quantity sold, and availability status.