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 01/10] 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 d3ed707d2710d73093106c4bc10489d75d4ef7e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Fr=C4=85cz?= Date: Sat, 11 Feb 2017 23:58:33 +0100 Subject: [PATCH 02/10] Working all Invoice and Product tests --- .../java/pl/edu/agh/mwo/invoice/Invoice.java | 35 +++++++++++++++---- .../edu/agh/mwo/invoice/product/Product.java | 16 ++++++--- 2 files changed, 40 insertions(+), 11 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..8599ebb76 100644 --- a/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java +++ b/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java @@ -1,30 +1,51 @@ package pl.edu.agh.mwo.invoice; import java.math.BigDecimal; -import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; import pl.edu.agh.mwo.invoice.product.Product; public class Invoice { - private Collection products; + private Map products = new HashMap(); public void addProduct(Product product) { - // TODO: implement + this.products.put(product, 1); } public void addProduct(Product product, Integer quantity) { - // TODO: implement + if (quantity <= 0) { + throw new IllegalArgumentException(); + } + this.products.put(product, quantity); } public BigDecimal getSubtotal() { - return null; + return getTotal().subtract(getTax()); } public BigDecimal getTax() { - return null; + BigDecimal totalTax = new BigDecimal(0); + Iterator> it = products.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + BigDecimal productTax = pair.getKey().getTaxOnly(); + BigDecimal quantity = new BigDecimal(pair.getValue()); + totalTax = totalTax.add(productTax.multiply(quantity)); + } + return totalTax; } public BigDecimal getTotal() { - return null; + BigDecimal total = new BigDecimal(0); + Iterator> it = products.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + BigDecimal productPriceWithTax = pair.getKey().getPriceWithTax(); + BigDecimal quantity = new BigDecimal(pair.getValue()); + total = total.add(productPriceWithTax.multiply(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 318de9ac9..b73269d75 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,32 @@ public abstract class Product { private final BigDecimal taxPercent; protected Product(String name, BigDecimal price, BigDecimal tax) { + if (name == null || name.equals("") || price == null || tax == null || tax.compareTo(new BigDecimal(0)) < 0 + || price.compareTo(new BigDecimal(0)) < 0) { + throw new IllegalArgumentException(); + } this.name = name; this.price = price; this.taxPercent = 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 null; + return price.add(getTaxOnly()); + } + + public BigDecimal getTaxOnly() { + return price.multiply(taxPercent); } } From ed3e3bbd519144068df8c7fff3cc8df55acd580a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Fr=C4=85cz?= Date: Fri, 24 Feb 2017 23:32:19 +0100 Subject: [PATCH 03/10] Simplify invoice implementation --- .../java/pl/edu/agh/mwo/invoice/Invoice.java | 43 ++++++++----------- .../edu/agh/mwo/invoice/product/Product.java | 6 +-- .../pl/edu/agh/mwo/invoice/InvoiceTest.java | 18 ++++---- 3 files changed, 28 insertions(+), 39 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 8599ebb76..72f4048d5 100644 --- a/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java +++ b/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java @@ -2,7 +2,6 @@ import java.math.BigDecimal; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; import pl.edu.agh.mwo.invoice.product.Product; @@ -11,41 +10,35 @@ public class Invoice { private Map products = new HashMap(); public void addProduct(Product product) { - this.products.put(product, 1); + addProduct(product, 1); } public void addProduct(Product product, Integer quantity) { - if (quantity <= 0) { + if (product == null || quantity <= 0) { throw new IllegalArgumentException(); } - this.products.put(product, quantity); + products.put(product, quantity); } - public BigDecimal getSubtotal() { - return getTotal().subtract(getTax()); + public BigDecimal getNetTotal() { + BigDecimal totalNet = BigDecimal.ZERO; + for (Product product : products.keySet()) { + BigDecimal quantity = new BigDecimal(products.get(product)); + totalNet = totalNet.add(product.getPrice().multiply(quantity)); + } + return totalNet; } - public BigDecimal getTax() { - BigDecimal totalTax = new BigDecimal(0); - Iterator> it = products.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = it.next(); - BigDecimal productTax = pair.getKey().getTaxOnly(); - BigDecimal quantity = new BigDecimal(pair.getValue()); - totalTax = totalTax.add(productTax.multiply(quantity)); - } - return totalTax; + public BigDecimal getTaxTotal() { + return getGrossTotal().subtract(getNetTotal()); } - public BigDecimal getTotal() { - BigDecimal total = new BigDecimal(0); - Iterator> it = products.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = it.next(); - BigDecimal productPriceWithTax = pair.getKey().getPriceWithTax(); - BigDecimal quantity = new BigDecimal(pair.getValue()); - total = total.add(productPriceWithTax.multiply(quantity)); + public BigDecimal getGrossTotal() { + BigDecimal totalGross = BigDecimal.ZERO; + for (Product product : products.keySet()) { + BigDecimal quantity = new BigDecimal(products.get(product)); + totalGross = totalGross.add(product.getPriceWithTax().multiply(quantity)); } - return total; + return totalGross; } } 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 b73269d75..cd0f86a48 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 @@ -32,10 +32,6 @@ public BigDecimal getTaxPercent() { } public BigDecimal getPriceWithTax() { - return price.add(getTaxOnly()); - } - - public BigDecimal getTaxOnly() { - return price.multiply(taxPercent); + return price.multiply(taxPercent).add(price); } } 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 8dce08e28..920b9a99c 100644 --- a/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java +++ b/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java @@ -23,24 +23,24 @@ public void createEmptyInvoiceForTheTest() { @Test public void testEmptyInvoiceHasEmptySubtotal() { - Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getSubtotal())); + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getNetTotal())); } @Test public void testEmptyInvoiceHasEmptyTaxAmount() { - Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTax())); + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTaxTotal())); } @Test public void testEmptyInvoiceHasEmptyTotal() { - Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTotal())); + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getGrossTotal())); } @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.getNetTotal(), Matchers.comparesEqualTo(invoice.getGrossTotal())); } @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.getNetTotal())); } @Test @@ -59,7 +59,7 @@ public void testInvoiceHasProperTaxValueForManyProduct() { 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())); + Assert.assertThat(new BigDecimal("10.30"), Matchers.comparesEqualTo(invoice.getTaxTotal())); } @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.getGrossTotal())); } @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.getNetTotal())); } @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.getGrossTotal())); } @Test(expected = IllegalArgumentException.class) From 4ce54e5170ca9a0b71f5cc7ba12c7f70b1c5e975 Mon Sep 17 00:00:00 2001 From: gmagdalena2012 <60036067+gmagdalena2012@users.noreply.github.com> Date: Fri, 1 May 2020 15:14:43 +0200 Subject: [PATCH 04/10] CI --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..403f3fbbf --- /dev/null +++ b/.travis.yml @@ -0,0 +1,2 @@ +language: java +script: mvn test From d532d9b2aef4ff707075b6c240ad4ef4df82e00b Mon Sep 17 00:00:00 2001 From: gmagdalena2012 <60036067+gmagdalena2012@users.noreply.github.com> Date: Fri, 22 May 2020 17:27:06 +0200 Subject: [PATCH 05/10] checkstyle and plugins added --- checkstyle.xml | 304 +++++++++++++++++++++++++++++++++++++++++++++++++ pom.xml | 54 +++++++++ 2 files changed, 358 insertions(+) create mode 100644 checkstyle.xml diff --git a/checkstyle.xml b/checkstyle.xml new file mode 100644 index 000000000..faa5606da --- /dev/null +++ b/checkstyle.xml @@ -0,0 +1,304 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2614c6984..b50807cae 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,8 @@ UTF-8 + 3.2.3 + checkstyle.xml @@ -25,8 +27,60 @@ 1.8 + + org.apache.maven.plugins + maven-shade-plugin + ${maven.shade.plugin.version} + + + package + + shade + + + + + + + org.apache.maven.plugins + maven-site-plugin + 3.7.1 + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 3.0.0 + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + 3 + true + @{argLine} -Xmx1024m -XX:MaxPermSize=256m + + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.1.1 + + + + checkstyle + + + + + + + From a9bcd4ca45c02392d78133455c12254abf1b39db Mon Sep 17 00:00:00 2001 From: gmagdalena2012 <60036067+gmagdalena2012@users.noreply.github.com> Date: Fri, 22 May 2020 17:30:35 +0200 Subject: [PATCH 06/10] surefire correction at pom --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index b50807cae..f30a328f3 100644 --- a/pom.xml +++ b/pom.xml @@ -58,7 +58,7 @@ 3 true - @{argLine} -Xmx1024m -XX:MaxPermSize=256m + -Xmx1024m -XX:MaxPermSize=256m From 518517da8ed31e4104b1c51d4df85af84b683b4e Mon Sep 17 00:00:00 2001 From: gmagdalena2012 <60036067+gmagdalena2012@users.noreply.github.com> Date: Fri, 22 May 2020 17:48:37 +0200 Subject: [PATCH 07/10] product corrected --- src/main/java/pl/edu/agh/mwo/invoice/product/Product.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 cd0f86a48..1e30d05cb 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,7 +10,8 @@ public abstract class Product { private final BigDecimal taxPercent; protected Product(String name, BigDecimal price, BigDecimal tax) { - if (name == null || name.equals("") || price == null || tax == null || tax.compareTo(new BigDecimal(0)) < 0 + if (name == null || name.equals("") || price == null || tax == null + || tax.compareTo(new BigDecimal(0)) < 0 || price.compareTo(new BigDecimal(0)) < 0) { throw new IllegalArgumentException(); } From 8e3ab9912fa389e41361bbb550c3a9f607ed8351 Mon Sep 17 00:00:00 2001 From: gmagdalena2012 <60036067+gmagdalena2012@users.noreply.github.com> Date: Fri, 22 May 2020 17:52:04 +0200 Subject: [PATCH 08/10] Travis yml updated checkstyle --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 403f3fbbf..929874f3f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,2 +1,4 @@ language: java -script: mvn test +script: + - mvn checkstyle:check + - mvn test From e085480f46893043358d6e1f479e160decb26be7 Mon Sep 17 00:00:00 2001 From: gmagdalena2012 <60036067+gmagdalena2012@users.noreply.github.com> Date: Fri, 22 May 2020 19:27:59 +0200 Subject: [PATCH 09/10] Print invoice number --- .../java/pl/edu/agh/mwo/invoice/Invoice.java | 10 ++++++-- .../pl/edu/agh/mwo/invoice/InvoiceTest.java | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 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 72f4048d5..73a220e73 100644 --- a/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java +++ b/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java @@ -7,8 +7,10 @@ import pl.edu.agh.mwo.invoice.product.Product; public class Invoice { - private Map products = new HashMap(); - + private Map products = new HashMap<>(); + private static int nextNumber = 0; + private final int number = ++nextNumber; + public void addProduct(Product product) { addProduct(product, 1); } @@ -41,4 +43,8 @@ public BigDecimal getGrossTotal() { } return totalGross; } + + public int getNumber() { + return number; + } } 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 920b9a99c..dd3878812 100644 --- a/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java +++ b/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java @@ -104,4 +104,29 @@ public void testInvoiceWithZeroQuantity() { public void testInvoiceWithNegativeQuantity() { invoice.addProduct(new DairyProduct("Zsiadle mleko", new BigDecimal("5.55")), -1); } + + @Test + public void testInvoiceHasNumberGreaterThan0() { + int number = invoice.getNumber(); + Assert.assertThat(number, Matchers.greaterThan(0)); + } + + @Test + public void testTwoInvoicesHaveDifferentNumbers() { + int number1 = new Invoice().getNumber(); + int number2 = new Invoice().getNumber(); + Assert.assertNotEquals(number1, number2); + } + + @Test + public void testInvoiceDoesNotChangeItsNumber() { + Assert.assertEquals(invoice.getNumber(), invoice.getNumber()); + } + + @Test + public void testTheFirstInvoiceNumberIsLowerThanTheSecond() { + int number1 = new Invoice().getNumber(); + int number2 = new Invoice().getNumber(); + Assert.assertThat(number1, Matchers.lessThan(number2)); + } } From bcaa0add7532191e58f405bbfeb039f77e348e48 Mon Sep 17 00:00:00 2001 From: gmagdalena2012 <60036067+gmagdalena2012@users.noreply.github.com> Date: Fri, 22 May 2020 20:34:36 +0200 Subject: [PATCH 10/10] Zad 1 - Printing invoice with products --- src/main/java/pl/edu/agh/mwo/invoice/Invoice.java | 12 ++++++++++++ .../java/pl/edu/agh/mwo/invoice/InvoiceTest.java | 8 ++++++++ 2 files changed, 20 insertions(+) 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 73a220e73..749d7c89b 100644 --- a/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java +++ b/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java @@ -47,4 +47,16 @@ public BigDecimal getGrossTotal() { public int getNumber() { return number; } + + public String print() { + String invoiceHeader = "Numer faktury: " + getNumber() + "\n"; + + for (Product product : products.keySet()) { + invoiceHeader = invoiceHeader + product.getName() + ", szt: " + products.get(product) + + ", cena/szt: " + product.getPrice() + " PLN\n"; + } + + invoiceHeader += "Liczba pozycji: " + this.products.size(); + return invoiceHeader; + } } 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 dd3878812..98a52066e 100644 --- a/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java +++ b/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java @@ -129,4 +129,12 @@ public void testTheFirstInvoiceNumberIsLowerThanTheSecond() { int number2 = new Invoice().getNumber(); Assert.assertThat(number1, Matchers.lessThan(number2)); } + @Test + public void testPrintInvoice(){ + Invoice printedInvoice = new Invoice(); + String expectedResults = "Numer faktury: " + printedInvoice.getNumber() + "\n" + "Kefir, szt: 10, cena/szt: 3 PLN" + "\n" +"Kubek, szt: 4, cena/szt: 15 PLN" + "\n" + "Liczba pozycji: 2"; + printedInvoice.addProduct(new TaxFreeProduct("Kubek", new BigDecimal("15")), 4); + printedInvoice.addProduct(new DairyProduct("Kefir", new BigDecimal("3")), 10); + Assert.assertEquals(expectedResults, printedInvoice.print()); + } }