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/9] 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 2/9] 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 a2f06093623ea9b3d0fd7e33684dfa286a692f4d Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 22:24:24 +0100 Subject: [PATCH 3/9] Added if instruction to constructor with conditions to throw exceptions --- Product.java | 25 ++++++++++++++----------- java-invoice | 1 + 2 files changed, 15 insertions(+), 11 deletions(-) create mode 160000 java-invoice diff --git a/Product.java b/Product.java index f42200be1..da36f3a6f 100644 --- a/Product.java +++ b/Product.java @@ -1,3 +1,8 @@ + +package pl.edu.agh.mwo.invoice.product; + +import java.math.BigDecimal; + public abstract class Product { private final String name; @@ -6,32 +11,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("Product name cannot be null"); - } - if (price == null || price.compareTo(BigDecimal.ZERO) < 0) { - throw new IllegalArgumentException("Product cannot be null"); - } + if (name == null || name.equals("")) { + + throw new IllegalArgumentException(Product name cannot be null"); + } + this.name = name; this.price = price; this.taxPercent = tax; } public String getName() { - return name; + return null; } public BigDecimal getPrice() { - return price; + return null; } public BigDecimal getTaxPercent() { - return taxPercent; + return null; } public BigDecimal getPriceWithTax() { - return this.price.add(this.taxPercent.multiply(this.price)); + 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 73aa954e28b5a9fc4ac8bbc587dd425e22d57032 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 22:30:09 +0100 Subject: [PATCH 4/9] Added if instruction to constructor with conditions to throw exeception for variable price --- Product.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Product.java b/Product.java index da36f3a6f..e4428f157 100644 --- a/Product.java +++ b/Product.java @@ -14,7 +14,9 @@ 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; From 0039fae2914da5fc4fc1704ac4d35bd25bc5df4f Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 22:33:17 +0100 Subject: [PATCH 5/9] return name not null in method getName() --- Product.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Product.java b/Product.java index e4428f157..b1fe67feb 100644 --- a/Product.java +++ b/Product.java @@ -24,7 +24,7 @@ protected Product(String name, BigDecimal price, BigDecimal tax) { } public String getName() { - return null; + return name; } public BigDecimal getPrice() { From 8dc59b07c5c984dc97d88fdeaca45ab3fe7a1a41 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 22:35:31 +0100 Subject: [PATCH 6/9] return price not null in method getPrice() --- Product.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Product.java b/Product.java index b1fe67feb..a0244a3c0 100644 --- a/Product.java +++ b/Product.java @@ -28,7 +28,7 @@ public String getName() { } public BigDecimal getPrice() { - return null; + return price; } public BigDecimal getTaxPercent() { From fc8755cdf6ffba32330f69e787f8702ad5c0a374 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 22:39:15 +0100 Subject: [PATCH 7/9] return taxPercent not null in method getTaxPerCent() --- Product.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Product.java b/Product.java index a0244a3c0..1e7f6bef4 100644 --- a/Product.java +++ b/Product.java @@ -32,7 +32,7 @@ public BigDecimal getPrice() { } public BigDecimal getTaxPercent() { - return null; + return taxPercent; } public BigDecimal getPriceWithTax() { From 5b6abc6556ae78b0772fca8e01b8c8341128fc51 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 22:42:55 +0100 Subject: [PATCH 8/9] return calculating method for variable price --- Product.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Product.java b/Product.java index 1e7f6bef4..2121d92d4 100644 --- a/Product.java +++ b/Product.java @@ -36,7 +36,7 @@ public BigDecimal getTaxPercent() { } public BigDecimal getPriceWithTax() { - return null; + return this.price.add(this.taxPercent.multiply(this.price)); } } From dd5079ae4c92072aafa155dce7aa1c348f097233 Mon Sep 17 00:00:00 2001 From: marcin7777 Date: Fri, 7 Feb 2020 22:44:33 +0100 Subject: [PATCH 9/9] Product.java entire code once again --- Product.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Product.java b/Product.java index 2121d92d4..45c2afdc3 100644 --- a/Product.java +++ b/Product.java @@ -1,4 +1,3 @@ - package pl.edu.agh.mwo.invoice.product; import java.math.BigDecimal; @@ -11,13 +10,12 @@ public abstract class Product { 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"); + 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; @@ -38,5 +36,6 @@ public BigDecimal getTaxPercent() { public BigDecimal getPriceWithTax() { return this.price.add(this.taxPercent.multiply(this.price)); } + }