diff --git a/src/main/java/com/abc/Account.java b/src/main/java/com/abc/Account.java index 099691e0..bd633419 100644 --- a/src/main/java/com/abc/Account.java +++ b/src/main/java/com/abc/Account.java @@ -2,6 +2,8 @@ import java.util.ArrayList; import java.util.List; +import java.util.Calendar; +import java.util.Date; public class Account { @@ -11,10 +13,13 @@ public class Account { private final int accountType; public List transactions; + private Date lastDeposit; + private Date lastWithdrawal; public Account(int accountType) { this.accountType = accountType; this.transactions = new ArrayList(); + lastDeposit = lastWithdrawal = DateProvider.getInstance().now(); } public void deposit(double amount) { @@ -22,36 +27,63 @@ public void deposit(double amount) { throw new IllegalArgumentException("amount must be greater than zero"); } else { transactions.add(new Transaction(amount)); + lastDeposit = DateProvider.getInstance().now(); } } - + //Just made this to test the date variables. If they are recording properly then that suggests that the maxi-function operates correctly. + public String getDepositDate(){ + return lastDeposit.toString(); + } + + public String getWithdrawalDate(){ + return lastWithdrawal.toString(); + } + //Remove after testing. This is just to test the maxi-savings over 10 days + public void setWithdrawlDate(Date date){ + lastWithdrawal = date; + } + public void withdraw(double amount) { if (amount <= 0) { throw new IllegalArgumentException("amount must be greater than zero"); } else { transactions.add(new Transaction(-amount)); + lastWithdrawal = DateProvider.getInstance().now(); } } +//I added in the accrue daily rates. This method will now return just the interest for 1 day rather than a year. +//In the future the program needs a timer so that everyday at a given time this method is called and the daily interest +//is added to each customer's account. public double interestEarned() { double amount = sumTransactions(); switch(accountType){ case SAVINGS: if (amount <= 1000) - return amount * 0.001; + //0.01% for first 1000 per-annum + return amount * (0.001/365); else - return 1 + (amount-1000) * 0.002; + //0.2% after first 1000 per-annum + return 1 + (amount-1000) * (0.002/365); // case SUPER_SAVINGS: // if (amount <= 4000) // return 20; case MAXI_SAVINGS: - if (amount <= 1000) + Date currentDate = DateProvider.getInstance().now(); + //5% per annum after 10 days without withdrawing + if(currentDate.compareTo(lastWithdrawal)>=10){ + return amount * (0.05/365); + } + //.1% per annum if the customer withdrew in the past 10 days + return amount * (0.001/365); + //Previous code for the MAXI_SAVINGS account interest calculation. + /*if (amount <= 1000) return amount * 0.02; if (amount <= 2000) return 20 + (amount-1000) * 0.05; - return 70 + (amount-2000) * 0.1; + return 70 + (amount-2000) * 0.1;*/ default: - return amount * 0.001; + return amount * (0.001/365); } } @@ -69,5 +101,5 @@ private double checkIfTransactionsExist(boolean checkAll) { public int getAccountType() { return accountType; } - + } diff --git a/src/main/java/com/abc/Bank.java b/src/main/java/com/abc/Bank.java index 5dd535bd..cdb86672 100644 --- a/src/main/java/com/abc/Bank.java +++ b/src/main/java/com/abc/Bank.java @@ -5,7 +5,8 @@ public class Bank { private List customers; - + private int idCount = 0; + public Bank() { customers = new ArrayList(); } @@ -36,7 +37,8 @@ public double totalInterestPaid() { public String getFirstCustomer() { try { - customers = null; + //Setting customers to null will erase the customer list. + //customers = null; return customers.get(0).getName(); } catch (Exception e){ e.printStackTrace(); diff --git a/src/main/java/com/abc/Customer.java b/src/main/java/com/abc/Customer.java index 31571685..b0e97b01 100644 --- a/src/main/java/com/abc/Customer.java +++ b/src/main/java/com/abc/Customer.java @@ -7,16 +7,26 @@ public class Customer { private String name; + //It's important to have a customer id so if two customers named "John Smith" come along you can differentiate between them. + //Ideally you would use a hash function to create a unique id or maybe a list of all customers and set the id equal to their + //position in the list. + private int customer_id; + private List accounts; - public Customer(String name) { + public Customer(String name, int id) { this.name = name; this.accounts = new ArrayList(); + this.customer_id = id; } public String getName() { return name; } + + public int getCustomer_id(){ + return customer_id; + } public Customer openAccount(Account account) { accounts.add(account); @@ -75,4 +85,15 @@ private String statementForAccount(Account a) { private String toDollars(double d){ return String.format("$%,.2f", abs(d)); } + + //To transfer between accounts just withdraw from account 1 and deposit that amount in account 2. + public void transferBtwnAccounts(Account transferFrom, Account transferTo, double amount){ + if(amount <= 0){ + throw new IllegalArgumentException("amount must be greater than zero"); + } + else{ + transferFrom.withdraw(amount); + transferTo.deposit(amount); + } + } } diff --git a/src/main/java/com/abc/DateProvider.java b/src/main/java/com/abc/DateProvider.java index 035ee90b..be7c40b1 100644 --- a/src/main/java/com/abc/DateProvider.java +++ b/src/main/java/com/abc/DateProvider.java @@ -15,4 +15,5 @@ public static DateProvider getInstance() { 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..b9739cc5 100644 --- a/src/main/java/com/abc/Transaction.java +++ b/src/main/java/com/abc/Transaction.java @@ -1,6 +1,6 @@ package com.abc; -import java.util.Calendar; +//import java.util.Calendar; import java.util.Date; public class Transaction { @@ -12,5 +12,5 @@ public Transaction(double amount) { this.amount = amount; this.transactionDate = DateProvider.getInstance().now(); } - + } diff --git a/src/test/java/com/abc/BankTest.java b/src/test/java/com/abc/BankTest.java index f8a82896..69fbd32f 100644 --- a/src/test/java/com/abc/BankTest.java +++ b/src/test/java/com/abc/BankTest.java @@ -1,16 +1,31 @@ package com.abc; import org.junit.Test; +import java.util.Calendar; +import java.util.Date; import static org.junit.Assert.assertEquals; public class BankTest { private static final double DOUBLE_DELTA = 1e-15; + int customer_id =0; + @Test + public void firstCustomer() { + Bank bank = new Bank(); + assertEquals("Error",bank.getFirstCustomer()); + + Customer john = new Customer("John", customer_id++); + john.openAccount(new Account(Account.SAVINGS)); + bank.addCustomer(john); + + assertEquals("John", bank.getFirstCustomer()); + } + @Test public void customerSummary() { Bank bank = new Bank(); - Customer john = new Customer("John"); + Customer john = new Customer("John",customer_id++); john.openAccount(new Account(Account.CHECKING)); bank.addCustomer(john); @@ -21,34 +36,34 @@ public void customerSummary() { public void checkingAccount() { Bank bank = new Bank(); Account checkingAccount = new Account(Account.CHECKING); - Customer bill = new Customer("Bill").openAccount(checkingAccount); + Customer bill = new Customer("Bill", customer_id++).openAccount(checkingAccount); bank.addCustomer(bill); checkingAccount.deposit(100.0); - assertEquals(0.1, bank.totalInterestPaid(), DOUBLE_DELTA); + assertEquals((100 *(.001/365)), bank.totalInterestPaid(), DOUBLE_DELTA); } @Test public void savings_account() { Bank bank = new Bank(); - Account checkingAccount = new Account(Account.SAVINGS); - bank.addCustomer(new Customer("Bill").openAccount(checkingAccount)); + Account savingsAccount = new Account(Account.SAVINGS); + bank.addCustomer(new Customer("Bill", customer_id++).openAccount(savingsAccount)); - checkingAccount.deposit(1500.0); + savingsAccount.deposit(1500.0); - assertEquals(2.0, bank.totalInterestPaid(), DOUBLE_DELTA); + assertEquals((1 + (1500-1000) * (0.002/365)), bank.totalInterestPaid(), DOUBLE_DELTA); } @Test public void maxi_savings_account() { Bank bank = new Bank(); - Account checkingAccount = new Account(Account.MAXI_SAVINGS); - bank.addCustomer(new Customer("Bill").openAccount(checkingAccount)); + Account maxiAccount = new Account(Account.MAXI_SAVINGS); + bank.addCustomer(new Customer("Bill", customer_id++).openAccount(maxiAccount)); - checkingAccount.deposit(3000.0); + maxiAccount.deposit(3000.0); - assertEquals(170.0, bank.totalInterestPaid(), DOUBLE_DELTA); + assertEquals((3000*(.001/365)), bank.totalInterestPaid(), DOUBLE_DELTA); } - + //need to test maxi saving for 10+ days. } diff --git a/src/test/java/com/abc/CustomerTest.java b/src/test/java/com/abc/CustomerTest.java index b8df9498..f408a30e 100644 --- a/src/test/java/com/abc/CustomerTest.java +++ b/src/test/java/com/abc/CustomerTest.java @@ -6,6 +6,8 @@ import static org.junit.Assert.assertEquals; public class CustomerTest { + int customer_id = 0; + private static final double DOUBLE_DELTA = 1e-15; @Test //Test customer statement generation public void testApp(){ @@ -13,7 +15,7 @@ public void testApp(){ Account checkingAccount = new Account(Account.CHECKING); Account savingsAccount = new Account(Account.SAVINGS); - Customer henry = new Customer("Henry").openAccount(checkingAccount).openAccount(savingsAccount); + Customer henry = new Customer("Henry", customer_id++).openAccount(checkingAccount).openAccount(savingsAccount); checkingAccount.deposit(100.0); savingsAccount.deposit(4000.0); @@ -35,23 +37,38 @@ public void testApp(){ @Test public void testOneAccount(){ - Customer oscar = new Customer("Oscar").openAccount(new Account(Account.SAVINGS)); + Customer oscar = new Customer("Oscar", customer_id++).openAccount(new Account(Account.SAVINGS)); assertEquals(1, oscar.getNumberOfAccounts()); } @Test public void testTwoAccount(){ - Customer oscar = new Customer("Oscar") + Customer oscar = new Customer("Oscar", customer_id++) .openAccount(new Account(Account.SAVINGS)); oscar.openAccount(new Account(Account.CHECKING)); assertEquals(2, oscar.getNumberOfAccounts()); } - @Ignore + @Test public void testThreeAcounts() { - Customer oscar = new Customer("Oscar") + Customer oscar = new Customer("Oscar", customer_id++) .openAccount(new Account(Account.SAVINGS)); oscar.openAccount(new Account(Account.CHECKING)); + oscar.openAccount(new Account(Account.CHECKING)); assertEquals(3, oscar.getNumberOfAccounts()); } + + @Test + public void transferBetweenAccounts() { + Account savings = new Account(Account.SAVINGS); + savings.deposit(10000); + Account checking = new Account(Account.CHECKING); + checking.deposit(200); + Customer oscar = new Customer("Oscar", customer_id++) + .openAccount(savings); + oscar.openAccount(checking); + oscar.transferBtwnAccounts(savings, checking, 3000); + assertEquals(7000.0, savings.sumTransactions(),DOUBLE_DELTA); + assertEquals(3200.0, checking.sumTransactions(),DOUBLE_DELTA); + } }