From b9ffe96f553ee29f48d2cbb9f517d5685fa39b1e 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 1/5] 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 fbb07dceb4407153fa266d69c832f48798da7a49 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 2/5] 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 | 22 +++++----- 3 files changed, 30 insertions(+), 41 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 7f4b6f795..50513171c 100644 --- a/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java +++ b/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java @@ -23,17 +23,17 @@ 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 @@ -42,21 +42,21 @@ public void testInvoiceSubtotalWithTwoDifferentProducts() { Product apples = new TaxFreeProduct("Owoce", new BigDecimal("10")); invoice.addProduct(onions); invoice.addProduct(apples); - Assert.assertThat(new BigDecimal("20"), Matchers.comparesEqualTo(invoice.getSubtotal())); + Assert.assertThat(new BigDecimal("20"), Matchers.comparesEqualTo(invoice.getNetTotal())); } @Test public void testInvoiceSubtotalWithManySameProducts() { Product onions = new TaxFreeProduct("Warzywa", BigDecimal.valueOf(10)); invoice.addProduct(onions, 100); - Assert.assertThat(new BigDecimal("1000"), Matchers.comparesEqualTo(invoice.getSubtotal())); + Assert.assertThat(new BigDecimal("1000"), Matchers.comparesEqualTo(invoice.getNetTotal())); } @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 @@ -64,7 +64,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 @@ -75,7 +75,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 @@ -86,7 +86,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 @@ -97,7 +97,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 @@ -108,7 +108,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 2f9283890a99393e48275f936d461505c8a4107e Mon Sep 17 00:00:00 2001 From: Tomasz Wincencik Date: Sat, 11 Apr 2026 11:43:21 +0200 Subject: [PATCH 3/5] Adding the CI file: .github/workflows/github-actions.yml --- .github/workflows/github-actions.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 .github/workflows/github-actions.yml diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml new file mode 100644 index 000000000..8fb587348 --- /dev/null +++ b/.github/workflows/github-actions.yml @@ -0,0 +1,15 @@ +name: CI for Java Invoice +on: [push] +jobs: + test: + name: Unit tests + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up JDK + uses: actions/setup-java@v2 + with: + java-version: '17' + distribution: 'adopt' + - name: Test + run: mvn test \ No newline at end of file From fa5dca66e02649c180f26e055b677c5361599e2d Mon Sep 17 00:00:00 2001 From: Tomasz Wincencik Date: Sat, 11 Apr 2026 13:24:02 +0200 Subject: [PATCH 4/5] Adding the checkstyle and Task0 --- checkstyle.xml | 298 ++++++++++++++++++ pom.xml | 18 ++ .../java/pl/edu/agh/mwo/invoice/Invoice.java | 13 + .../pl/edu/agh/mwo/invoice/InvoiceTest.java | 18 ++ 4 files changed, 347 insertions(+) create mode 100644 checkstyle.xml diff --git a/checkstyle.xml b/checkstyle.xml new file mode 100644 index 000000000..fbcd05132 --- /dev/null +++ b/checkstyle.xml @@ -0,0 +1,298 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2d40c3e8a..ed8c7a138 100644 --- a/pom.xml +++ b/pom.xml @@ -41,4 +41,22 @@ 1.3 + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.6.0 + + + + checkstyle + + + + + + + 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..60e09a4b4 100644 --- a/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java +++ b/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java @@ -3,6 +3,7 @@ import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; +import java.util.Random; import pl.edu.agh.mwo.invoice.product.Product; @@ -41,4 +42,16 @@ public BigDecimal getGrossTotal() { } return totalGross; } + + static int NUMBER = 0; + private int number; + + public Invoice() { + this.number = NUMBER++; + } + + 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 50513171c..8eab23659 100644 --- a/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java +++ b/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java @@ -125,4 +125,22 @@ public void testInvoiceWithNegativeQuantity() { public void testAddingNullProduct() { invoice.addProduct(null); } + + @Test + public void testInvoiceHasNumberGreaterThamZero() { + int number = invoice.getNumber(); + Assert.assertThat(number, Matchers.greaterThanOrEqualTo(0));} + + public void testTwoInvoicesHaveDifferentNumbers() { + Invoice invoice1 = new Invoice(); + Invoice invoice2 = new Invoice(); + Assert.assertNotEquals(invoice1.getNumber(), invoice2.getNumber()); + } + + public void testInvoiceHasConsequentNumbers() { + Invoice invoice1 = new Invoice(); + Invoice invoice2 = new Invoice(); + Assert.assertEquals(invoice1.getNumber(), invoice2.getNumber()-1); + } + } From e3450bdc5fe7289c9ee2584186f199550c79e64b Mon Sep 17 00:00:00 2001 From: Tomasz Wincencik Date: Tue, 16 Jun 2026 17:59:20 +0200 Subject: [PATCH 5/5] Final implementation Task 1. Printing list of products, invice number and number of products --- .../java/pl/edu/agh/mwo/invoice/Invoice.java | 86 ++++++++++++------- .../edu/agh/mwo/invoice/product/Product.java | 25 ++++-- .../pl/edu/agh/mwo/invoice/InvoiceTest.java | 52 ++++++----- 3 files changed, 104 insertions(+), 59 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 60e09a4b4..368d03f67 100644 --- a/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java +++ b/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java @@ -1,57 +1,85 @@ package pl.edu.agh.mwo.invoice; import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.Random; import pl.edu.agh.mwo.invoice.product.Product; public class Invoice { - private Map products = new HashMap(); + private Map products = + new HashMap<>(); - public void addProduct(Product product) { - addProduct(product, 1); + private static int invoiceNumberNext = 1; + private int invoiceNumber; + + public Invoice() { + this.invoiceNumber = invoiceNumberNext++; } + public int getInvoiceNumber() { + return invoiceNumber; + } + + public void addProduct(Product product) { + + this.addProduct(product, 1); + } public void addProduct(Product product, Integer quantity) { - if (product == null || quantity <= 0) { - throw new IllegalArgumentException(); + if (quantity<=0 ) { + throw new IllegalArgumentException("Quantity of products cannot be less or equal 0"); + } + else if (product == null) { + throw new IllegalArgumentException("Product cannot be null"); } - products.put(product, quantity); + + this.products.put(product, quantity); } - 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)); + + public BigDecimal getSubtotal() { + BigDecimal value = BigDecimal.ZERO; + for (Product product : this.products.keySet()) { + Integer quantity = this.products.get(product); + BigDecimal price = product.getPrice(); + price = price.multiply(BigDecimal.valueOf(quantity)); + value = value.add(price); } - return totalNet; + return value; + } - public BigDecimal getTaxTotal() { - return getGrossTotal().subtract(getNetTotal()); + public BigDecimal getTax() { + + return getTotal().subtract(getSubtotal()); + } - public BigDecimal getGrossTotal() { - BigDecimal totalGross = BigDecimal.ZERO; + public String getProductList() { + String productList = "Numer faktury: " + invoiceNumber + "\n"; + for (Product product : products.keySet()) { - BigDecimal quantity = new BigDecimal(products.get(product)); - totalGross = totalGross.add(product.getPriceWithTax().multiply(quantity)); + productList += product.getName() + "," + products.get(product) + ", " + product.getPrice() + "\n"; } - return totalGross; - } - - static int NUMBER = 0; - private int number; + productList += "Liczba pozycji: " + products.size(); - public Invoice() { - this.number = NUMBER++; + return productList; } - public int getNumber() { - - return number; + public BigDecimal getTotal() { + BigDecimal value = BigDecimal.ZERO; + for (Product product : this.products.keySet()) { + Integer quantity = this.products.get(product); + BigDecimal price = product.getPriceWithTax(); //.subtract(product.getTaxPercent()/100.0); //.multiply(new BigDecimal("0.01"))); + price = price.multiply(BigDecimal.valueOf(quantity)); //.setScale(2, RoundingMode.HALF_UP); + System.out.println("price aft round"); + System.out.println(price); + System.out.println(product); + value = value.add(price); + } + return value; } } 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..3cb566d3e 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 @@ -2,6 +2,8 @@ import java.math.BigDecimal; +import static java.math.BigDecimal.ZERO; + public abstract class Product { private final String name; @@ -9,29 +11,36 @@ 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(); + if (name==null || name.isEmpty()) { + throw new IllegalArgumentException("Name of the product cannot be null or empty"); + } + else if ((price == null) || price.compareTo(BigDecimal.ZERO) < 0) { + throw new IllegalArgumentException("Price cannot be null or empty"); } + //else if (tax.) { + // throw new IllegalArgumentException("Price cannot be null or empty"); + //} + this.name = name; this.price = price; this.taxPercent = tax; + } public String getName() { - return name; + return this.name; } public BigDecimal getPrice() { - return price; + return this.price; } public BigDecimal getTaxPercent() { - return taxPercent; + return this.taxPercent; } - public BigDecimal getPriceWithTax() { - return price.multiply(taxPercent).add(price); + public BigDecimal getPriceWithTax() { return this.price.multiply(this.taxPercent).add(this.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 8eab23659..d20a48e71 100644 --- a/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java +++ b/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java @@ -23,17 +23,17 @@ public void createEmptyInvoiceForTheTest() { @Test public void testEmptyInvoiceHasEmptySubtotal() { - Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getNetTotal())); + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getSubtotal())); } @Test public void testEmptyInvoiceHasEmptyTaxAmount() { - Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTaxTotal())); + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTax())); } @Test public void testEmptyInvoiceHasEmptyTotal() { - Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getGrossTotal())); + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTotal())); } @Test @@ -42,21 +42,21 @@ public void testInvoiceSubtotalWithTwoDifferentProducts() { Product apples = new TaxFreeProduct("Owoce", new BigDecimal("10")); invoice.addProduct(onions); invoice.addProduct(apples); - Assert.assertThat(new BigDecimal("20"), Matchers.comparesEqualTo(invoice.getNetTotal())); + Assert.assertThat(new BigDecimal("20"), Matchers.comparesEqualTo(invoice.getSubtotal())); } @Test public void testInvoiceSubtotalWithManySameProducts() { Product onions = new TaxFreeProduct("Warzywa", BigDecimal.valueOf(10)); invoice.addProduct(onions, 100); - Assert.assertThat(new BigDecimal("1000"), Matchers.comparesEqualTo(invoice.getNetTotal())); + Assert.assertThat(new BigDecimal("1000"), Matchers.comparesEqualTo(invoice.getSubtotal())); } @Test public void testInvoiceHasTheSameSubtotalAndTotalIfTaxIsZero() { Product taxFreeProduct = new TaxFreeProduct("Warzywa", new BigDecimal("199.99")); invoice.addProduct(taxFreeProduct); - Assert.assertThat(invoice.getNetTotal(), Matchers.comparesEqualTo(invoice.getGrossTotal())); + Assert.assertThat(invoice.getTotal(), Matchers.comparesEqualTo(invoice.getSubtotal())); } @Test @@ -64,7 +64,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.getNetTotal())); + Assert.assertThat(new BigDecimal("310"), Matchers.comparesEqualTo(invoice.getSubtotal())); } @Test @@ -75,7 +75,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.getTaxTotal())); + Assert.assertThat(new BigDecimal("10.30"), Matchers.comparesEqualTo(invoice.getTax())); } @Test @@ -86,7 +86,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.getGrossTotal())); + Assert.assertThat(new BigDecimal("320.30"), Matchers.comparesEqualTo(invoice.getTotal())); } @Test @@ -97,7 +97,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.getNetTotal())); + Assert.assertThat(new BigDecimal("50"), Matchers.comparesEqualTo(invoice.getSubtotal())); } @Test @@ -108,7 +108,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.getGrossTotal())); + Assert.assertThat(new BigDecimal("54.70"), Matchers.comparesEqualTo(invoice.getTotal())); } @Test(expected = IllegalArgumentException.class) @@ -127,20 +127,28 @@ public void testAddingNullProduct() { } @Test - public void testInvoiceHasNumberGreaterThamZero() { - int number = invoice.getNumber(); - Assert.assertThat(number, Matchers.greaterThanOrEqualTo(0));} + public void testProductListContainsInvoiceNumber() { + invoice.addProduct(new TaxFreeProduct("Chleb", new BigDecimal("5")), 2); + + String productList = invoice.getProductList(); - public void testTwoInvoicesHaveDifferentNumbers() { - Invoice invoice1 = new Invoice(); - Invoice invoice2 = new Invoice(); - Assert.assertNotEquals(invoice1.getNumber(), invoice2.getNumber()); + Assert.assertTrue(productList.contains("Numer faktury: " + invoice.getInvoiceNumber())); } - public void testInvoiceHasConsequentNumbers() { - Invoice invoice1 = new Invoice(); - Invoice invoice2 = new Invoice(); - Assert.assertEquals(invoice1.getNumber(), invoice2.getNumber()-1); + @Test + public void testProductListContainsProductNameQuantityAndPrice() { + invoice.addProduct(new TaxFreeProduct("Chleb", new BigDecimal("5")), 2); + String productList = invoice.getProductList(); + Assert.assertTrue(productList.contains("Chleb")); + Assert.assertTrue(productList.contains("2")); + Assert.assertTrue(productList.contains("5")); } + @Test + public void testProductListContainsNumberOfPositions() { + invoice.addProduct(new TaxFreeProduct("Chleb", new BigDecimal("5")), 2); + invoice.addProduct(new DairyProduct("Pinezka", new BigDecimal("10")), 3); + String productList = invoice.getProductList(); + Assert.assertTrue(productList.contains("Liczba pozycji: 2")); + } }