From 75d31f4604034642d599cb823a34fef0b4cf7475 Mon Sep 17 00:00:00 2001 From: maywald Date: Sun, 11 Jan 2026 13:24:14 +0100 Subject: [PATCH 1/7] First priductTest working --- src/main/java/pl/edu/agh/mwo/invoice/product/OtherProduct.java | 1 + src/main/java/pl/edu/agh/mwo/invoice/product/Product.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/pl/edu/agh/mwo/invoice/product/OtherProduct.java b/src/main/java/pl/edu/agh/mwo/invoice/product/OtherProduct.java index e324fe5a4..be62a994b 100644 --- a/src/main/java/pl/edu/agh/mwo/invoice/product/OtherProduct.java +++ b/src/main/java/pl/edu/agh/mwo/invoice/product/OtherProduct.java @@ -3,6 +3,7 @@ import java.math.BigDecimal; public class OtherProduct extends Product { + public OtherProduct(String name, BigDecimal price) { super(name, price, new BigDecimal("0.23")); } 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..d2edc9974 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,7 +16,7 @@ protected Product(String name, BigDecimal price, BigDecimal tax) { } public String getName() { - return null; + return this.name; } public BigDecimal getPrice() { From b87e1e174b3bf407b91cc747385d3e655fc3d5c8 Mon Sep 17 00:00:00 2001 From: maywald Date: Sun, 11 Jan 2026 13:50:56 +0100 Subject: [PATCH 2/7] =?UTF-8?q?Kolejen=20testy=20przesz=C5=82y!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/pl/edu/agh/mwo/invoice/product/Product.java | 8 +++++--- 1 file changed, 5 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 d2edc9974..62f8d8f07 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 @@ -20,14 +20,16 @@ public String getName() { } public BigDecimal getPrice() { - return null; + return this.price; } public BigDecimal getTaxPercent() { - return null; + return this.taxPercent; } public BigDecimal getPriceWithTax() { - return null; + BigDecimal result = this.price.multiply(this.taxPercent); + return (BigDecimal)price.add(result); + } } From 573b94a1202f7e7ca0c014fc47f8e802b5ade908 Mon Sep 17 00:00:00 2001 From: MajowyLas Date: Sun, 11 Jan 2026 14:02:08 +0100 Subject: [PATCH 3/7] IllegalArgumentException - handling --- .../pl/edu/agh/mwo/invoice/product/Product.java | 14 ++++++++++++++ 1 file changed, 14 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 62f8d8f07..d0c2f5850 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 @@ -9,12 +9,26 @@ public abstract class Product { private final BigDecimal taxPercent; + protected Product(String name, BigDecimal price, BigDecimal tax) { + + if(name == null) { + throw new IllegalArgumentException("name cannot be null"); + } this.name = name; + + if(price == null) { + throw new IllegalArgumentException("price cannot be null"); + } this.price = price; + + if(tax == null) { + throw new IllegalArgumentException("tax cannot be null"); + } this.taxPercent = tax; } + public String getName() { return this.name; } From 1fa3a1e3de92600c1d686ac3d4c7a4e6b9eb2706 Mon Sep 17 00:00:00 2001 From: MajowyLas Date: Sun, 11 Jan 2026 14:19:19 +0100 Subject: [PATCH 4/7] Rest of the tests --- .../java/pl/edu/agh/mwo/invoice/product/Product.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 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 d0c2f5850..dc09e2fb7 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 @@ -12,18 +12,18 @@ public abstract class Product { protected Product(String name, BigDecimal price, BigDecimal tax) { - if(name == null) { - throw new IllegalArgumentException("name cannot be null"); + if(name == null || name.isBlank()) { + throw new IllegalArgumentException("Product name cannot be null"); } this.name = name; if(price == null) { - throw new IllegalArgumentException("price cannot be null"); + throw new IllegalArgumentException("Price cannot be null"); } this.price = price; - if(tax == null) { - throw new IllegalArgumentException("tax cannot be null"); + if(tax == null|| price.compareTo(BigDecimal.ZERO) < 0) { + throw new IllegalArgumentException("Tax percent cannot be null"); } this.taxPercent = tax; } From 186ccc2f94a60cfe1f680061f51c43f804b494c5 Mon Sep 17 00:00:00 2001 From: Anna Maywald Date: Tue, 13 Jan 2026 14:47:08 +0100 Subject: [PATCH 5/7] No comiit made it through in class... Need to upload all changes made in class in one commit now. --- .../java/pl/edu/agh/mwo/invoice/Invoice.java | 47 ++++++++++++++++--- .../pl/edu/agh/mwo/invoice/InvoiceTest.java | 23 +++++---- 2 files changed, 51 insertions(+), 19 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..91cecf9fb 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,63 @@ package pl.edu.agh.mwo.invoice; import java.math.BigDecimal; +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 Collection products; + public Invoice() { + this.products = new ArrayList<>(); + } + + public void addProduct(Product product) { - // TODO: implement + this.addProduct(product,1); } + public void addProduct(Product product, Integer quantity) { - // TODO: implement + this.productsMap.put(product,quantity); } - public BigDecimal getSubtotal() { - return null; + + private Map productsMap = new HashMap<>(); + + public BigDecimal getNetValue() { + BigDecimal netValue = BigDecimal.ZERO; + + for (Product product : this.productsMap.keySet()) { + Integer quantity = this.productsMap.get(product); + BigDecimal price = product.getPrice(); + price = price.multiply(BigDecimal.valueOf(quantity)); + netValue = netValue.add(price); + + } + return netValue; } + public BigDecimal getTax() { - return null; + return getGrossValue().subtract(getNetValue()); } - public BigDecimal getTotal() { - return null; + + public BigDecimal getGrossValue() { + BigDecimal netValue = BigDecimal.ZERO; + + for (Product product : this.productsMap.keySet()) { + Integer quantity = this.productsMap.get(product); + BigDecimal price = product.getPriceWithTax(); + price = price.multiply(BigDecimal.valueOf(quantity)); + netValue = netValue.add(price); + + } + return netValue; } } + 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..140dc461b 100644 --- a/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java +++ b/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java @@ -7,7 +7,6 @@ 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; @@ -23,7 +22,7 @@ public void createEmptyInvoiceForTheTest() { @Test public void testEmptyInvoiceHasEmptySubtotal() { - Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getSubtotal())); + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getNetValue())); } @Test @@ -33,30 +32,30 @@ public void testEmptyInvoiceHasEmptyTaxAmount() { @Test public void testEmptyInvoiceHasEmptyTotal() { - Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTotal())); + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getGrossValue())); } @Test - public void testInvoiceSubtotalWithTwoDifferentProducts() { + public void testInvoiceNetValueWithTwoDifferentProducts() { Product onions = new TaxFreeProduct("Warzywa", new BigDecimal("10")); 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.getNetValue())); } @Test - public void testInvoiceSubtotalWithManySameProducts() { + public void testInvoiceSNetValueWithManySameProducts() { 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.getNetValue())); } @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.getGrossValue(), Matchers.comparesEqualTo(invoice.getNetValue())); } @Test @@ -64,7 +63,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.getNetValue())); } @Test @@ -86,7 +85,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.getGrossValue())); } @Test @@ -97,7 +96,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.getNetValue())); } @Test @@ -108,7 +107,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.getGrossValue())); } @Test(expected = IllegalArgumentException.class) From 558297440445f1ab99e2e3ddaa8d9b86afd59c59 Mon Sep 17 00:00:00 2001 From: Anna Maywald Date: Tue, 13 Jan 2026 14:50:11 +0100 Subject: [PATCH 6/7] No comiit made it through in class... Need to upload all changes made in class in one commit now. --- src/main/java/pl/edu/agh/mwo/invoice/Invoice.java | 1 + src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) 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 91cecf9fb..ed7a05cca 100644 --- a/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java +++ b/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java @@ -58,6 +58,7 @@ public BigDecimal getGrossValue() { } return netValue; + } } 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 140dc461b..44696ebc5 100644 --- a/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java +++ b/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java @@ -20,6 +20,7 @@ public void createEmptyInvoiceForTheTest() { invoice = new Invoice(); } + @Test public void testEmptyInvoiceHasEmptySubtotal() { Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getNetValue())); @@ -37,7 +38,7 @@ public void testEmptyInvoiceHasEmptyTotal() { @Test public void testInvoiceNetValueWithTwoDifferentProducts() { - Product onions = new TaxFreeProduct("Warzywa", new BigDecimal("10")); + Product onions = new TaxFreeProduct("WarzywaWazrywa", new BigDecimal("10")); Product apples = new TaxFreeProduct("Owoce", new BigDecimal("10")); invoice.addProduct(onions); invoice.addProduct(apples); From 30c264d7750626b16c5d8fb9a33f1fc624b0c852 Mon Sep 17 00:00:00 2001 From: Anna Maywald Date: Fri, 16 Jan 2026 17:51:33 +0100 Subject: [PATCH 7/7] testInvoiceWithZeroQuantity testInvoiceWithNegativeQuantity testAddingNullProduct --- .../java/pl/edu/agh/mwo/invoice/Invoice.java | 16 ++++++++++++++-- 1 file changed, 14 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 ed7a05cca..6948c890b 100644 --- a/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java +++ b/src/main/java/pl/edu/agh/mwo/invoice/Invoice.java @@ -22,6 +22,13 @@ public void addProduct(Product product) { public void addProduct(Product product, Integer quantity) { + if(product == null) { + throw new IllegalArgumentException("Product cannot be null"); + + } if (quantity <= 0) { + throw new IllegalArgumentException("Quantity must be greater than 0"); + } + this.productsMap.put(product,quantity); } @@ -29,6 +36,8 @@ public void addProduct(Product product, Integer quantity) { private Map productsMap = new HashMap<>(); public BigDecimal getNetValue() { + + BigDecimal netValue = BigDecimal.ZERO; for (Product product : this.productsMap.keySet()) { @@ -37,8 +46,9 @@ public BigDecimal getNetValue() { price = price.multiply(BigDecimal.valueOf(quantity)); netValue = netValue.add(price); - } - return netValue; + }return netValue; + + } @@ -60,5 +70,7 @@ public BigDecimal getGrossValue() { return netValue; } + + }