diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 000000000..929874f3f
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,4 @@
+language: java
+script:
+ - mvn checkstyle:check
+ - mvn test
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..f30a328f3 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
+ -Xmx1024m -XX:MaxPermSize=256m
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-checkstyle-plugin
+ 3.1.1
+
+
+
+ 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 56fe02359..749d7c89b 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,62 @@
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 products;
-
+ private Map products = new HashMap<>();
+ private static int nextNumber = 0;
+ private final int number = ++nextNumber;
+
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;
+ }
+
+ 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/main/java/pl/edu/agh/mwo/invoice/product/Product.java b/src/main/java/pl/edu/agh/mwo/invoice/product/Product.java
index 318de9ac9..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,24 +10,29 @@ 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);
}
}
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..98a52066e
--- /dev/null
+++ b/src/test/java/pl/edu/agh/mwo/invoice/InvoiceTest.java
@@ -0,0 +1,140 @@
+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.getNetTotal()));
+ }
+
+ @Test
+ public void testEmptyInvoiceHasEmptyTaxAmount() {
+ Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTaxTotal()));
+ }
+
+ @Test
+ public void testEmptyInvoiceHasEmptyTotal() {
+ 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.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.getNetTotal()));
+ }
+
+ @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.getTaxTotal()));
+ }
+
+ @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.getGrossTotal()));
+ }
+
+ @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.getNetTotal()));
+ }
+
+ @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.getGrossTotal()));
+ }
+
+ @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);
+ }
+
+ @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));
+ }
+ @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());
+ }
+}
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"));
+ }
+}