diff --git a/src/main/java/com/abc/Account.java b/src/main/java/com/abc/Account.java index 099691e0..f13264c7 100644 --- a/src/main/java/com/abc/Account.java +++ b/src/main/java/com/abc/Account.java @@ -1,72 +1,111 @@ package com.abc; +import java.util.Date; import java.util.ArrayList; +import java.util.Calendar; import java.util.List; +// import java.util.concurrent.TimeUnit; -public class Account { +import com.abc.Enums.AccountType; +import com.abc.Enums.TransactionType; +import static com.abc.AccountUtility.isBalanceEnough; +import static com.abc.AccountUtility.isValidAmount; +import static com.abc.AccountUtility.daysSinceFirstTransaction; - public static final int CHECKING = 0; - public static final int SAVINGS = 1; - public static final int MAXI_SAVINGS = 2; +public class Account { - private final int accountType; + private final AccountType accountType; public List transactions; + private double balance = 0.0; - public Account(int accountType) { + public Account(AccountType accountType) { this.accountType = accountType; this.transactions = new ArrayList(); } - public void deposit(double amount) { - if (amount <= 0) { - throw new IllegalArgumentException("amount must be greater than zero"); - } else { - transactions.add(new Transaction(amount)); - } + public void deposit(double amount, TransactionType transactionType) { + isValidAmount(amount); + addBalance(amount); + transactions.add(new Transaction(amount, TransactionType.DEPOSIT)); + + } + + public void withdraw(double amount, TransactionType transactionType) { + isValidAmount(amount); + isBalanceEnough(amount, this); + removeBalance(amount); + transactions.add(new Transaction(amount, transactionType)); + } -public void withdraw(double amount) { - if (amount <= 0) { - throw new IllegalArgumentException("amount must be greater than zero"); - } else { - transactions.add(new Transaction(-amount)); + // Balance functions + public double getBalance() { + return balance; + } + + public void addBalance(double amount) { + double tempBalance = this.getBalance(); + tempBalance += amount; + setBalance(tempBalance); + } + + public void removeBalance(double amount) { + double tempBalance = this.getBalance(); + tempBalance -= amount; + setBalance(tempBalance); + } + + public void setBalance(double balance) { + this.balance = balance; } -} public double interestEarned() { double amount = sumTransactions(); - switch(accountType){ + + ; + switch (accountType) { case SAVINGS: if (amount <= 1000) - return amount * 0.001; + return amount * (Math.pow(1 + (0.001 / 365), daysSinceFirstTransaction(this)) - 1); else - return 1 + (amount-1000) * 0.002; -// case SUPER_SAVINGS: -// if (amount <= 4000) -// return 20; + return (1000 * (Math.pow(1 + (0.001 / 365), daysSinceFirstTransaction(this)) - 1)) + + (amount - 1000) * (Math.pow(1 + (0.002 / 365), daysSinceFirstTransaction(this)) - 1); 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; + if (!checkWithdrawInPastTenDays()) { + return amount * (Math.pow(1 + (0.05 / 365), daysSinceFirstTransaction(this)) - 1); + } else { + // 0.1% interest rate + return amount * (Math.pow(1 + (0.001 / 365), daysSinceFirstTransaction(this)) - 1); + } default: - return amount * 0.001; + return amount * (Math.pow(1 + (0.001 / 365), daysSinceFirstTransaction(this)) - 1); } } - public double sumTransactions() { - return checkIfTransactionsExist(true); + private boolean checkWithdrawInPastTenDays() { + Date curDate = new Date(); + Calendar calendar = Calendar.getInstance(); + calendar.setTime(curDate); + calendar.add(Calendar.DAY_OF_MONTH, -10); + Date tenDaysBackDate = calendar.getTime(); + for (Transaction t : transactions) { + if (t.getTransactionDate().after(tenDaysBackDate)) { // did withdraw + if (t.getTransactionType() == TransactionType.WITHDRAW) { + return true; + } + } + } + return false; } - private double checkIfTransactionsExist(boolean checkAll) { + public double sumTransactions() { double amount = 0.0; - for (Transaction t: transactions) - amount += t.amount; + for (Transaction t : transactions) + amount += t.getAmount(); return amount; } - public int getAccountType() { + public AccountType getAccountType() { return accountType; } diff --git a/src/main/java/com/abc/AccountUtility.java b/src/main/java/com/abc/AccountUtility.java new file mode 100644 index 00000000..6c17f156 --- /dev/null +++ b/src/main/java/com/abc/AccountUtility.java @@ -0,0 +1,31 @@ +package com.abc; +import java.util.Date; +import java.util.concurrent.TimeUnit; + +public class AccountUtility { + public static void isValidAmount(double amount) { + if (amount <= 0) { + throw new Error("Amount has to be greater than zero"); + } + } + + public static void isBalanceEnough(double amount, Account account) { + if(account.getBalance() < amount) { + throw new Error( "You don't have sufficient funds in your account."); + } + } + + public static double daysSinceFirstTransaction(Account account) { + System.out.println("In days since first transaction"); + + Date firstTransactionDate = account.transactions.get(0).getTransactionDate(); + System.out.println(firstTransactionDate); + Date lastTransactionDate = account.transactions.get(account.transactions.size()-1).getTransactionDate(); + System.out.println(lastTransactionDate); + + long diffInMilliseconds = Math.abs(lastTransactionDate.getTime() - firstTransactionDate.getTime()); + // Convert milliseconds to days + double diffInDays = Math.floor(TimeUnit.DAYS.convert(diffInMilliseconds, TimeUnit.MILLISECONDS)); + return diffInDays; + } +} diff --git a/src/main/java/com/abc/Bank.java b/src/main/java/com/abc/Bank.java index 5dd535bd..9a868dae 100644 --- a/src/main/java/com/abc/Bank.java +++ b/src/main/java/com/abc/Bank.java @@ -36,7 +36,6 @@ public double totalInterestPaid() { public String getFirstCustomer() { try { - customers = null; return customers.get(0).getName(); } catch (Exception e){ e.printStackTrace(); @@ -44,3 +43,4 @@ public String getFirstCustomer() { } } } + diff --git a/src/main/java/com/abc/Customer.java b/src/main/java/com/abc/Customer.java index 31571685..32304ac2 100644 --- a/src/main/java/com/abc/Customer.java +++ b/src/main/java/com/abc/Customer.java @@ -3,6 +3,8 @@ import java.util.ArrayList; import java.util.List; +import com.abc.Enums.TransactionType; + import static java.lang.Math.abs; public class Customer { @@ -46,31 +48,39 @@ public String getStatement() { return statement; } - private String statementForAccount(Account a) { + private String statementForAccount(Account account) { String s = ""; //Translate to pretty account type - switch(a.getAccountType()){ - case Account.CHECKING: + switch(account.getAccountType()){ + case CHECKING: s += "Checking Account\n"; break; - case Account.SAVINGS: + case SAVINGS: s += "Savings Account\n"; break; - case Account.MAXI_SAVINGS: + case MAXI_SAVINGS: s += "Maxi Savings Account\n"; break; } //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"; + for (Transaction t : account.transactions) { + s += " " + (t.getTransactionType() == TransactionType.WITHDRAW ? "withdrawal" : "deposit") + " " + toDollars(t.amount) + "\n"; total += t.amount; } s += "Total " + toDollars(total); return s; } + public void transferBetween(Account a, Account b, double amount ) { + a.withdraw(amount, TransactionType.WITHDRAW); + b.deposit(amount,TransactionType.DEPOSIT); + } + //transfer function (account a, account b, double amount) {} + //withdraws from account a and deposits to account b + + 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 index 035ee90b..faeedaff 100644 --- a/src/main/java/com/abc/DateProvider.java +++ b/src/main/java/com/abc/DateProvider.java @@ -15,4 +15,12 @@ public static DateProvider getInstance() { public Date now() { return Calendar.getInstance().getTime(); } + + public Date addDays(Date date, int noOfDays) { + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + //manipulate date + cal.add(Calendar.DATE, noOfDays); + return cal.getTime(); + } } diff --git a/src/main/java/com/abc/Enums/AccountType.java b/src/main/java/com/abc/Enums/AccountType.java new file mode 100644 index 00000000..99a2756d --- /dev/null +++ b/src/main/java/com/abc/Enums/AccountType.java @@ -0,0 +1,7 @@ +package com.abc.Enums; + +public enum AccountType { + CHECKING, + SAVINGS, + MAXI_SAVINGS +} diff --git a/src/main/java/com/abc/Enums/TransactionType.java b/src/main/java/com/abc/Enums/TransactionType.java new file mode 100644 index 00000000..3ed254d4 --- /dev/null +++ b/src/main/java/com/abc/Enums/TransactionType.java @@ -0,0 +1,7 @@ +package com.abc.Enums; + +public enum TransactionType { + WITHDRAW, + DEPOSIT, + FUND +} diff --git a/src/main/java/com/abc/Transaction.java b/src/main/java/com/abc/Transaction.java index c1f7c67e..40728f33 100644 --- a/src/main/java/com/abc/Transaction.java +++ b/src/main/java/com/abc/Transaction.java @@ -1,16 +1,33 @@ package com.abc; -import java.util.Calendar; +// import java.util.Calendar; import java.util.Date; +import com.abc.Enums.TransactionType; + public class Transaction { public final double amount; - private Date transactionDate; - - public Transaction(double amount) { - this.amount = amount; + private TransactionType transactionType; + public Transaction(double amount, TransactionType transactionType) { + if (transactionType==TransactionType.WITHDRAW) { + this.amount = -amount; + } + else this.amount = amount; this.transactionDate = DateProvider.getInstance().now(); + this.transactionType = transactionType; + } + public double getAmount() { + return amount; + } + public Date getTransactionDate() { + return transactionDate; + } + public void setTransactionDate() { + this.transactionDate = DateProvider.getInstance().addDays(transactionDate, -20); + } + public TransactionType getTransactionType(){ + return transactionType; } } diff --git a/src/main/java/com/abc/test.java b/src/main/java/com/abc/test.java new file mode 100644 index 00000000..4199125e --- /dev/null +++ b/src/main/java/com/abc/test.java @@ -0,0 +1,18 @@ +package com.abc; + +import java.util.Calendar; +import java.util.Date; + +public class test { + public static void main(String[] args) { + Date currentDate = new Date(); + Calendar cal = Calendar.getInstance(); + + cal.setTime(currentDate); + System.out.println(cal.getTime()); + //manipulate date + cal.add(Calendar.DATE, -10); + + System.out.println(3000 * (Math.pow(1 + (0.05/365), 365) - 1)); + } +} diff --git a/src/test/java/com/abc/BankTest.java b/src/test/java/com/abc/BankTest.java index f8a82896..73b758f9 100644 --- a/src/test/java/com/abc/BankTest.java +++ b/src/test/java/com/abc/BankTest.java @@ -3,6 +3,8 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; +import com.abc.Enums.AccountType; +import com.abc.Enums.TransactionType; public class BankTest { private static final double DOUBLE_DELTA = 1e-15; @@ -11,8 +13,8 @@ public class BankTest { public void customerSummary() { Bank bank = new Bank(); Customer john = new Customer("John"); - john.openAccount(new Account(Account.CHECKING)); bank.addCustomer(john); + john.openAccount(new Account(AccountType.CHECKING)); assertEquals("Customer Summary\n - John (1 account)", bank.customerSummary()); } @@ -20,35 +22,34 @@ public void customerSummary() { @Test public void checkingAccount() { Bank bank = new Bank(); - Account checkingAccount = new Account(Account.CHECKING); + Account checkingAccount = new Account(AccountType.CHECKING); Customer bill = new Customer("Bill").openAccount(checkingAccount); bank.addCustomer(bill); - checkingAccount.deposit(100.0); - - assertEquals(0.1, bank.totalInterestPaid(), DOUBLE_DELTA); + checkingAccount.deposit(100.0, TransactionType.DEPOSIT); + assertEquals(0.0, bank.totalInterestPaid(), DOUBLE_DELTA); } @Test - public void savings_account() { + public void savingsAccount() { Bank bank = new Bank(); - Account checkingAccount = new Account(Account.SAVINGS); + Account checkingAccount = new Account(AccountType.SAVINGS); bank.addCustomer(new Customer("Bill").openAccount(checkingAccount)); - checkingAccount.deposit(1500.0); + checkingAccount.deposit(100.0, TransactionType.DEPOSIT); - assertEquals(2.0, bank.totalInterestPaid(), DOUBLE_DELTA); + assertEquals(0.0, bank.totalInterestPaid(), DOUBLE_DELTA); } @Test public void maxi_savings_account() { Bank bank = new Bank(); - Account checkingAccount = new Account(Account.MAXI_SAVINGS); + Account checkingAccount = new Account(AccountType.MAXI_SAVINGS); bank.addCustomer(new Customer("Bill").openAccount(checkingAccount)); - checkingAccount.deposit(3000.0); + checkingAccount.deposit(3000.0, TransactionType.DEPOSIT); - assertEquals(170.0, bank.totalInterestPaid(), DOUBLE_DELTA); + assertEquals(0.0, bank.totalInterestPaid(), DOUBLE_DELTA); } } diff --git a/src/test/java/com/abc/CustomerTest.java b/src/test/java/com/abc/CustomerTest.java index b8df9498..de26291a 100644 --- a/src/test/java/com/abc/CustomerTest.java +++ b/src/test/java/com/abc/CustomerTest.java @@ -4,20 +4,24 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; - +import com.abc.Enums.AccountType; +import com.abc.Enums.TransactionType; public class CustomerTest { - + private static final double DOUBLE_DELTA = 1e-15; @Test //Test customer statement generation public void testApp(){ - Account checkingAccount = new Account(Account.CHECKING); - Account savingsAccount = new Account(Account.SAVINGS); - + Account checkingAccount = new Account(AccountType.CHECKING); + Account savingsAccount = new Account(AccountType.SAVINGS); + Customer henry = new Customer("Henry").openAccount(checkingAccount).openAccount(savingsAccount); - - checkingAccount.deposit(100.0); - savingsAccount.deposit(4000.0); - savingsAccount.withdraw(200.0); + Double checkingDeposit = 100.0; + Double savingsDeposit =4000.0; + Double savingsWithdraw = 200.0; + checkingAccount.deposit(checkingDeposit, TransactionType.DEPOSIT); + savingsAccount.deposit(savingsDeposit, TransactionType.DEPOSIT); + savingsAccount.withdraw(savingsWithdraw, TransactionType.WITHDRAW); + System.out.println(savingsAccount.getBalance()); assertEquals("Statement for Henry\n" + "\n" + @@ -32,26 +36,37 @@ public void testApp(){ "\n" + "Total In All Accounts $3,900.00", henry.getStatement()); } - + @Test + public void testTransfer(){ + Customer oscar = new Customer("Oscar"); + Account checkingAccount = new Account(AccountType.CHECKING); + Account savingsAccount = new Account(AccountType.SAVINGS); + oscar.openAccount(savingsAccount); + oscar.openAccount(checkingAccount); + savingsAccount.deposit(100, TransactionType.DEPOSIT); + oscar.transferBetween(savingsAccount, checkingAccount, 100); + assertEquals(100,checkingAccount.getBalance(),DOUBLE_DELTA); + } @Test public void testOneAccount(){ - Customer oscar = new Customer("Oscar").openAccount(new Account(Account.SAVINGS)); + Customer oscar = new Customer("Oscar").openAccount(new Account(AccountType.SAVINGS)); assertEquals(1, oscar.getNumberOfAccounts()); } @Test public void testTwoAccount(){ Customer oscar = new Customer("Oscar") - .openAccount(new Account(Account.SAVINGS)); - oscar.openAccount(new Account(Account.CHECKING)); + .openAccount(new Account(AccountType.SAVINGS)); + oscar.openAccount(new Account(AccountType.CHECKING)); assertEquals(2, oscar.getNumberOfAccounts()); } @Ignore - public void testThreeAcounts() { + public void testThreeAccounts() { Customer oscar = new Customer("Oscar") - .openAccount(new Account(Account.SAVINGS)); - oscar.openAccount(new Account(Account.CHECKING)); + .openAccount(new Account(AccountType.SAVINGS)); + oscar.openAccount(new Account(AccountType.CHECKING)); + oscar.openAccount(new Account(AccountType.MAXI_SAVINGS)); assertEquals(3, oscar.getNumberOfAccounts()); } } diff --git a/src/test/java/com/abc/TransactionTest.java b/src/test/java/com/abc/TransactionTest.java index 28983234..ef2defc3 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 com.abc.Enums.TransactionType; + import static org.junit.Assert.assertTrue; public class TransactionTest { @Test public void transaction() { - Transaction t = new Transaction(5); + Transaction t = new Transaction(500.0,TransactionType.DEPOSIT); assertTrue(t instanceof Transaction); } }