diff --git a/src/main/java/com/example/Category.java b/src/main/java/com/example/Category.java new file mode 100644 index 00000000..2282ad7c --- /dev/null +++ b/src/main/java/com/example/Category.java @@ -0,0 +1,38 @@ +package com.example; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public final class Category { + private static final Map CATEGORIES = new ConcurrentHashMap<>(); + private final String name; + + private Category(String name) { + if (name == null) throw new IllegalArgumentException("Category name can't be null"); + if (name.isBlank()) throw new IllegalArgumentException("Category name can't be blank"); + this.name = normalizedName(name); + + } + + public static Category of(String name) { + if (name == null) { + throw new IllegalArgumentException("Category name can't be null"); + } + if (name.isBlank()) { + throw new IllegalArgumentException("Category name can't be blank"); + } + name = normalizedName(name); + return CATEGORIES.computeIfAbsent(name, Category::new); + } + + public String getName() { + return name; + } + + public static String normalizedName(String name) { + return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase(); + } + +} + + diff --git a/src/main/java/com/example/ElectronicsProduct.java b/src/main/java/com/example/ElectronicsProduct.java new file mode 100644 index 00000000..a6377bb5 --- /dev/null +++ b/src/main/java/com/example/ElectronicsProduct.java @@ -0,0 +1,34 @@ +package com.example; + +import java.math.BigDecimal; +import java.util.UUID; + +public class ElectronicsProduct extends Product implements Shippable { + private final int warrantyMonths; + private final BigDecimal weight; + + protected ElectronicsProduct(UUID id, String name, Category category, BigDecimal price, int warrantyMonths, BigDecimal weight) { + super(id, name, category, price); + if (warrantyMonths < 0) throw new IllegalArgumentException("Warranty months cannot be negative."); + this.warrantyMonths = warrantyMonths; + this.weight = weight; + } + + public String productDetails() { + return "Electronics: " + name() + ", Warranty: " + warrantyMonths + " months"; + } + + public BigDecimal calculateShippingCost() { + BigDecimal shippingCost = BigDecimal.valueOf(79); + if (this.weight.compareTo(BigDecimal.valueOf(5.0)) > 0) { + return shippingCost.add(BigDecimal.valueOf(49)); + } else { + return shippingCost; + } + } + + public double weight() { + return this.weight.doubleValue(); + } + +} diff --git a/src/main/java/com/example/FoodProduct.java b/src/main/java/com/example/FoodProduct.java new file mode 100644 index 00000000..c934106a --- /dev/null +++ b/src/main/java/com/example/FoodProduct.java @@ -0,0 +1,37 @@ +package com.example; + +import java.math.BigDecimal; +import java.time.LocalDate; +import java.util.UUID; + +public class FoodProduct extends Product implements Perishable, Shippable { + private final LocalDate expirationDate; + private final BigDecimal weight; + + protected FoodProduct(UUID id, String name, Category category, BigDecimal price, LocalDate expirationDate, BigDecimal weight) { + if (price.doubleValue() < 0) throw new IllegalArgumentException("Price cannot be negative."); + if (weight.doubleValue() < 0) throw new IllegalArgumentException("Weight cannot be negative."); + super(id, name, category, price); + this.expirationDate = expirationDate; + this.weight = weight; + } + + + public String productDetails() { + return "Food: " + name() + ", Expires: " + this.expirationDate; + } + + public LocalDate expirationDate() { + return this.expirationDate; + } + + public BigDecimal calculateShippingCost() { + return this.weight.multiply(BigDecimal.valueOf(50)); + } + + @Override + public double weight() { + return this.weight.doubleValue(); + } + +} diff --git a/src/main/java/com/example/Perishable.java b/src/main/java/com/example/Perishable.java new file mode 100644 index 00000000..9c80d7cf --- /dev/null +++ b/src/main/java/com/example/Perishable.java @@ -0,0 +1,12 @@ +package com.example; + +import java.time.LocalDate; + +public interface Perishable { + + LocalDate expirationDate(); + + default boolean isExpired(LocalDate referenceDate) { + return expirationDate().isBefore(referenceDate); + } +} diff --git a/src/main/java/com/example/Product.java b/src/main/java/com/example/Product.java new file mode 100644 index 00000000..52bfdac4 --- /dev/null +++ b/src/main/java/com/example/Product.java @@ -0,0 +1,43 @@ +package com.example; + +import java.math.BigDecimal; +import java.util.Objects; +import java.util.UUID; + +public abstract class Product { + + private final UUID id; + private final String name; + private final Category category; + private BigDecimal price; + + Product(UUID id, String name, Category category, BigDecimal price) { + this.id = Objects.requireNonNull(id, "ID can't be null"); + this.name = Objects.requireNonNull(name, "Name can't be null"); + this.category = Objects.requireNonNull(category, "Category can't be null"); + this.price = Objects.requireNonNull(price, "Price can't be null"); + } + + public UUID uuid() { + return id; + } + + public String name() { + return name; + } + + public Category category() { + return category; + } + + public BigDecimal price() { + return price; + } + + public void setPrice(BigDecimal price) { + if (price == null || price.doubleValue() < 0) throw new IllegalArgumentException("Price can't be null or negative."); + this.price = price; + } + + public abstract String productDetails(); +} diff --git a/src/main/java/com/example/Shippable.java b/src/main/java/com/example/Shippable.java new file mode 100644 index 00000000..2e843115 --- /dev/null +++ b/src/main/java/com/example/Shippable.java @@ -0,0 +1,9 @@ +package com.example; + +import java.math.BigDecimal; + +public interface Shippable { + BigDecimal calculateShippingCost(); + + double weight(); +} diff --git a/src/main/java/com/example/Warehouse.java b/src/main/java/com/example/Warehouse.java new file mode 100644 index 00000000..4cc283ab --- /dev/null +++ b/src/main/java/com/example/Warehouse.java @@ -0,0 +1,116 @@ +package com.example; + +import java.time.LocalDate; +import java.util.stream.Collectors; +import java.math.BigDecimal; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; + + +public class Warehouse { + private static final Map WAREHOUSES = new ConcurrentHashMap<>(); + + private final String name; + + private final Map products; + + private final Set changedProductIds; + + private Warehouse(String name) { + this.name = name; + this.products = new ConcurrentHashMap<>(); + this.changedProductIds = Collections.newSetFromMap(new ConcurrentHashMap<>()); + } + + public static Warehouse getInstance(String name) { + if (name == null || name.isBlank()) { + throw new IllegalArgumentException("Warehouse name must be provided"); + } + return WAREHOUSES.computeIfAbsent(name, Warehouse::new); + } + + public static Warehouse getInstance() { + return getInstance("DefaultWarehouse"); + } + + public void addProduct(Product product) { + if (product == null) { + throw new IllegalArgumentException("Product cannot be null."); + } + Product existingProduct = products.putIfAbsent(product.uuid(), product); + if (existingProduct != null) { + throw new IllegalArgumentException("Product with that id already exists, use updateProduct for updates."); + } + + changedProductIds.add(product.uuid()); + + } + + public List getProducts() { + return products.values().stream().toList(); + } + + public Optional getProductById(UUID id) { + if (id == null) { + return Optional.empty(); + } + return Optional.ofNullable(products.get(id)); + } + + public Map> getProductsGroupedByCategories() { + return products.values() + .stream() + .collect(Collectors.groupingBy(Product::category)); + + } + + public void clearProducts() { + Warehouse warehouse = WAREHOUSES.get(name); + if (warehouse != null) { + warehouse.products.clear(); + warehouse.changedProductIds.clear(); + } + } + + public boolean isEmpty() { + return products.isEmpty(); + } + + public void updateProductPrice(UUID id, BigDecimal newPrice) { + Product product = getProductById(id). + orElseThrow(() -> new NoSuchElementException("Product not found with id: " + id)); + product.setPrice(newPrice); + changedProductIds.add(id); + } + + public List getChangedProducts() { + return changedProductIds.stream() + .filter(products::containsKey) + .map(products::get) + .toList(); + } + + public List expiredProducts() { + LocalDate today = LocalDate.now(); + return products.values().stream() + .filter(p -> p instanceof Perishable) + .filter(p -> ((Perishable) p).isExpired(today)) + .map(p -> (Perishable) p) + .collect(Collectors.toList()); + + } + + public List shippableProducts() { + return products.values().stream() + .filter(p -> p instanceof Shippable) + .map(p -> (Shippable) p) + .collect(Collectors.toList()); + } + + public void remove(UUID id) { + if (id != null) { + products.remove(id); + changedProductIds.remove(id); + } + } +} diff --git a/src/main/java/com/example/WarehouseAnalyzer.java b/src/main/java/com/example/WarehouseAnalyzer.java index 1779fc33..04374d4e 100644 --- a/src/main/java/com/example/WarehouseAnalyzer.java +++ b/src/main/java/com/example/WarehouseAnalyzer.java @@ -1,7 +1,7 @@ package com.example; +import java.io.Serializable; import java.math.BigDecimal; -import java.math.MathContext; import java.math.RoundingMode; import java.time.LocalDate; import java.util.*; @@ -138,31 +138,45 @@ public Map calculateWeightedAveragePriceByCategory() { } /** - * Identifies products whose price deviates from the mean by more than the specified - * number of standard deviations. Uses population standard deviation over all products. - * Test expectation: with a mostly tight cluster and two extremes, calling with 2.0 returns the two extremes. + * Identifies products whose prices are statistical outliers, + * using the interquartile range (IQR) method. + * Products with prices outside the IQR threshold are considered outliers. + * Test expectation: with a mostly tight cluster and two extremes, + * calling with 1.5 (the typical IQR threshold) returns the two extremes. * - * @param standardDeviations threshold in standard deviations (e.g., 2.0) + * @param factor multiplier in IQR calculation * @return list of products considered outliers */ - public List findPriceOutliers(double standardDeviations) { + public List findPriceOutliers(double factor) { List products = warehouse.getProducts(); - int n = products.size(); - if (n == 0) return List.of(); - double sum = products.stream().map(Product::price).mapToDouble(bd -> bd.doubleValue()).sum(); - double mean = sum / n; - double variance = products.stream() - .map(Product::price) - .mapToDouble(bd -> Math.pow(bd.doubleValue() - mean, 2)) - .sum() / n; - double std = Math.sqrt(variance); - double threshold = standardDeviations * std; - List outliers = new ArrayList<>(); - for (Product p : products) { - double diff = Math.abs(p.price().doubleValue() - mean); - if (diff > threshold) outliers.add(p); - } - return outliers; + if (products.size() < 4) return List.of(); + + List prices = products.stream() + .map(p -> p.price().doubleValue()) + .sorted() + .toList(); + + int n = prices.size(); + double q1 = median(prices.subList(0, n / 2)); + double q3 = median(prices.subList((n + 1) / 2, n)); + double iqr = q3 - q1; + + double lowerLimit = q1 - factor * iqr; + double upperLimit = q3 + factor * iqr; + + return products.stream() + .filter(p -> { + double price = p.price().doubleValue(); + return price < lowerLimit || price > upperLimit; + }) + .toList(); + } + + private double median(List sortedList){ + int n = sortedList.size(); + return n % 2 == 0 + ? (sortedList.get(n/2 - 1) + sortedList.get(n/2)) / 2.0 + : sortedList.get(n/2); } /** @@ -245,7 +259,6 @@ public Map calculateExpirationBasedDiscounts() { * when percentage exceeds 70%. * - Category diversity: count of distinct categories in the inventory. The tests expect at least 2. * - Convenience booleans: highValueWarning (percentage > 70%) and minimumDiversity (category count >= 2). - * * Note: The exact high-value threshold is implementation-defined, but the provided tests create a clear * separation using very expensive electronics (e.g., 2000) vs. low-priced food items (e.g., 10), * allowing percentage computation regardless of the chosen cutoff as long as it matches the scenario. @@ -295,7 +308,7 @@ public InventoryStatistics getInventoryStatistics() { /** * Represents a group of products for shipping */ -class ShippingGroup { +class ShippingGroup implements Serializable { private final List products; private final Double totalWeight; private final BigDecimal totalShippingCost; diff --git a/src/test/java/com/example/BasicTest.java b/src/test/java/com/example/BasicTest.java index a11fc976..8a45877e 100644 --- a/src/test/java/com/example/BasicTest.java +++ b/src/test/java/com/example/BasicTest.java @@ -6,6 +6,7 @@ import java.math.BigDecimal; import java.time.LocalDate; import java.util.List; +import java.util.Map; import java.util.NoSuchElementException; import java.util.UUID; @@ -118,22 +119,149 @@ void should_beEmpty_when_newlySetUp() { .isTrue(); } + // --- Singleton and Factory Pattern Tests --- + @Nested @DisplayName("Factory and Singleton Behavior") class FactoryTests { - // ... (omitted for brevity, same as before) + + @Test + @DisplayName("✅ should not have any public constructors") + void should_notHavePublicConstructors() { + Constructor[] constructors = Warehouse.class.getConstructors(); + assertThat(constructors) + .as("Warehouse should only be accessed via its getInstance() factory method.") + .isEmpty(); + } + + @Test + @DisplayName("✅ should be created by calling the 'getInstance' factory method") + void should_beCreated_when_usingFactoryMethod() { + Warehouse defaultWarehouse = Warehouse.getInstance(); + assertThat(defaultWarehouse).isNotNull(); + } + + @Test + @DisplayName("✅ should return the same instance for the same name") + void should_returnSameInstance_when_nameIsIdentical() { + Warehouse warehouse1 = Warehouse.getInstance("GlobalStore"); + Warehouse warehouse2 = Warehouse.getInstance("GlobalStore"); + assertThat(warehouse1) + .as("Warehouses with the same name should be the same singleton instance.") + .isSameAs(warehouse2); + } } @Nested @DisplayName("Product Management") class ProductManagementTests { - // ... (most tests omitted for brevity, same as before) + @Test + @DisplayName("✅ should be empty when new") + void should_beEmpty_when_new() { + assertThat(warehouse.isEmpty()) + .as("A new warehouse instance should have no products.") + .isTrue(); + } + + @Test + @DisplayName("✅ should return an empty product list when new") + void should_returnEmptyProductList_when_new() { + assertThat(warehouse.getProducts()) + .as("A new warehouse should return an empty list, not null.") + .isEmpty(); + } + + @Test + @DisplayName("✅ should store various product types (Food, Electronics)") + void should_storeHeterogeneousProducts() { + // Arrange + Product milk = new FoodProduct(UUID.randomUUID(), "Milk", Category.of("Dairy"), new BigDecimal("15.50"), LocalDate.now().plusDays(7), new BigDecimal("1.0")); + Product laptop = new ElectronicsProduct(UUID.randomUUID(), "Laptop", Category.of("Electronics"), new BigDecimal("12999"), 24, new BigDecimal("2.2")); + + // Act + warehouse.addProduct(milk); + warehouse.addProduct(laptop); + + // Assert + assertThat(warehouse.getProducts()) + .as("Warehouse should correctly store different subtypes of Product.") + .hasSize(2) + .containsExactlyInAnyOrder(milk, laptop); + } + + + + @Test + @DisplayName("❌ should throw an exception when adding a product with a duplicate ID") + void should_throwException_when_addingProductWithDuplicateId() { + // Arrange + UUID sharedId = UUID.randomUUID(); + Product milk = new FoodProduct(sharedId, "Milk", Category.of("Dairy"), BigDecimal.ONE, LocalDate.now(), BigDecimal.ONE); + Product cheese = new FoodProduct(sharedId, "Cheese", Category.of("Dairy"), BigDecimal.TEN, LocalDate.now(), BigDecimal.TEN); + warehouse.addProduct(milk); + + // Act & Assert + assertThatThrownBy(() -> warehouse.addProduct(cheese)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Product with that id already exists, use updateProduct for updates."); + } + + @Test + @DisplayName("✅ should update the price of an existing product") + void should_updateExistingProductPrice() { + // Arrange + Product milk = new FoodProduct(UUID.randomUUID(), "Milk", Category.of("Dairy"), new BigDecimal("15.50"), LocalDate.now().plusDays(7), new BigDecimal("1.0")); + warehouse.addProduct(milk); + BigDecimal newPrice = new BigDecimal("17.00"); + + // Act + warehouse.updateProductPrice(milk.uuid(), newPrice); + + // Assert + assertThat(warehouse.getProductById(milk.uuid())) + .as("The product's price should be updated to the new value.") + .isPresent() + .hasValueSatisfying(product -> + assertThat(product.price()).isEqualByComparingTo(newPrice) + ); + } + + @Test + @DisplayName("✅ should group products correctly by their category") + void should_groupProductsByCategories() { + // Arrange + Product milk = new FoodProduct(UUID.randomUUID(), "Milk", Category.of("Dairy"), BigDecimal.ONE, LocalDate.now(), BigDecimal.ONE); + Product apple = new FoodProduct(UUID.randomUUID(), "Apple", Category.of("Fruit"), BigDecimal.ONE, LocalDate.now(), BigDecimal.ONE); + Product laptop = new ElectronicsProduct(UUID.randomUUID(), "Laptop", Category.of("Electronics"), BigDecimal.TEN, 24, BigDecimal.TEN); + warehouse.addProduct(milk); + warehouse.addProduct(apple); + warehouse.addProduct(laptop); + + Map> expectedMap = Map.of( + Category.of("Dairy"), List.of(milk), + Category.of("Fruit"), List.of(apple), + Category.of("Electronics"), List.of(laptop) + ); + + // Act & Assert + assertThat(warehouse.getProductsGroupedByCategories()) + .as("The returned map should have categories as keys and lists of products as values.") + .isEqualTo(expectedMap); + } @Test @DisplayName("🔒 should return an unmodifiable list of products to protect internal state") void should_returnUnmodifiableProductList() { - // ... (same as before) + // Arrange + Product milk = new FoodProduct(UUID.randomUUID(), "Milk", Category.of("Dairy"), BigDecimal.ONE, LocalDate.now(), BigDecimal.ONE); + warehouse.addProduct(milk); + List products = warehouse.getProducts(); + + // Act & Assert + assertThatThrownBy(products::clear) + .as("The list returned by getProducts() should be immutable to prevent external modification.") + .isInstanceOf(UnsupportedOperationException.class); } @Test diff --git a/src/test/java/com/example/EdgeCaseTest.java b/src/test/java/com/example/EdgeCaseTest.java index fb4f9358..c35f61a4 100644 --- a/src/test/java/com/example/EdgeCaseTest.java +++ b/src/test/java/com/example/EdgeCaseTest.java @@ -188,9 +188,9 @@ void should_calculateWeightedAveragePrice_byCategory() { @Test @DisplayName("📊 should identify products with abnormal pricing (outliers)") /** - * Detects price outliers using mean and standard deviation. + * Detects price outliers using the interquartile range (IQR) method. * Arrange: mostly normal-priced items around 15, plus very cheap and very expensive outliers. - * Act: analyzer.findPriceOutliers(2.0). + * Act: analyzer.findPriceOutliers(1.5) - considers prices outside 1.5×IQR from Q1 or Q3 as outliers. * Assert: returns exactly the two outliers ("Expensive" and "Cheap"). */ void should_identifyPriceOutliers_usingStatistics() { @@ -207,12 +207,12 @@ void should_identifyPriceOutliers_usingStatistics() { warehouse.addProduct(outlierHigh); warehouse.addProduct(outlierLow); - // Act - Find outliers (products with price > 2 standard deviations from mean) - List outliers = analyzer.findPriceOutliers(2.0); // 2 standard deviations + // Act - Find outliers (products with prices outside 1.5×IQR from the interquartile range) + List outliers = analyzer.findPriceOutliers(1.5); // 1.5xIQR threshold // Assert assertThat(outliers) - .as("Should identify statistical outliers beyond 2 standard deviations") + .as("Should identify price outliers beyond the 1.5×IQR threshold") .hasSize(2) .extracting(Product::name) .containsExactlyInAnyOrder("Expensive", "Cheap");