From 722f869ef8ed18cf7059f4fec424a94123081293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Fr=C4=85cz?= Date: Wed, 8 Jan 2020 15:31:13 +0100 Subject: [PATCH 1/4] Specs! --- .../pl/edu/agh/mwo/invoice/InvoiceTest.java | 107 ++++++++++++++++++ .../agh/mwo/invoice/product/ProductTest.java | 57 ++++++++++ 2 files changed, 164 insertions(+) create mode 100644 src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java create mode 100644 src/test/java/pl/edu/agh/mwo/invoice/product/ProductTest.java diff --git a/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java b/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java new file mode 100644 index 000000000..8dce08e28 --- /dev/null +++ b/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java @@ -0,0 +1,107 @@ +package pl.edu.agh.mwo.invoice; + +import java.math.BigDecimal; + +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import pl.edu.agh.mwo.invoice.Invoice; +import pl.edu.agh.mwo.invoice.product.DairyProduct; +import pl.edu.agh.mwo.invoice.product.OtherProduct; +import pl.edu.agh.mwo.invoice.product.Product; +import pl.edu.agh.mwo.invoice.product.TaxFreeProduct; + +public class InvoiceTest { + private Invoice invoice; + + @Before + public void createEmptyInvoiceForTheTest() { + invoice = new Invoice(); + } + + @Test + public void testEmptyInvoiceHasEmptySubtotal() { + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getSubtotal())); + } + + @Test + public void testEmptyInvoiceHasEmptyTaxAmount() { + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTax())); + } + + @Test + public void testEmptyInvoiceHasEmptyTotal() { + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTotal())); + } + + @Test + public void testInvoiceHasTheSameSubtotalAndTotalIfTaxIsZero() { + Product taxFreeProduct = new TaxFreeProduct("Warzywa", new BigDecimal("199.99")); + invoice.addProduct(taxFreeProduct); + Assert.assertThat(invoice.getTotal(), Matchers.comparesEqualTo(invoice.getSubtotal())); + } + + @Test + public void testInvoiceHasProperSubtotalForManyProducts() { + invoice.addProduct(new TaxFreeProduct("Owoce", new BigDecimal("200"))); + invoice.addProduct(new DairyProduct("Maslanka", new BigDecimal("100"))); + invoice.addProduct(new OtherProduct("Wino", new BigDecimal("10"))); + Assert.assertThat(new BigDecimal("310"), Matchers.comparesEqualTo(invoice.getSubtotal())); + } + + @Test + public void testInvoiceHasProperTaxValueForManyProduct() { + // tax: 0 + invoice.addProduct(new TaxFreeProduct("Pampersy", new BigDecimal("200"))); + // tax: 8 + invoice.addProduct(new DairyProduct("Kefir", new BigDecimal("100"))); + // tax: 2.30 + invoice.addProduct(new OtherProduct("Piwko", new BigDecimal("10"))); + Assert.assertThat(new BigDecimal("10.30"), Matchers.comparesEqualTo(invoice.getTax())); + } + + @Test + public void testInvoiceHasProperTotalValueForManyProduct() { + // price with tax: 200 + invoice.addProduct(new TaxFreeProduct("Maskotki", new BigDecimal("200"))); + // price with tax: 108 + invoice.addProduct(new DairyProduct("Maslo", new BigDecimal("100"))); + // price with tax: 12.30 + invoice.addProduct(new OtherProduct("Chipsy", new BigDecimal("10"))); + Assert.assertThat(new BigDecimal("320.30"), Matchers.comparesEqualTo(invoice.getTotal())); + } + + @Test + public void testInvoiceHasPropoerSubtotalWithQuantityMoreThanOne() { + // 2x kubek - price: 10 + invoice.addProduct(new TaxFreeProduct("Kubek", new BigDecimal("5")), 2); + // 3x kozi serek - price: 30 + invoice.addProduct(new DairyProduct("Kozi Serek", new BigDecimal("10")), 3); + // 1000x pinezka - price: 10 + invoice.addProduct(new OtherProduct("Pinezka", new BigDecimal("0.01")), 1000); + Assert.assertThat(new BigDecimal("50"), Matchers.comparesEqualTo(invoice.getSubtotal())); + } + + @Test + public void testInvoiceHasPropoerTotalWithQuantityMoreThanOne() { + // 2x chleb - price with tax: 10 + invoice.addProduct(new TaxFreeProduct("Chleb", new BigDecimal("5")), 2); + // 3x chedar - price with tax: 32.40 + invoice.addProduct(new DairyProduct("Chedar", new BigDecimal("10")), 3); + // 1000x pinezka - price with tax: 12.30 + invoice.addProduct(new OtherProduct("Pinezka", new BigDecimal("0.01")), 1000); + Assert.assertThat(new BigDecimal("54.70"), Matchers.comparesEqualTo(invoice.getTotal())); + } + + @Test(expected = IllegalArgumentException.class) + public void testInvoiceWithZeroQuantity() { + invoice.addProduct(new TaxFreeProduct("Tablet", new BigDecimal("1678")), 0); + } + + @Test(expected = IllegalArgumentException.class) + public void testInvoiceWithNegativeQuantity() { + invoice.addProduct(new DairyProduct("Zsiadle mleko", new BigDecimal("5.55")), -1); + } +} diff --git a/src/test/java/pl/edu/agh/mwo/invoice/product/ProductTest.java b/src/test/java/pl/edu/agh/mwo/invoice/product/ProductTest.java new file mode 100644 index 000000000..9b3c5bd6e --- /dev/null +++ b/src/test/java/pl/edu/agh/mwo/invoice/product/ProductTest.java @@ -0,0 +1,57 @@ +package pl.edu.agh.mwo.invoice.product; + +import java.math.BigDecimal; + +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Test; + +import pl.edu.agh.mwo.invoice.product.Product; + +public class ProductTest { + @Test + public void testProductNameIsCorrect() { + Product product = new OtherProduct("buty", new BigDecimal("100.0")); + Assert.assertEquals("buty", product.getName()); + } + + @Test + public void testProductPriceAndTaxWithDefaultTax() { + Product product = new OtherProduct("Ogorki", new BigDecimal("100.0")); + Assert.assertThat(new BigDecimal("100"), Matchers.comparesEqualTo(product.getPrice())); + Assert.assertThat(new BigDecimal("0.23"), Matchers.comparesEqualTo(product.getTaxPercent())); + } + + @Test + public void testProductPriceAndTaxWithDairyProduct() { + Product product = new DairyProduct("Szarlotka", new BigDecimal("100.0")); + Assert.assertThat(new BigDecimal("100"), Matchers.comparesEqualTo(product.getPrice())); + Assert.assertThat(new BigDecimal("0.08"), Matchers.comparesEqualTo(product.getTaxPercent())); + } + + @Test + public void testPriceWithTax() { + Product product = new DairyProduct("Oscypek", new BigDecimal("100.0")); + Assert.assertThat(new BigDecimal("108"), Matchers.comparesEqualTo(product.getPriceWithTax())); + } + + @Test(expected = IllegalArgumentException.class) + public void testProductWithNullName() { + new OtherProduct(null, new BigDecimal("100.0")); + } + + @Test(expected = IllegalArgumentException.class) + public void testProductWithEmptyName() { + new TaxFreeProduct("", new BigDecimal("100.0")); + } + + @Test(expected = IllegalArgumentException.class) + public void testProductWithNullPrice() { + new DairyProduct("Banany", null); + } + + @Test(expected = IllegalArgumentException.class) + public void testProductWithNegativePrice() { + new TaxFreeProduct("Mandarynki", new BigDecimal("-1.00")); + } +} From 64642ccb48277c79dd43152ae0f4718073cd2731 Mon Sep 17 00:00:00 2001 From: Magda Date: Sun, 17 Jan 2021 11:28:46 +0100 Subject: [PATCH 2/4] ProductTest is working --- .../pl/edu/agh/mwo/invoice/product/Product.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/pl/edu/agh/mwo/invoice/product/Product.java b/src/main/java/pl/edu/agh/mwo/invoice/product/Product.java index 318de9ac9..4490e7498 100644 --- a/src/main/java/pl/edu/agh/mwo/invoice/product/Product.java +++ b/src/main/java/pl/edu/agh/mwo/invoice/product/Product.java @@ -10,24 +10,30 @@ public abstract class Product { private final BigDecimal taxPercent; protected Product(String name, BigDecimal price, BigDecimal tax) { + if(name==null || name.equals("")) { + throw new IllegalArgumentException("You cannot create products whit null name"); + } + if(price==null || price.signum()==-1) { + throw new IllegalArgumentException("You cannot create products whit null name"); + } this.name = name; this.price = price; this.taxPercent = tax; } public String getName() { - return null; + return this.name; } public BigDecimal getPrice() { - return null; + return this.price; } public BigDecimal getTaxPercent() { - return null; + return this.taxPercent; } public BigDecimal getPriceWithTax() { - return null; + return this.price.multiply(this.taxPercent).add(this.price); } } From b86063fd46361c4eacd70f6b0d319c19fee15110 Mon Sep 17 00:00:00 2001 From: Magda Date: Sun, 17 Jan 2021 12:16:03 +0100 Subject: [PATCH 3/4] Kod etap po lab --- .../java/pl/edu/agh/mwo/invoice/Invoice.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java b/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java index 56fe02359..f6aa438aa 100644 --- a/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java +++ b/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java @@ -2,29 +2,37 @@ import java.math.BigDecimal; import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; import pl.edu.agh.mwo.invoice.product.Product; public class Invoice { - private Collection products; + //private Collection products; + private Map products = new LinkedHashMap<>(); public void addProduct(Product product) { - // TODO: implement + this.products.put(product, 1); } public void addProduct(Product product, Integer quantity) { - // TODO: implement + this.products.put(product, quantity); } public BigDecimal getSubtotal() { - return null; + BigDecimal sum = BigDecimal.ZERO; + for(Product product: this.products.keySet()) { + Integer quantity = this.products.get(product); + sum = sum.add(product.getPrice().multiply(new BigDecimal(quantity))); + } + return sum; } public BigDecimal getTax() { - return null; + return BigDecimal.ZERO; } public BigDecimal getTotal() { - return null; + return BigDecimal.ZERO; } } From a422d03938139869106e1e17fb06cedc634286b4 Mon Sep 17 00:00:00 2001 From: Magdalena Czesak Date: Sun, 14 Feb 2021 17:37:39 +0100 Subject: [PATCH 4/4] Add homework --- .../java/pl/edu/agh/mwo/invoice/Invoice.java | 68 ++++++++++-------- .../edu/agh/mwo/invoice/product/Product.java | 70 ++++++++++--------- 2 files changed, 76 insertions(+), 62 deletions(-) diff --git a/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java b/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java index f6aa438aa..0fc89c9e9 100644 --- a/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java +++ b/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java @@ -1,38 +1,50 @@ package pl.edu.agh.mwo.invoice; import java.math.BigDecimal; -import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; import pl.edu.agh.mwo.invoice.product.Product; public class Invoice { - //private Collection products; - private Map products = new LinkedHashMap<>(); - - public void addProduct(Product product) { - this.products.put(product, 1); - } - - public void addProduct(Product product, Integer quantity) { - this.products.put(product, quantity); - } - - public BigDecimal getSubtotal() { - BigDecimal sum = BigDecimal.ZERO; - for(Product product: this.products.keySet()) { - Integer quantity = this.products.get(product); - sum = sum.add(product.getPrice().multiply(new BigDecimal(quantity))); - } - return sum; - } - - public BigDecimal getTax() { - return BigDecimal.ZERO; - } - - public BigDecimal getTotal() { - return BigDecimal.ZERO; - } + //private Collection products; + private Map products = new LinkedHashMap<>(); + + public void addProduct(Product product) { + products.put(product, 1); + } + + public void addProduct(Product product, Integer quantity) { + if (quantity <= 0) { + throw new IllegalArgumentException("Cannot add product with negative or zero quantity"); + } + products.put(product, quantity); + } + + public BigDecimal getSubtotal() { + BigDecimal subTotal = BigDecimal.ZERO; + for (Product product : this.products.keySet()) { + Integer quantity = this.products.get(product); + subTotal = subTotal.add(product.getPrice().multiply(new BigDecimal(quantity))); + } + return subTotal; + } + + public BigDecimal getTax() { + BigDecimal tax = BigDecimal.ZERO; + BigDecimal percent = new BigDecimal("10"); + for (Product product : this.products.keySet()) { + tax = tax.add(product.getTaxPercent()).multiply(percent); + } + return tax; + } + + public BigDecimal getTotal() { + BigDecimal total = BigDecimal.ZERO; + for (Product product : this.products.keySet()) { + Integer quantity = this.products.get(product); + total = total.add(product.getPriceWithTax().multiply(new BigDecimal(quantity))); + } + return total; + } } diff --git a/src/main/java/pl/edu/agh/mwo/invoice/product/Product.java b/src/main/java/pl/edu/agh/mwo/invoice/product/Product.java index 4490e7498..673bfb6bf 100644 --- a/src/main/java/pl/edu/agh/mwo/invoice/product/Product.java +++ b/src/main/java/pl/edu/agh/mwo/invoice/product/Product.java @@ -3,37 +3,39 @@ import java.math.BigDecimal; public abstract class Product { - private final String name; - - private final BigDecimal price; - - private final BigDecimal taxPercent; - - protected Product(String name, BigDecimal price, BigDecimal tax) { - if(name==null || name.equals("")) { - throw new IllegalArgumentException("You cannot create products whit null name"); - } - if(price==null || price.signum()==-1) { - throw new IllegalArgumentException("You cannot create products whit null name"); - } - this.name = name; - this.price = price; - this.taxPercent = tax; - } - - public String getName() { - return this.name; - } - - public BigDecimal getPrice() { - return this.price; - } - - public BigDecimal getTaxPercent() { - return this.taxPercent; - } - - public BigDecimal getPriceWithTax() { - return this.price.multiply(this.taxPercent).add(this.price); - } -} + + private final String name; + + private final BigDecimal price; + + private final BigDecimal taxPercent; + + protected Product(String name, BigDecimal price, BigDecimal tax) { + + if (name == null || name.equals("")) { + throw new IllegalArgumentException("You cannot create products with null name!"); + } + this.name = name; + if (price == null || price.intValue() < 0) { + throw new IllegalArgumentException("You cannot create products with null or negative price!"); + } + this.price = price; + this.taxPercent = tax; + } + + public String getName() { + return name; + } + + public BigDecimal getPrice() { + return this.price; + } + + public BigDecimal getTaxPercent() { + return this.taxPercent; + } + + public BigDecimal getPriceWithTax() { + return this.price.add(this.price.multiply(this.taxPercent)); + } +} \ No newline at end of file