From 2e94444bac854c7311e9360d920424631bcc0427 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/7] 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..720f0c9a5 --- /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..832ae2c96 --- /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 84b2b5fef6b876e7048ae8113497b889f47d3abc Mon Sep 17 00:00:00 2001 From: Aleksandra Wandzilak Date: Sun, 19 Jan 2020 09:54:22 +0100 Subject: [PATCH 2/7] Fixed the first three tests in product test --- src/main/java/pl/edu/agh/mwo/invoice/product/Product.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 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 b75785935..d013dc8db 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 @@ -16,18 +16,19 @@ protected Product(String name, BigDecimal price, BigDecimal tax) { } public String getName() { - return null; + return name; } public BigDecimal getPrice() { - return null; + return price; } public BigDecimal getTaxPercent() { - return null; + return taxPercent; } public BigDecimal getPriceWithTax() { +// return (this.price*=(this.taxPercent.add(100))); return null; } } From ff99d826c277cbaebda4b085f4ed71d065ea7485 Mon Sep 17 00:00:00 2001 From: Aleksandra Wandzilak Date: Sun, 19 Jan 2020 10:12:29 +0100 Subject: [PATCH 3/7] Fuck'n PriceWithTax finally works! --- src/main/java/pl/edu/agh/mwo/invoice/product/Product.java | 5 +++-- 1 file changed, 3 insertions(+), 2 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 d013dc8db..3f229e87a 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 @@ -28,7 +28,8 @@ public BigDecimal getTaxPercent() { } public BigDecimal getPriceWithTax() { -// return (this.price*=(this.taxPercent.add(100))); - return null; + BigDecimal a = new BigDecimal ("1"); + return (price.multiply((taxPercent.add(a)))); + } } From 25ebf13d48c0cd4ae9a0f255fe201c813bee5559 Mon Sep 17 00:00:00 2001 From: Aleksandra Wandzilak Date: Sun, 19 Jan 2020 10:41:12 +0100 Subject: [PATCH 4/7] All IllegalArgument test passed --- .../pl/edu/agh/mwo/invoice/product/Product.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 3f229e87a..1e9ecc275 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,6 +10,23 @@ public abstract class Product { private final BigDecimal taxPercent; protected Product(String name, BigDecimal price, BigDecimal tax) { + if (name == null) + { + throw new IllegalArgumentException("Product name can't be null!"); + } + if (name.isEmpty()) + { + throw new IllegalArgumentException(); + } + if (price == null) + { + throw new IllegalArgumentException(); + } + BigDecimal a = new BigDecimal ("0"); + if (price.compareTo(a) < 0) + { + throw new IllegalArgumentException(); + } this.name = name; this.price = price; this.taxPercent = tax; From f5e1406c6870daa6bb547b15911c938e688f0472 Mon Sep 17 00:00:00 2001 From: Aleksandra Wandzilak Date: Sun, 19 Jan 2020 12:14:24 +0100 Subject: [PATCH 5/7] I should have commited step by step, but I was too focused on fixing the problems. but now all test of Invoice class work :) --- .../java/pl/edu/agh/mwo/invoice/Invoice.java | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 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 28f0847a8..f3ae1013e 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,54 @@ import java.math.BigDecimal; import java.util.Collection; +import java.util.HashMap; +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 HashMap<>(); public void addProduct(Product product) { - // TODO: implement + this.addProduct(product, 1); +// if (products.containsKey(product)) { +// int a = products.get(product); +// products.put(product,a+1); +// } +// products.put(product,1); } public void addProduct(Product product, Integer quantity) { - // TODO: implement + if (products.containsKey(product)) { + int a = products.get(product); + products.put(product,quantity+1); + } + products.put(product,quantity); } - public BigDecimal getSubtotal() { - return null; + public BigDecimal getNetPrice() { + BigDecimal sum = new BigDecimal ("0"); + for(Product s : this.products.keySet()){ + Integer amount = this.products.get(s); + BigDecimal a = new BigDecimal(amount); + BigDecimal price = s.getPrice(); + sum = sum.add(price.multiply(a)); + } + return sum; } public BigDecimal getTax() { - return null; + return this.getGrossPrice().subtract(this.getNetPrice()); } - public BigDecimal getTotal() { - return null; + public BigDecimal getGrossPrice() { + BigDecimal gross = new BigDecimal("0"); + for(Product s : this.products.keySet()){ + BigDecimal a = new BigDecimal(this.products.get(s)); + BigDecimal tax = s.getPriceWithTax().subtract(s.getPrice()); + gross = gross.add(tax.multiply(a)); + } + return gross; } } From 8f90ca7b4016315c61a21aca825875acd02c1fab Mon Sep 17 00:00:00 2001 From: Aleksandra Wandzilak Date: Sun, 19 Jan 2020 12:16:25 +0100 Subject: [PATCH 6/7] class Invoice is implemented. All test work. --- .../java/pl/edu/agh/mwo/invoice/InvoiceTest.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java b/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java index 720f0c9a5..9010eada3 100644 --- a/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java +++ b/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java @@ -23,7 +23,7 @@ public void createEmptyInvoiceForTheTest() { @Test public void testEmptyInvoiceHasEmptySubtotal() { - Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getSubtotal())); + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getNetPrice())); } @Test @@ -33,14 +33,14 @@ public void testEmptyInvoiceHasEmptyTaxAmount() { @Test public void testEmptyInvoiceHasEmptyTotal() { - Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTotal())); + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getGrossPrice())); } @Test public void testInvoiceHasTheSameSubtotalAndTotalIfTaxIsZero() { Product taxFreeProduct = new TaxFreeProduct("Warzywa", new BigDecimal("199.99")); invoice.addProduct(taxFreeProduct); - Assert.assertThat(invoice.getTotal(), Matchers.comparesEqualTo(invoice.getSubtotal())); + Assert.assertThat(invoice.getGrossPrice(), Matchers.comparesEqualTo(invoice.getNetPrice())); } @Test @@ -48,7 +48,7 @@ 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())); + Assert.assertThat(new BigDecimal("310"), Matchers.comparesEqualTo(invoice.getNetPrice())); } @Test @@ -70,7 +70,7 @@ public void testInvoiceHasProperTotalValueForManyProduct() { 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())); + Assert.assertThat(new BigDecimal("320.30"), Matchers.comparesEqualTo(invoice.getGrossPrice())); } @Test @@ -81,7 +81,7 @@ public void testInvoiceHasPropoerSubtotalWithQuantityMoreThanOne() { 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())); + Assert.assertThat(new BigDecimal("50"), Matchers.comparesEqualTo(invoice.getNetPrice())); } @Test @@ -92,7 +92,7 @@ public void testInvoiceHasPropoerTotalWithQuantityMoreThanOne() { 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())); + Assert.assertThat(new BigDecimal("54.70"), Matchers.comparesEqualTo(invoice.getGrossPrice())); } @Test(expected = IllegalArgumentException.class) From ec7e100d7811cccc4571088579639e90df0702ef Mon Sep 17 00:00:00 2001 From: Aleksandra Wandzilak Date: Sun, 19 Jan 2020 12:17:20 +0100 Subject: [PATCH 7/7] class Product has been refactored --- .../java/pl/edu/agh/mwo/invoice/product/Product.java | 9 ++------- 1 file changed, 2 insertions(+), 7 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 1e9ecc275..01c670a73 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,20 +10,15 @@ public abstract class Product { private final BigDecimal taxPercent; protected Product(String name, BigDecimal price, BigDecimal tax) { - if (name == null) + if (name == null || name.isEmpty()) { throw new IllegalArgumentException("Product name can't be null!"); } - if (name.isEmpty()) - { - throw new IllegalArgumentException(); - } if (price == null) { throw new IllegalArgumentException(); } - BigDecimal a = new BigDecimal ("0"); - if (price.compareTo(a) < 0) + if (price.compareTo(new BigDecimal ("0")) < 0) { throw new IllegalArgumentException(); }