Skip to content

Ready #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions src/main/java/pl/edu/agh/mwo/invoice/Invoice.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
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 Collection<Product> products;
private Map<Product, Integer> products = new HashMap<Product, Integer>();

public void addProduct(Product product) {
// TODO: implement
addProduct(product, 1);
}

public void addProduct(Product product, Integer quantity) {
// TODO: implement
if (product == null || quantity <= 0) {
throw new IllegalArgumentException();
}
products.put(product, quantity);
}

public BigDecimal getSubtotal() {
return null;
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() {
return null;
public BigDecimal getTaxTotal() {
return getGrossTotal().subtract(getNetTotal());
}

public BigDecimal getTotal() {
return null;
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 totalGross;
}
}
12 changes: 8 additions & 4 deletions src/main/java/pl/edu/agh/mwo/invoice/product/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@ 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.multiply(taxPercent).add(price);
}
}
18 changes: 9 additions & 9 deletions src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@ 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
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down