Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions verificar-limite/Product.java
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
62 changes: 62 additions & 0 deletions verificar-limite/ShoppingList.java
Original file line number Diff line number Diff line change
@@ -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<Product> 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<Product> 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();
}
}
95 changes: 95 additions & 0 deletions verificar-limite/ShoppingListTest.java
Original file line number Diff line number Diff line change
@@ -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());

}
}
43 changes: 43 additions & 0 deletions verificar-limite/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.gabriel</groupId>
<artifactId>verificar-limite</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
<!-- JUnit 5 (Jupiter) -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<!-- Definindo seus diretórios personalizados -->
<sourceDirectory>${project.basedir}</sourceDirectory>
<testSourceDirectory>${project.basedir}</testSourceDirectory>

<plugins>
<!-- Plugin para rodar os testes -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<useModulePath>false</useModulePath>
</configuration>
</plugin>
</plugins>
</build>
</project>