diff --git a/pom.xml b/pom.xml
index 3994d594..8c9b25ca 100644
--- a/pom.xml
+++ b/pom.xml
@@ -17,8 +17,20 @@
junit
junit
- 4.11
+ 4.13.1
test
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+ 8
+ 8
+
+
+
+
diff --git a/src/main/java/com/abc/Account.java b/src/main/java/com/abc/Account.java
index 099691e0..a6294013 100644
--- a/src/main/java/com/abc/Account.java
+++ b/src/main/java/com/abc/Account.java
@@ -1,73 +1,96 @@
package com.abc;
+import com.util.BigDecimalProvider;
+import com.util.CurrencyStringFormatter;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
-public class Account {
- public static final int CHECKING = 0;
- public static final int SAVINGS = 1;
- public static final int MAXI_SAVINGS = 2;
+public abstract class Account {
+ protected List transactions = new ArrayList<>();
+
+ abstract protected String getAccountTypeLabel();
+
+ abstract public BigDecimal getInterestEarned();
- private final int accountType;
- public List transactions;
- public Account(int accountType) {
- this.accountType = accountType;
- this.transactions = new ArrayList();
+ protected Transaction deposit(BigDecimal amount) {
+ Account.validateAmount(amount);
+ return commitTransaction(BigDecimalProvider.format(amount));
+ }
+
+ protected Transaction withdraw(BigDecimal amount) {
+ Account.validateAmount(amount);
+ return commitTransaction(BigDecimalProvider.format(amount).negate());
}
- public void deposit(double amount) {
- if (amount <= 0) {
- throw new IllegalArgumentException("amount must be greater than zero");
- } else {
- transactions.add(new Transaction(amount));
+ protected static void validateAmount(BigDecimal amount) {
+ if (amount.compareTo(java.math.BigDecimal.ZERO) <= 0) {
+ throw new IllegalArgumentException("Provided amount must be greater than zero");
}
}
-public void withdraw(double amount) {
- if (amount <= 0) {
- throw new IllegalArgumentException("amount must be greater than zero");
- } else {
- transactions.add(new Transaction(-amount));
+ protected Transaction commitTransaction(BigDecimal amount) {
+ Transaction transaction = new Transaction(amount);
+ transactions.add(transaction);
+ return transaction;
+ }
+
+ BigDecimal calculateInterest(BigDecimal amount, BigDecimal rate) {
+ return amount.multiply(rate).setScale(2, RoundingMode.HALF_EVEN);
}
-}
- public double interestEarned() {
- double amount = sumTransactions();
- switch(accountType){
- case SAVINGS:
- if (amount <= 1000)
- return amount * 0.001;
- else
- return 1 + (amount-1000) * 0.002;
-// case SUPER_SAVINGS:
-// if (amount <= 4000)
-// return 20;
- case MAXI_SAVINGS:
- if (amount <= 1000)
- return amount * 0.02;
- if (amount <= 2000)
- return 20 + (amount-1000) * 0.05;
- return 70 + (amount-2000) * 0.1;
- default:
- return amount * 0.001;
+ public BigDecimal sumTransactions() {
+ BigDecimal amount = BigDecimalProvider.getZero();
+
+ if (transactions.isEmpty()) {
+ return amount;
}
+
+ for (Transaction transaction : transactions)
+ amount = amount.add(transaction.getAmount());
+ return amount;
}
- public double sumTransactions() {
- return checkIfTransactionsExist(true);
+ public List getTransactions() {
+ return new ArrayList<>(transactions);
}
- private double checkIfTransactionsExist(boolean checkAll) {
- double amount = 0.0;
- for (Transaction t: transactions)
- amount += t.amount;
- return amount;
+ public String getStatement() {
+ StringBuilder statement = new StringBuilder();
+ String accountTypeLabel = getAccountTypeLabel();
+ statement.append(accountTypeLabel);
+ statement.append("\n");
+
+ BigDecimal total = BigDecimalProvider.getZero();
+ for (Transaction transaction : this.getTransactions()) {
+ statement.append(" ");
+ statement.append(transaction.getAmount().compareTo(BigDecimal.ZERO) < 0 ? "withdrawal" : "deposit");
+ statement.append(" ");
+ statement.append(CurrencyStringFormatter.format(transaction.getAmount()));
+ statement.append("\n");
+ total = total.add(transaction.getAmount());
+ }
+ statement.append("Total ");
+ statement.append(CurrencyStringFormatter.format(total));
+
+ return statement.toString();
}
- public int getAccountType() {
- return accountType;
+ protected void verifyTransaction(Transaction transaction) {
+ if (!(transactions.contains(transaction))) {
+ throw new IllegalStateException("This account does not contain given transaction!");
+ }
+
}
+ protected void rollbackTransaction(Transaction transaction) {
+ verifyTransaction(transaction);
+ transactions.remove(transaction);
+ }
+
+
}
diff --git a/src/main/java/com/abc/AccountChecking.java b/src/main/java/com/abc/AccountChecking.java
new file mode 100644
index 00000000..3dc93128
--- /dev/null
+++ b/src/main/java/com/abc/AccountChecking.java
@@ -0,0 +1,20 @@
+package com.abc;
+
+import com.util.BigDecimalProvider;
+
+import java.math.BigDecimal;
+
+public class AccountChecking extends Account {
+ private final BigDecimal CHECKING_RATE = BigDecimalProvider.getInterestRateFormatted(0.001);
+
+ @Override
+ public BigDecimal getInterestEarned() {
+ BigDecimal amount = sumTransactions();
+ return calculateInterest(amount, CHECKING_RATE);
+ }
+
+ @Override
+ protected String getAccountTypeLabel() {
+ return "Checking Account";
+ }
+}
diff --git a/src/main/java/com/abc/AccountMaxiSavings.java b/src/main/java/com/abc/AccountMaxiSavings.java
new file mode 100644
index 00000000..0a00dde7
--- /dev/null
+++ b/src/main/java/com/abc/AccountMaxiSavings.java
@@ -0,0 +1,49 @@
+package com.abc;
+
+import com.util.BigDecimalProvider;
+
+import java.math.BigDecimal;
+
+
+public class AccountMaxiSavings extends Account {
+
+ private final BigDecimal MAXI_SAVINGS_LOW_THRESHOLD_AMOUNT = BigDecimalProvider.format(1000);
+ private final BigDecimal MAXI_SAVINGS_MID_THRESHOLD_AMOUNT = BigDecimalProvider.format(2000);
+ private final BigDecimal MAXI_SAVINGS_RATE_LOW = BigDecimalProvider.getInterestRateFormatted(0.02);
+ private final BigDecimal MAXI_SAVINGS_RATE_MID = BigDecimalProvider.getInterestRateFormatted(0.05);
+ private final BigDecimal MAXI_SAVINGS_RATE_HIGH = BigDecimalProvider.getInterestRateFormatted(0.1);
+
+
+ @Override
+ public BigDecimal getInterestEarned() {
+ BigDecimal amount = sumTransactions();
+ BigDecimal belowThreshold1;
+ BigDecimal belowThreshold2;
+ BigDecimal aboveThreshold;
+
+ if (amount.compareTo(MAXI_SAVINGS_LOW_THRESHOLD_AMOUNT) <= 0) {
+ return calculateInterest(amount, MAXI_SAVINGS_RATE_LOW);
+ }
+
+ if (amount.compareTo(MAXI_SAVINGS_MID_THRESHOLD_AMOUNT) <= 0) {
+ belowThreshold1 = calculateInterest(MAXI_SAVINGS_LOW_THRESHOLD_AMOUNT, MAXI_SAVINGS_RATE_LOW);
+ aboveThreshold = calculateInterest(amount.subtract(MAXI_SAVINGS_LOW_THRESHOLD_AMOUNT), MAXI_SAVINGS_RATE_MID);
+ return belowThreshold1.add(aboveThreshold);
+ }
+
+ belowThreshold1 = calculateInterest(MAXI_SAVINGS_LOW_THRESHOLD_AMOUNT, MAXI_SAVINGS_RATE_LOW);
+ belowThreshold2 = calculateInterest(
+ MAXI_SAVINGS_MID_THRESHOLD_AMOUNT.subtract(MAXI_SAVINGS_LOW_THRESHOLD_AMOUNT),
+ MAXI_SAVINGS_RATE_MID);
+ aboveThreshold = calculateInterest(
+ amount.subtract(MAXI_SAVINGS_MID_THRESHOLD_AMOUNT),
+ MAXI_SAVINGS_RATE_HIGH);
+ return belowThreshold1.add(belowThreshold2).add(aboveThreshold);
+
+ }
+
+ @Override
+ protected String getAccountTypeLabel() {
+ return "Maxi Savings Account";
+ }
+}
diff --git a/src/main/java/com/abc/AccountSavings.java b/src/main/java/com/abc/AccountSavings.java
new file mode 100644
index 00000000..dd1d09ff
--- /dev/null
+++ b/src/main/java/com/abc/AccountSavings.java
@@ -0,0 +1,33 @@
+package com.abc;
+
+import com.util.BigDecimalProvider;
+
+import java.math.BigDecimal;
+
+
+public class AccountSavings extends Account {
+
+ private final BigDecimal SAVINGS_THRESHOLD_AMOUNT = BigDecimalProvider.format(1000);
+ private final BigDecimal SAVINGS_RATE_LOW = BigDecimalProvider.getInterestRateFormatted(0.001);
+ private final BigDecimal SAVINGS_RATE_HIGH = BigDecimalProvider.getInterestRateFormatted(0.002);
+
+ @Override
+ public BigDecimal getInterestEarned() {
+ BigDecimal amount = sumTransactions();
+ BigDecimal belowThreshold1;
+ BigDecimal aboveThreshold;
+
+ if (amount.compareTo(SAVINGS_THRESHOLD_AMOUNT) <= 0) {
+ return calculateInterest(amount, SAVINGS_RATE_LOW);
+ }
+
+ belowThreshold1 = calculateInterest(SAVINGS_THRESHOLD_AMOUNT, SAVINGS_RATE_LOW);
+ aboveThreshold = calculateInterest(amount.subtract(SAVINGS_THRESHOLD_AMOUNT), SAVINGS_RATE_HIGH);
+ return belowThreshold1.add(aboveThreshold);
+ }
+
+ @Override
+ protected String getAccountTypeLabel() {
+ return "Savings Account";
+ }
+}
diff --git a/src/main/java/com/abc/Bank.java b/src/main/java/com/abc/Bank.java
index 5dd535bd..d2fa7311 100644
--- a/src/main/java/com/abc/Bank.java
+++ b/src/main/java/com/abc/Bank.java
@@ -1,46 +1,45 @@
package com.abc;
+import com.util.BigDecimalProvider;
+
+import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class Bank {
- private List customers;
+ private final List customers;
public Bank() {
- customers = new ArrayList();
+ customers = new ArrayList<>();
}
public void addCustomer(Customer customer) {
customers.add(customer);
}
- public String customerSummary() {
- String summary = "Customer Summary";
- for (Customer c : customers)
- summary += "\n - " + c.getName() + " (" + format(c.getNumberOfAccounts(), "account") + ")";
- return summary;
- }
-
- //Make sure correct plural of word is created based on the number passed in:
- //If number passed in is 1 just return the word otherwise add an 's' at the end
- private String format(int number, String word) {
- return number + " " + (number == 1 ? word : word + "s");
+ public String getAllCustomersSummary() {
+ StringBuilder summary = new StringBuilder("Customer Summary");
+ for (Customer customer : customers) {
+ summary.append("\n - ");
+ summary.append(customer.getName());
+ summary.append(" (");
+ int numberOfAccounts = customer.getNumberOfAccounts();
+ summary.append(numberOfAccounts);
+ summary.append(" ");
+ summary.append(numberOfAccounts == 1 ? "account" : "accounts");
+ summary.append(")");
+ }
+ return summary.toString();
}
- public double totalInterestPaid() {
- double total = 0;
- for(Customer c: customers)
- total += c.totalInterestEarned();
+ public BigDecimal getTotalInterestsPaid() {
+ BigDecimal total = BigDecimalProvider.getZero();
+ for (Customer customer : customers)
+ total = total.add(customer.totalInterestEarned());
return total;
}
- public String getFirstCustomer() {
- try {
- customers = null;
- return customers.get(0).getName();
- } catch (Exception e){
- e.printStackTrace();
- return "Error";
- }
+ public boolean hasCustomer(Customer customer) {
+ return customers.contains(customer);
}
}
diff --git a/src/main/java/com/abc/Customer.java b/src/main/java/com/abc/Customer.java
index 31571685..b3bcc235 100644
--- a/src/main/java/com/abc/Customer.java
+++ b/src/main/java/com/abc/Customer.java
@@ -1,78 +1,77 @@
package com.abc;
+import com.util.BigDecimalProvider;
+import com.util.CurrencyStringFormatter;
+
+import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
-import static java.lang.Math.abs;
-
public class Customer {
- private String name;
- private List accounts;
+ private final String name;
+ private final List accounts;
public Customer(String name) {
this.name = name;
- this.accounts = new ArrayList();
+ this.accounts = new ArrayList<>();
}
public String getName() {
return name;
}
- public Customer openAccount(Account account) {
+ public void openAccount(Account account) {
accounts.add(account);
- return this;
+ }
+
+ public void transfer(Account origin, Account target, BigDecimal amount) throws IllegalStateException {
+ if (!(checkAccounts(origin, target))) {
+ throw new IllegalArgumentException("One or more of provided accounts do not belong to the customer!");
+ }
+
+ Transaction withdrawal = origin.withdraw(amount);
+ origin.verifyTransaction(withdrawal);
+
+ try {
+ Transaction deposit = target.deposit(amount);
+ target.verifyTransaction(deposit);
+ } catch (IllegalStateException exception) {
+ origin.rollbackTransaction(withdrawal);
+ throw exception;
+ }
+ }
+
+ private boolean checkAccounts(Account origin, Account target) {
+ return accounts.contains(origin) && accounts.contains(target);
+
}
public int getNumberOfAccounts() {
return accounts.size();
}
- public double totalInterestEarned() {
- double total = 0;
- for (Account a : accounts)
- total += a.interestEarned();
+ public BigDecimal totalInterestEarned() {
+ BigDecimal total = BigDecimalProvider.getZero();
+ for (Account account : accounts) {
+ total = total.add(account.getInterestEarned());
+ }
return total;
}
public String getStatement() {
- String statement = null;
- statement = "Statement for " + name + "\n";
- double total = 0.0;
- for (Account a : accounts) {
- statement += "\n" + statementForAccount(a) + "\n";
- total += a.sumTransactions();
- }
- statement += "\nTotal In All Accounts " + toDollars(total);
- return statement;
- }
+ StringBuilder statement = new StringBuilder("Statement for " + name + "\n");
+ BigDecimal total = BigDecimalProvider.getZero();
- private String statementForAccount(Account a) {
- String s = "";
-
- //Translate to pretty account type
- switch(a.getAccountType()){
- case Account.CHECKING:
- s += "Checking Account\n";
- break;
- case Account.SAVINGS:
- s += "Savings Account\n";
- break;
- case Account.MAXI_SAVINGS:
- s += "Maxi Savings Account\n";
- break;
+ for (Account account : accounts) {
+ statement.append("\n");
+ statement.append(account.getStatement());
+ statement.append("\n");
+ total = total.add(account.sumTransactions());
}
+ statement.append("\nTotal In All Accounts ");
+ statement.append(CurrencyStringFormatter.format(total));
- //Now total up all the transactions
- double total = 0.0;
- for (Transaction t : a.transactions) {
- s += " " + (t.amount < 0 ? "withdrawal" : "deposit") + " " + toDollars(t.amount) + "\n";
- total += t.amount;
- }
- s += "Total " + toDollars(total);
- return s;
+ return statement.toString();
}
- private String toDollars(double d){
- return String.format("$%,.2f", abs(d));
- }
}
diff --git a/src/main/java/com/abc/DateProvider.java b/src/main/java/com/abc/DateProvider.java
deleted file mode 100644
index 035ee90b..00000000
--- a/src/main/java/com/abc/DateProvider.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package com.abc;
-
-import java.util.Calendar;
-import java.util.Date;
-
-public class DateProvider {
- private static DateProvider instance = null;
-
- public static DateProvider getInstance() {
- if (instance == null)
- instance = new DateProvider();
- return instance;
- }
-
- public Date now() {
- return Calendar.getInstance().getTime();
- }
-}
diff --git a/src/main/java/com/abc/Transaction.java b/src/main/java/com/abc/Transaction.java
index c1f7c67e..4f55f295 100644
--- a/src/main/java/com/abc/Transaction.java
+++ b/src/main/java/com/abc/Transaction.java
@@ -1,16 +1,23 @@
package com.abc;
-import java.util.Calendar;
+import java.math.BigDecimal;
+import java.sql.Timestamp;
import java.util.Date;
public class Transaction {
- public final double amount;
+ private final BigDecimal amount;
+ private final Timestamp transactionDate;
- private Date transactionDate;
-
- public Transaction(double amount) {
+ public Transaction(BigDecimal amount) {
this.amount = amount;
- this.transactionDate = DateProvider.getInstance().now();
+ this.transactionDate = new Timestamp(System.currentTimeMillis());
+ }
+
+ public BigDecimal getAmount() {
+ return amount;
}
+ public Date getTransactionDate() {
+ return transactionDate;
+ }
}
diff --git a/src/main/java/com/util/BigDecimalProvider.java b/src/main/java/com/util/BigDecimalProvider.java
new file mode 100644
index 00000000..fb00e64e
--- /dev/null
+++ b/src/main/java/com/util/BigDecimalProvider.java
@@ -0,0 +1,39 @@
+package com.util;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+
+public class BigDecimalProvider {
+ static final int DECIMAL_POINTS_AMOUNT = 2;
+ static final int DECIMAL_POINTS_INTEREST_RATE = 3;
+ static final RoundingMode ROUNDING_MODE = RoundingMode.HALF_EVEN;
+
+ public static BigDecimal format(long value) {
+ return configureAmount(BigDecimal.valueOf(value));
+ }
+
+ public static BigDecimal format(double value) {
+ return configureAmount(BigDecimal.valueOf(value));
+ }
+
+ public static BigDecimal format(BigDecimal value) {
+ return configureAmount(value);
+ }
+
+ public static BigDecimal getZero() {
+ return format(0);
+ }
+
+ public static BigDecimal getInterestRateFormatted(double value) {
+ return BigDecimal.valueOf(value);
+ }
+
+ private static BigDecimal configureAmount(BigDecimal value) {
+ return value.setScale(DECIMAL_POINTS_AMOUNT, ROUNDING_MODE);
+ }
+
+ private static BigDecimal configureInterestRate(BigDecimal value) {
+ return value.setScale(DECIMAL_POINTS_INTEREST_RATE, ROUNDING_MODE);
+ }
+
+}
diff --git a/src/main/java/com/util/CurrencyStringFormatter.java b/src/main/java/com/util/CurrencyStringFormatter.java
new file mode 100644
index 00000000..ac8a133c
--- /dev/null
+++ b/src/main/java/com/util/CurrencyStringFormatter.java
@@ -0,0 +1,18 @@
+package com.util;
+
+import java.math.BigDecimal;
+import java.text.NumberFormat;
+import java.util.Locale;
+
+public class CurrencyStringFormatter {
+ public static String format(BigDecimal amount) {
+ Locale locale = getLocale();
+ NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(getLocale());
+
+ return currencyFormatter.format(amount);
+ }
+
+ private static Locale getLocale() {
+ return new Locale("en", "US"); // default
+ }
+}
diff --git a/src/test/java/com/abc/AccountCheckingTest.java b/src/test/java/com/abc/AccountCheckingTest.java
new file mode 100644
index 00000000..50d77d08
--- /dev/null
+++ b/src/test/java/com/abc/AccountCheckingTest.java
@@ -0,0 +1,31 @@
+package com.abc;
+
+import com.util.BigDecimalProvider;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class AccountCheckingTest {
+ Account account;
+
+ @Before
+ public void setup() {
+ account = new AccountChecking();
+ account.deposit(BigDecimalProvider.format(100));
+ account.deposit(BigDecimalProvider.format(4000));
+ account.withdraw(BigDecimalProvider.format(200));
+ }
+
+ @Test
+ public void getStatementTest() {
+ assertEquals(
+ "Checking Account\n deposit $100.00\n deposit $4,000.00\n withdrawal -$200.00\nTotal $3,900.00",
+ account.getStatement());
+ }
+
+ @Test
+ public void interestEarnedTest() {
+ assertEquals(BigDecimalProvider.format(3.9), account.getInterestEarned());
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/abc/AccountMaxiSavingsTest.java b/src/test/java/com/abc/AccountMaxiSavingsTest.java
new file mode 100644
index 00000000..265bb8f7
--- /dev/null
+++ b/src/test/java/com/abc/AccountMaxiSavingsTest.java
@@ -0,0 +1,50 @@
+package com.abc;
+
+import com.util.BigDecimalProvider;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class AccountMaxiSavingsTest {
+ Account account;
+
+ @Before
+ public void setUp() throws Exception {
+ account = new AccountMaxiSavings();
+ account.deposit(BigDecimalProvider.format(400));
+ account.deposit(BigDecimalProvider.format(200));
+ account.withdraw(BigDecimalProvider.format(100));
+ }
+
+ @Test
+ public void getStatement() {
+ assertEquals(
+ "Maxi Savings Account\n deposit $400.00\n deposit $200.00\n withdrawal -$100.00\nTotal $500.00",
+ account.getStatement());
+ }
+
+ @Test
+ public void interestEarned() {
+
+ }
+
+ @Test
+ public void getInterestsEarned_LOW() {
+ account.deposit(BigDecimalProvider.format(87));
+ assertEquals(BigDecimalProvider.format(11.74), account.getInterestEarned());
+ }
+
+ @Test
+ public void getInterestsEarned_MID() {
+ account.deposit(BigDecimalProvider.format(1087));
+ assertEquals(BigDecimalProvider.format(49.35), account.getInterestEarned());
+ }
+
+ @Test
+ public void getInterestsEarned_HIGH() {
+ account.deposit(BigDecimalProvider.format(2087));
+ assertEquals(BigDecimalProvider.format(128.70), account.getInterestEarned());
+ }
+
+}
\ No newline at end of file
diff --git a/src/test/java/com/abc/AccountSavingsTest.java b/src/test/java/com/abc/AccountSavingsTest.java
new file mode 100644
index 00000000..be7afb99
--- /dev/null
+++ b/src/test/java/com/abc/AccountSavingsTest.java
@@ -0,0 +1,39 @@
+package com.abc;
+
+import com.util.BigDecimalProvider;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class AccountSavingsTest {
+ Account account;
+
+ @Before
+ public void setup() {
+ account = new AccountSavings();
+ account.deposit(BigDecimalProvider.format(400));
+ account.deposit(BigDecimalProvider.format(200));
+ account.withdraw(BigDecimalProvider.format(100));
+ }
+
+ @Test
+ public void getStatement() {
+ assertEquals(
+ "Savings Account\n deposit $400.00\n deposit $200.00\n withdrawal -$100.00\nTotal $500.00",
+ account.getStatement());
+ }
+
+ @Test
+ public void getInterestEarnedTest_LOW() {
+ account.deposit(BigDecimalProvider.format(298));
+ assertEquals(BigDecimalProvider.format(0.80), account.getInterestEarned());
+ }
+
+ @Test
+ public void getInterestEarnedTest_HIGH() {
+ account.deposit(BigDecimalProvider.format(3265));
+ assertEquals(BigDecimalProvider.format(6.53), account.getInterestEarned());
+ }
+
+}
\ No newline at end of file
diff --git a/src/test/java/com/abc/BankTest.java b/src/test/java/com/abc/BankTest.java
index f8a82896..ae059b54 100644
--- a/src/test/java/com/abc/BankTest.java
+++ b/src/test/java/com/abc/BankTest.java
@@ -1,54 +1,88 @@
package com.abc;
+import com.util.BigDecimalProvider;
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class BankTest {
- private static final double DOUBLE_DELTA = 1e-15;
- @Test
- public void customerSummary() {
- Bank bank = new Bank();
- Customer john = new Customer("John");
- john.openAccount(new Account(Account.CHECKING));
- bank.addCustomer(john);
+ Bank bank;
+ Customer customer;
+
+ @Before
+ public void setUp() {
+ bank = new Bank();
+ customer = new Customer("John");
+ bank.addCustomer(customer);
+ }
- assertEquals("Customer Summary\n - John (1 account)", bank.customerSummary());
+ @After
+ public void tearDown() {
+ bank = null;
+ customer = null;
}
@Test
- public void checkingAccount() {
- Bank bank = new Bank();
- Account checkingAccount = new Account(Account.CHECKING);
- Customer bill = new Customer("Bill").openAccount(checkingAccount);
- bank.addCustomer(bill);
+ public void addCustomerTest() {
+ bank.addCustomer(customer);
+ assertEquals(true, bank.hasCustomer(customer));
+ }
- checkingAccount.deposit(100.0);
+ @Test
+ public void getAllCustomersSummaryTest() {
+ customer.openAccount(new AccountChecking());
+ assertEquals("Customer Summary\n - John (1 account)", bank.getAllCustomersSummary());
+ }
- assertEquals(0.1, bank.totalInterestPaid(), DOUBLE_DELTA);
+ @Test
+ public void getTotalInterestsPaidTest_CHECKING() {
+ AccountChecking accountChecking = new AccountChecking();
+ customer.openAccount(accountChecking);
+ accountChecking.deposit(BigDecimalProvider.format(798));
+ assertEquals(BigDecimalProvider.format(0.80), bank.getTotalInterestsPaid());
}
@Test
- public void savings_account() {
- Bank bank = new Bank();
- Account checkingAccount = new Account(Account.SAVINGS);
- bank.addCustomer(new Customer("Bill").openAccount(checkingAccount));
+ public void getTotalInterestsPaidTest_SAVINGS_LOW() {
+ Account account = new AccountSavings();
+ customer.openAccount(account);
+ account.deposit(BigDecimalProvider.format(798));
+ assertEquals(BigDecimalProvider.format(0.80), bank.getTotalInterestsPaid());
+ }
- checkingAccount.deposit(1500.0);
+ @Test
+ public void getTotalInterestsPaidTest_SAVINGS_HIGH() {
+ Account account = new AccountSavings();
+ customer.openAccount(account);
+ account.deposit(BigDecimalProvider.format(3765));
+ assertEquals(BigDecimalProvider.format(6.53), bank.getTotalInterestsPaid());
+ }
- assertEquals(2.0, bank.totalInterestPaid(), DOUBLE_DELTA);
+ @Test
+ public void getTotalInterestsPaidTest_MAXI_SAVINGS_LOW() {
+ Account account = new AccountMaxiSavings();
+ customer.openAccount(account);
+ account.deposit(BigDecimalProvider.format(587));
+ assertEquals(BigDecimalProvider.format(11.74), bank.getTotalInterestsPaid());
}
@Test
- public void maxi_savings_account() {
- Bank bank = new Bank();
- Account checkingAccount = new Account(Account.MAXI_SAVINGS);
- bank.addCustomer(new Customer("Bill").openAccount(checkingAccount));
+ public void getTotalInterestsPaidTest_MAXI_SAVINGS_MID() {
+ Account account = new AccountMaxiSavings();
+ customer.openAccount(account);
+ account.deposit(BigDecimalProvider.format(1587));
+ assertEquals(BigDecimalProvider.format(49.35), bank.getTotalInterestsPaid());
+ }
- checkingAccount.deposit(3000.0);
- assertEquals(170.0, bank.totalInterestPaid(), DOUBLE_DELTA);
+ @Test
+ public void getTotalInterestsPaidTest_MAXI_SAVINGS_HIGH() {
+ Account account = new AccountMaxiSavings();
+ customer.openAccount(account);
+ account.deposit(BigDecimalProvider.format(2587));
+ assertEquals(BigDecimalProvider.format(128.70), bank.getTotalInterestsPaid());
}
-
}
diff --git a/src/test/java/com/abc/CustomerTest.java b/src/test/java/com/abc/CustomerTest.java
index b8df9498..1355551e 100644
--- a/src/test/java/com/abc/CustomerTest.java
+++ b/src/test/java/com/abc/CustomerTest.java
@@ -1,57 +1,129 @@
package com.abc;
-import org.junit.Ignore;
+import com.util.BigDecimalProvider;
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
+import java.math.BigDecimal;
+
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
public class CustomerTest {
+ Customer customer;
+ AccountChecking checkingAccount;
+ AccountSavings savingsAccount;
+
+ @Before
+ public void setup() {
+ customer = new Customer("Henry");
+ checkingAccount = new AccountChecking();
+ savingsAccount = new AccountSavings();
+ }
+
+ @After
+ public void tearDown() {
+ customer = null;
+ checkingAccount = null;
+ savingsAccount = null;
+ }
+
@Test //Test customer statement generation
- public void testApp(){
+ public void getStatementTest() {
+ customer.openAccount(checkingAccount);
+ customer.openAccount(savingsAccount);
+
+ checkingAccount.deposit(BigDecimal.valueOf(100.0));
+ savingsAccount.deposit(BigDecimal.valueOf(4000.0));
+ savingsAccount.withdraw(BigDecimal.valueOf(200.0));
+
+ assertEquals(
+ "Statement for Henry\n\nChecking Account\n deposit $100.00\nTotal $100.00\n\nSavings Account\n deposit $4,000.00\n withdrawal -$200.00\nTotal $3,800.00\n\nTotal In All Accounts $3,900.00",
+ customer.getStatement());
+ }
+
+ @Test
+ public void openAccount_ONE() {
+ customer.openAccount(checkingAccount);
+ assertEquals(1, customer.getNumberOfAccounts());
+ }
+
+ @Test
+ public void openAccount_TWO() {
+ customer.openAccount(checkingAccount);
+ customer.openAccount(savingsAccount);
+ assertEquals(2, customer.getNumberOfAccounts());
+ }
+
+ @Test
+ public void getName() {
+ assertEquals("Henry", customer.getName());
+ }
+
+ @Test
+ public void totalInterestEarned() {
+ customer.openAccount(checkingAccount);
+ customer.openAccount(savingsAccount);
+ Account maxiSavingsACcount = new AccountMaxiSavings();
+ customer.openAccount(maxiSavingsACcount);
- Account checkingAccount = new Account(Account.CHECKING);
- Account savingsAccount = new Account(Account.SAVINGS);
+ checkingAccount.deposit(BigDecimalProvider.format(1928)); // 1.93
+ savingsAccount.deposit(BigDecimalProvider.format(3765)); // 6.53
+ maxiSavingsACcount.deposit(BigDecimalProvider.format(2587)); // 128.70
- Customer henry = new Customer("Henry").openAccount(checkingAccount).openAccount(savingsAccount);
+ assertEquals(BigDecimalProvider.format(137.16), customer.totalInterestEarned());
- checkingAccount.deposit(100.0);
- savingsAccount.deposit(4000.0);
- savingsAccount.withdraw(200.0);
- assertEquals("Statement for Henry\n" +
- "\n" +
- "Checking Account\n" +
- " deposit $100.00\n" +
- "Total $100.00\n" +
- "\n" +
- "Savings Account\n" +
- " deposit $4,000.00\n" +
- " withdrawal $200.00\n" +
- "Total $3,800.00\n" +
- "\n" +
- "Total In All Accounts $3,900.00", henry.getStatement());
}
@Test
- public void testOneAccount(){
- Customer oscar = new Customer("Oscar").openAccount(new Account(Account.SAVINGS));
- assertEquals(1, oscar.getNumberOfAccounts());
+ public void transferTest_OK() {
+ customer.openAccount(checkingAccount);
+ customer.openAccount(savingsAccount);
+ customer.transfer(checkingAccount, savingsAccount, BigDecimalProvider.format(100));
}
@Test
- public void testTwoAccount(){
- Customer oscar = new Customer("Oscar")
- .openAccount(new Account(Account.SAVINGS));
- oscar.openAccount(new Account(Account.CHECKING));
- assertEquals(2, oscar.getNumberOfAccounts());
+ public void transferTest_Fail1() {
+ try {
+ customer.openAccount(checkingAccount);
+ customer.transfer(checkingAccount, savingsAccount, BigDecimalProvider.format(100));
+ fail("Expected exception has not occured!");
+ } catch (IllegalArgumentException e) {
+ // ok
+ } catch (Exception e) {
+ throw e;
+ }
}
- @Ignore
- public void testThreeAcounts() {
- Customer oscar = new Customer("Oscar")
- .openAccount(new Account(Account.SAVINGS));
- oscar.openAccount(new Account(Account.CHECKING));
- assertEquals(3, oscar.getNumberOfAccounts());
+ @Test
+ public void transferTest_Fail2() {
+ try {
+ customer.openAccount(savingsAccount);
+ customer.transfer(checkingAccount, savingsAccount, BigDecimalProvider.format(100));
+ fail("Expected exception has not occured!");
+ } catch (IllegalArgumentException e) {
+ // ok
+ } catch (Exception e) {
+ throw e;
+ }
+ }
+
+ @Test
+ public void transferTest_Fail3() {
+ try {
+ customer.openAccount(checkingAccount);
+ customer.openAccount(savingsAccount);
+ customer.transfer(checkingAccount, savingsAccount, BigDecimalProvider.format(-100));
+ fail("Expected exception has not occured!");
+ } catch (IllegalArgumentException e) {
+ // ok
+ } catch (Exception e) {
+ throw e;
+ }
}
+
+
}
diff --git a/src/test/java/com/abc/TransactionTest.java b/src/test/java/com/abc/TransactionTest.java
index 28983234..57a66cb8 100644
--- a/src/test/java/com/abc/TransactionTest.java
+++ b/src/test/java/com/abc/TransactionTest.java
@@ -2,12 +2,14 @@
import org.junit.Test;
+import java.math.BigDecimal;
+
import static org.junit.Assert.assertTrue;
public class TransactionTest {
@Test
public void transaction() {
- Transaction t = new Transaction(5);
+ Transaction t = new Transaction(BigDecimal.valueOf(5));
assertTrue(t instanceof Transaction);
}
}
diff --git a/src/test/java/com/util/BigDecimalProviderTest.java b/src/test/java/com/util/BigDecimalProviderTest.java
new file mode 100644
index 00000000..c005b611
--- /dev/null
+++ b/src/test/java/com/util/BigDecimalProviderTest.java
@@ -0,0 +1,39 @@
+package com.util;
+
+import org.junit.Test;
+
+import java.math.BigDecimal;
+
+import static org.junit.Assert.assertEquals;
+
+public class BigDecimalProviderTest {
+
+ @Test
+ public void getAmountFormatted_LONG() {
+ assertEquals(
+ BigDecimal.valueOf(12345.00).setScale(BigDecimalProvider.DECIMAL_POINTS_AMOUNT, BigDecimalProvider.ROUNDING_MODE),
+ BigDecimalProvider.format(12345));
+ }
+
+ @Test
+ public void getAmountFormatted_DOUBLE() {
+ assertEquals(
+ BigDecimal.valueOf(123.45).setScale(BigDecimalProvider.DECIMAL_POINTS_AMOUNT, BigDecimalProvider.ROUNDING_MODE),
+ BigDecimalProvider.format(123.45));
+ }
+
+ @Test
+ public void testGetZero() {
+ assertEquals(
+ BigDecimal.ZERO.setScale(BigDecimalProvider.DECIMAL_POINTS_AMOUNT, BigDecimalProvider.ROUNDING_MODE),
+ BigDecimalProvider.format(0));
+ }
+
+ @Test
+ public void getInterestRateFormatted() {
+ assertEquals(
+ BigDecimal.valueOf(0.100).setScale(BigDecimalProvider.DECIMAL_POINTS_AMOUNT, BigDecimalProvider.ROUNDING_MODE),
+ BigDecimalProvider.format(0.1));
+ }
+
+}
\ No newline at end of file
diff --git a/src/test/java/com/util/CurrencyStringFormatterTest.java b/src/test/java/com/util/CurrencyStringFormatterTest.java
new file mode 100644
index 00000000..5c7785d0
--- /dev/null
+++ b/src/test/java/com/util/CurrencyStringFormatterTest.java
@@ -0,0 +1,17 @@
+package com.util;
+
+import org.junit.Test;
+
+import java.math.BigDecimal;
+
+import static org.junit.Assert.assertEquals;
+
+public class CurrencyStringFormatterTest {
+
+ @Test
+ public void formatTest() {
+
+ assertEquals("$123.00", CurrencyStringFormatter.format(BigDecimal.valueOf(123.00)));
+ }
+
+}
\ No newline at end of file