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 01/16] 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 1fa7412752925739cb1e2bc95116a4dc08b0c780 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Thu, 6 Feb 2020 23:10:01 +0100 Subject: [PATCH 02/16] First change in Product class that is about price in class constructor --- Product.java | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Product.java diff --git a/Product.java b/Product.java new file mode 100644 index 000000000..f42200be1 --- /dev/null +++ b/Product.java @@ -0,0 +1,37 @@ +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("Product name cannot be null"); + } + if (price == null || price.compareTo(BigDecimal.ZERO) < 0) { + throw new IllegalArgumentException("Product cannot be null"); + } + this.name = name; + this.price = price; + this.taxPercent = tax; + } + + public String getName() { + return name; + } + + public BigDecimal getPrice() { + return price; + } + + public BigDecimal getTaxPercent() { + return taxPercent; + } + + public BigDecimal getPriceWithTax() { + return this.price.add(this.taxPercent.multiply(this.price)); + } + +} + From b438387c4944daf51d2552017912f22e1fc5a805 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 18:07:14 +0100 Subject: [PATCH 03/16] Change code in line 9, created HashMap --- Invoice.java | 33 +++++++++++++++++++++++++++++++++ java-invoice | 1 + 2 files changed, 34 insertions(+) create mode 100644 Invoice.java create mode 160000 java-invoice diff --git a/Invoice.java b/Invoice.java new file mode 100644 index 000000000..34a49a157 --- /dev/null +++ b/Invoice.java @@ -0,0 +1,33 @@ +package pl.edu.agh.mwo.invoice; + +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 Map products = new HashMap<>(); + + public void addProduct(Product product) { + // TODO: implement + } + + public void addProduct(Product product, Integer quantity) { + // TODO: implement + } + + public BigDecimal getSubtotal() { + return null; + } + + public BigDecimal getTax() { + return null; + } + + public BigDecimal getTotal() { + return null; + } +} + diff --git a/java-invoice b/java-invoice new file mode 160000 index 000000000..c9f6e9205 --- /dev/null +++ b/java-invoice @@ -0,0 +1 @@ +Subproject commit c9f6e92050237c5f2de729304ffcf85a826b8b57 From b69c10466d02617ec777658a82a1811023ec777b Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 19:18:46 +0100 Subject: [PATCH 04/16] Created fields and class Invoice constructor --- Invoice.java | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Invoice.java b/Invoice.java index 34a49a157..6a09e3f0f 100644 --- a/Invoice.java +++ b/Invoice.java @@ -1,16 +1,29 @@ package pl.edu.agh.mwo.invoice; import java.math.BigDecimal; +import java.time.LocalDate; +import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; -import java.util.Map; import pl.edu.agh.mwo.invoice.product.Product; public class Invoice { - private Map products = new HashMap<>(); + private Collection products; + private BigDecimal netValue; + private BigDecimal total; + private BigDecimal tax; + + + Invoice() { + netValue = new BigDecimal("0"); + total = new BigDecimal("0"); + tax = new BigDecimal("0"); + products = new ArrayList(); - public void addProduct(Product product) { + } + + +public void addProduct(Product product) { // TODO: implement } @@ -28,6 +41,6 @@ public BigDecimal getTax() { public BigDecimal getTotal() { return null; - } + } } From 589156f07bc3dc91f4cf735e7d50f21a1d893de7 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 19:22:13 +0100 Subject: [PATCH 05/16] Created an implemetation of method addProduct --- Invoice.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Invoice.java b/Invoice.java index 6a09e3f0f..edc746d8c 100644 --- a/Invoice.java +++ b/Invoice.java @@ -24,7 +24,8 @@ public class Invoice { public void addProduct(Product product) { - // TODO: implement + products.add(Product); + update(); } public void addProduct(Product product, Integer quantity) { From 9510f9d7f693d2e8f421ba43d467daa92df9ea0d Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 19:32:46 +0100 Subject: [PATCH 06/16] Created an implementation of addProduct method --- Invoice.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Invoice.java b/Invoice.java index edc746d8c..c294618ef 100644 --- a/Invoice.java +++ b/Invoice.java @@ -23,13 +23,19 @@ public class Invoice { } -public void addProduct(Product product) { + public void addProduct(Product product) { products.add(Product); update(); } public void addProduct(Product product, Integer quantity) { - // TODO: implement + if (quantity <= 0) { + throw new IllegalArgumentException(); + } + for (int i =0; i < quantity; i++) { + products.add(product); + } + update(); } public BigDecimal getSubtotal() { From 9dbbb0dc10b97e2aa8c7b145b024dd611874d592 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 20:02:37 +0100 Subject: [PATCH 07/16] Created method setNetValue() with an implementation --- Invoice.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Invoice.java b/Invoice.java index c294618ef..32593577b 100644 --- a/Invoice.java +++ b/Invoice.java @@ -7,6 +7,7 @@ import pl.edu.agh.mwo.invoice.product.Product; + public class Invoice { private Collection products; private BigDecimal netValue; @@ -37,6 +38,13 @@ public void addProduct(Product product, Integer quantity) { } update(); } + public void setNetValue() { + + netValue = BigDecimal.ZERO; + for (Product product: products) { + netValue = netValue.add(product.getPrice()); + } + } public BigDecimal getSubtotal() { return null; From 81064d282e39435bc0cc83676d2588b4c445f3cf Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 20:25:48 +0100 Subject: [PATCH 08/16] Renamed field netValue to subTotal to avoid errors in testing --- Invoice.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Invoice.java b/Invoice.java index 32593577b..f3f5202ed 100644 --- a/Invoice.java +++ b/Invoice.java @@ -10,7 +10,7 @@ public class Invoice { private Collection products; - private BigDecimal netValue; + private BigDecimal subTotal; private BigDecimal total; private BigDecimal tax; @@ -38,11 +38,11 @@ public void addProduct(Product product, Integer quantity) { } update(); } - public void setNetValue() { + public void setSubTotal() { - netValue = BigDecimal.ZERO; + subTotal = BigDecimal.ZERO; for (Product product: products) { - netValue = netValue.add(product.getPrice()); + subTotal = subTotal.add(product.getPrice()); } } From 72c3d5746aab1af713899dce43baed459dddaf34 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 20:54:07 +0100 Subject: [PATCH 09/16] In getSubtotal method return subTotal not null --- Invoice.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Invoice.java b/Invoice.java index f3f5202ed..18ad14c9d 100644 --- a/Invoice.java +++ b/Invoice.java @@ -47,9 +47,10 @@ public void setSubTotal() { } public BigDecimal getSubtotal() { - return null; + return subTotal; } + public BigDecimal getTax() { return null; } From b21f19db2dd3c0f47e6caf0e8889e690dc2f8f6a Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 21:00:38 +0100 Subject: [PATCH 10/16] The method setTax created and implemeted --- Invoice.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Invoice.java b/Invoice.java index 18ad14c9d..f88cd83ac 100644 --- a/Invoice.java +++ b/Invoice.java @@ -50,7 +50,10 @@ public BigDecimal getSubtotal() { return subTotal; } + public void setTax() { + tax = total.subtract(subTotal); + } public BigDecimal getTax() { return null; } From 0c30f6fff8bae29c675cfb63b185321ebf0d9a47 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 21:03:01 +0100 Subject: [PATCH 11/16] In method getTotal() return total --- Invoice.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Invoice.java b/Invoice.java index f88cd83ac..b6d0d809a 100644 --- a/Invoice.java +++ b/Invoice.java @@ -55,7 +55,7 @@ public void setTax() { } public BigDecimal getTax() { - return null; + return tax; } public BigDecimal getTotal() { From cad909ca0636758d0a79987b57fabba2a3300890 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 21:07:08 +0100 Subject: [PATCH 12/16] In method getTotal() must do it again --- Invoice.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Invoice.java b/Invoice.java index b6d0d809a..054632dc7 100644 --- a/Invoice.java +++ b/Invoice.java @@ -59,7 +59,7 @@ public BigDecimal getTax() { } public BigDecimal getTotal() { - return null; + return total; } } From 0dcc73c29975a6a1882493e9ce3d60e07bd48552 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 21:11:11 +0100 Subject: [PATCH 13/16] Created and implemeted method setTotal() --- Invoice.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Invoice.java b/Invoice.java index 054632dc7..af923d3b3 100644 --- a/Invoice.java +++ b/Invoice.java @@ -61,5 +61,14 @@ public BigDecimal getTax() { public BigDecimal getTotal() { return total; } + + public void setTotal() { + + total = BigDecimal.ZERO; + for (Product product: products) { + total = total.add(product.getPriceWithTax()); + } + } + } From 07159d9dd0de11513d0693b01f0782fa72e426b6 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 21:14:27 +0100 Subject: [PATCH 14/16] Created public static Invoice create() method --- Invoice.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Invoice.java b/Invoice.java index af923d3b3..1060ec7b1 100644 --- a/Invoice.java +++ b/Invoice.java @@ -69,6 +69,9 @@ public void setTotal() { total = total.add(product.getPriceWithTax()); } } + public static Invoice create() { + return new Invoice(); + } } From 89506bb527629500ebc09b2bc1b8354b1a2c7fd6 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 21:25:49 +0100 Subject: [PATCH 15/16] Created update() method --- Invoice.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Invoice.java b/Invoice.java index 1060ec7b1..0159659be 100644 --- a/Invoice.java +++ b/Invoice.java @@ -72,6 +72,11 @@ public void setTotal() { public static Invoice create() { return new Invoice(); } - + + public void update() { + setSubtotal(); + setTotal(); + setTax(); + } } From d83f42beedbbf2d94080db2a9009449b3a2a6e7d Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 21:36:00 +0100 Subject: [PATCH 16/16] Entire class Invoice once again --- Invoice.java | 67 +++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/Invoice.java b/Invoice.java index 0159659be..4c28b4d5c 100644 --- a/Invoice.java +++ b/Invoice.java @@ -1,13 +1,11 @@ package pl.edu.agh.mwo.invoice; import java.math.BigDecimal; -import java.time.LocalDate; import java.util.ArrayList; import java.util.Collection; import pl.edu.agh.mwo.invoice.product.Product; - public class Invoice { private Collection products; private BigDecimal subTotal; @@ -16,67 +14,66 @@ public class Invoice { Invoice() { - netValue = new BigDecimal("0"); + subTotal = new BigDecimal("0"); total = new BigDecimal("0"); tax = new BigDecimal("0"); products = new ArrayList(); } - - public void addProduct(Product product) { - products.add(Product); + public void addProduct(Product product) { + products.add(product); update(); } public void addProduct(Product product, Integer quantity) { if (quantity <= 0) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException(); } - for (int i =0; i < quantity; i++) { - products.add(product); - } - update(); - } - public void setSubTotal() { - - subTotal = BigDecimal.ZERO; - for (Product product: products) { - subTotal = subTotal.add(product.getPrice()); + for (int i = 0; i < quantity; i++) { + products.add(product); } + update(); } - public BigDecimal getSubtotal() { return subTotal; } + public void setSubtotal() { + subTotal = BigDecimal.ZERO; + for (Product product : products) { + subTotal = subTotal.add(product.getPrice()); + } + } - public void setTax() { - tax = total.subtract(subTotal); - - } public BigDecimal getTax() { return tax; } + + public void setTax() { + tax = total.subtract(subTotal); + } public BigDecimal getTotal() { return total; - } - + } public void setTotal() { - - total = BigDecimal.ZERO; - for (Product product: products) { - total = total.add(product.getPriceWithTax()); + total = BigDecimal.ZERO; + for (Product product : products) { + total = total.add(product.getPriceWithTax()); } } + public static Invoice create() { - return new Invoice(); - } - - public void update() { - setSubtotal(); - setTotal(); - setTax(); + return new Invoice(); + } + + public void update() { + setSubtotal(); + setTotal(); + setTax(); } + + + }