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
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..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,44 +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 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";
+ }
+ productList += "Liczba pozycji: " + products.size();
+
+ return productList;
+ }
+
+ 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 totalGross;
+ return value;
}
}
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..32ae3140e 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
@@ -4,6 +4,7 @@
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 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 50513171c..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)
@@ -125,4 +125,30 @@ public void testInvoiceWithNegativeQuantity() {
public void testAddingNullProduct() {
invoice.addProduct(null);
}
+
+ @Test
+ public void testProductListContainsInvoiceNumber() {
+ invoice.addProduct(new TaxFreeProduct("Chleb", new BigDecimal("5")), 2);
+
+ String productList = invoice.getProductList();
+
+ Assert.assertTrue(productList.contains("Numer faktury: " + invoice.getInvoiceNumber()));
+ }
+
+ @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"));
+ }
}