From 30e3648dc84acc07d4460e0b9f75eed30f07ae28 Mon Sep 17 00:00:00 2001 From: GabrielNNS Date: Fri, 24 Oct 2025 13:54:00 -0300 Subject: [PATCH] resolved --- verificar-limite/Product.java | 28 ++++++++ verificar-limite/ShoppingList.java | 62 +++++++++++++++++ verificar-limite/ShoppingListTest.java | 95 ++++++++++++++++++++++++++ verificar-limite/pom.xml | 43 ++++++++++++ 4 files changed, 228 insertions(+) create mode 100644 verificar-limite/Product.java create mode 100644 verificar-limite/ShoppingList.java create mode 100644 verificar-limite/ShoppingListTest.java create mode 100644 verificar-limite/pom.xml diff --git a/verificar-limite/Product.java b/verificar-limite/Product.java new file mode 100644 index 0000000..43d57d6 --- /dev/null +++ b/verificar-limite/Product.java @@ -0,0 +1,28 @@ +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class Product { + private String name; + private BigDecimal price; + + public Product(String name, BigDecimal price) { + this.name = name; + this.price = price.setScale(2, RoundingMode.HALF_UP); + } + + public String getName() { + return name; + } + + public BigDecimal getPrice() { + return price; + } + + @Override + public String toString() { + return "Product{" + + "name='" + name + '\'' + + ", price=" + price + + '}'; + } +} diff --git a/verificar-limite/ShoppingList.java b/verificar-limite/ShoppingList.java new file mode 100644 index 0000000..4848f91 --- /dev/null +++ b/verificar-limite/ShoppingList.java @@ -0,0 +1,62 @@ +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.List; + +public class ShoppingList { + private BigDecimal purchaseLimit; + private List productList; + + public ShoppingList(BigDecimal purchaseLimit) { + if(purchaseLimit.equals(new BigDecimal(0))) throw new IllegalArgumentException("Purchase Limit is zero or negative!"); + this.purchaseLimit = purchaseLimit.setScale(2, RoundingMode.HALF_UP); + this.productList = new ArrayList<>(); + } + + public BigDecimal getPurchaseLimit() { + return this.purchaseLimit; + } + + public List getProductList() { + return this.productList; + } + + public BigDecimal getTotalPriceProductList() { + BigDecimal totalPurchase = new BigDecimal(0); + for(Product p : productList) { + totalPurchase = totalPurchase.add(p.getPrice()); + } + return totalPurchase; + } + + public void addLimit(BigDecimal value) { + if(value.longValue() < BigDecimal.valueOf(0).longValue()) throw new IllegalArgumentException("Purchase Limit add negative!"); + purchaseLimit = purchaseLimit.add(value).setScale(2, RoundingMode.HALF_UP); + } + + private void discountLimit(BigDecimal value) { + purchaseLimit = purchaseLimit.subtract(value); + } + + public void addProduct(Product product) { + if(product == null) throw new IllegalArgumentException("Product is null!"); + productList.add(product); + } + + public void removeProduct(Product product) { + if(!productList.remove(product)) throw new IllegalArgumentException("Product non exists!"); + } + + public Boolean finishPurchase() { + BigDecimal totalPurchase = getTotalPriceProductList(); + if(purchaseLimit.longValue() < totalPurchase.longValue()) throw new RuntimeException("Insufficient purchase limit!"); + this.productList.clear(); + discountLimit(totalPurchase); + return true; + } + + public String getReport() { + return "PurchaceLimit: " + getPurchaseLimit() + + " | Products: " + getProductList(); + } +} diff --git a/verificar-limite/ShoppingListTest.java b/verificar-limite/ShoppingListTest.java new file mode 100644 index 0000000..2a79f9f --- /dev/null +++ b/verificar-limite/ShoppingListTest.java @@ -0,0 +1,95 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +import static org.junit.jupiter.api.Assertions.*; + +public class ShoppingListTest { + + private ShoppingList shoppingList; + private Product product; + private Product secondProduct; + + private static final String NAME_PRODUCT = "Test"; + private static final BigDecimal PRICE_PRODUCT = BigDecimal.valueOf(10); + + @BeforeEach + public void setUp() { + shoppingList = new ShoppingList(BigDecimal.valueOf(30.00)); + product = new Product(NAME_PRODUCT, PRICE_PRODUCT); + secondProduct = new Product(NAME_PRODUCT, PRICE_PRODUCT); + } + + @Test + public void shouldAddLimitWhenValueIsValid() { + BigDecimal limitExpected = BigDecimal.valueOf(50.00).setScale(2, RoundingMode.HALF_UP); + + shoppingList.addLimit(BigDecimal.valueOf(20)); + + assertEquals(limitExpected, shoppingList.getPurchaseLimit()); + } + + @Test + public void shouldAddProductsToProductListWhenValid() { + int sizeExpected = 2; + + shoppingList.addProduct(product); + shoppingList.addProduct(secondProduct); + + assertEquals(sizeExpected, shoppingList.getProductList().size()); + } + + @Test + public void shouldRemoveProductsToProductListWhenIndexValid() { + int sizeExpected = 1; + + shoppingList.addProduct(product); + shoppingList.addProduct(secondProduct); + + shoppingList.removeProduct(secondProduct); + + assertEquals(sizeExpected, shoppingList.getProductList().size()); + assertFalse(shoppingList.getProductList().contains(secondProduct)); + } + + @Test + public void shouldThrowExceptionWhenAddLimitIsNegativeArgument() { + assertThrows(IllegalArgumentException.class, () -> shoppingList.addLimit(BigDecimal.valueOf(-1))); + } + + @Test + public void shouldThrowExceptionWhenAddProductNull() { + Product productNull = null; + assertThrows(IllegalArgumentException.class, () -> shoppingList.addProduct(productNull)); + } + + @Test + public void shouldThrowExceptionWhenRemoveProductNonExistentInProductList() { + Product productOffList = new Product(NAME_PRODUCT, PRICE_PRODUCT); + assertThrows(IllegalArgumentException.class, () -> shoppingList.removeProduct(productOffList)); + } + + @Test + public void shouldThrowExceptionWhenFinishingPurchaseWithLimitInsufficient() { + Product productGreaterValue = new Product(NAME_PRODUCT, BigDecimal.valueOf(100)); + + shoppingList.addProduct(productGreaterValue); + + assertThrows(RuntimeException.class, () -> shoppingList.finishPurchase()); + } + + @Test + public void shouldFinishPurchaseWhenLimitAvailable() { + BigDecimal newLimitAvailable = BigDecimal.valueOf(10.00).setScale(2, RoundingMode.HALF_UP); + + shoppingList.addProduct(product); + shoppingList.addProduct(secondProduct); + + assertTrue(shoppingList.finishPurchase()); + assertEquals(newLimitAvailable, shoppingList.getPurchaseLimit()); + assertEquals(0, shoppingList.getProductList().size()); + + } +} diff --git a/verificar-limite/pom.xml b/verificar-limite/pom.xml new file mode 100644 index 0000000..54ed6e6 --- /dev/null +++ b/verificar-limite/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + + com.gabriel + verificar-limite + 1.0-SNAPSHOT + + + 17 + 17 + + + + + + org.junit.jupiter + junit-jupiter + 5.10.2 + test + + + + + + ${project.basedir} + ${project.basedir} + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.5 + + false + + + + + \ No newline at end of file