Skip to content
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
60 changes: 53 additions & 7 deletions src/main/java/pl/edu/agh/mwo/invoice/Invoice.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,76 @@
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<Product> 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
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);
}

public BigDecimal getSubtotal() {
return null;

private Map<Product,Integer> 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;

}


}

Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down
24 changes: 20 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 @@ -9,25 +9,41 @@ public abstract class Product {

private final BigDecimal taxPercent;


protected Product(String name, BigDecimal price, BigDecimal tax) {

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");
}
this.price = price;

if(tax == null|| price.compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException("Tax percent cannot be null");
}
this.taxPercent = tax;
}


public String getName() {
return null;
return this.name;
}

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);

}
}
26 changes: 13 additions & 13 deletions src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,9 +20,10 @@ public void createEmptyInvoiceForTheTest() {
invoice = new Invoice();
}


@Test
public void testEmptyInvoiceHasEmptySubtotal() {
Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getSubtotal()));
Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getNetValue()));
}

@Test
Expand All @@ -33,38 +33,38 @@ 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() {
Product onions = new TaxFreeProduct("Warzywa", new BigDecimal("10"));
public void testInvoiceNetValueWithTwoDifferentProducts() {
Product onions = new TaxFreeProduct("WarzywaWazrywa", 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
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
Expand All @@ -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.getGrossValue()));
}

@Test
Expand All @@ -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.getNetValue()));
}

@Test
Expand All @@ -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.getGrossValue()));
}

@Test(expected = IllegalArgumentException.class)
Expand Down