From f712d895091aaf74eb731ded696d98e053c46367 Mon Sep 17 00:00:00 2001 From: sousadmc Date: Sun, 5 May 2019 22:49:12 +0100 Subject: [PATCH] my-bank Diogo Carrapato Sousa --- pom.xml | 5 ++ src/main/java/com/abc/Account.java | 37 +++++++++----- src/main/java/com/abc/Customer.java | 35 ++++++++++++++ src/main/java/com/abc/Transaction.java | 7 +++ src/test/java/com/abc/BankTest.java | 27 ++++++++++- src/test/java/com/abc/CustomerTest.java | 56 +++++++++++++++++++++- src/test/java/com/abc/TransactionTest.java | 2 + 7 files changed, 156 insertions(+), 13 deletions(-) diff --git a/pom.xml b/pom.xml index 3994d594..b2db2baa 100644 --- a/pom.xml +++ b/pom.xml @@ -20,5 +20,10 @@ 4.11 test + + joda-time + joda-time + 2.10.1 + diff --git a/src/main/java/com/abc/Account.java b/src/main/java/com/abc/Account.java index 099691e0..9c22878a 100644 --- a/src/main/java/com/abc/Account.java +++ b/src/main/java/com/abc/Account.java @@ -1,5 +1,7 @@ package com.abc; +import org.joda.time.DateTimeUtils; + import java.util.ArrayList; import java.util.List; @@ -25,13 +27,13 @@ public void deposit(double amount) { } } -public void withdraw(double amount) { - if (amount <= 0) { - throw new IllegalArgumentException("amount must be greater than zero"); - } else { - transactions.add(new Transaction(-amount)); + public void withdraw(double amount) { + if (amount <= 0) { + throw new IllegalArgumentException("amount must be greater than zero"); + } else { + transactions.add(new Transaction(-amount)); + } } -} public double interestEarned() { double amount = sumTransactions(); @@ -45,16 +47,29 @@ public double interestEarned() { // 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; + if(lastTransaction()) + return amount * 0.05; + + else + return amount * 0.001; + default: return amount * 0.001; } } + + public boolean lastTransaction(){ + long last = transactions.get(0).getTransactionDate().getTime(); + long now = DateTimeUtils.currentTimeMillis(); + long days = (now - last) / (24 * 60 * 60 * 1000); + if(days >= 10){ + return true; + } + else + return false; + } + public double sumTransactions() { return checkIfTransactionsExist(true); } diff --git a/src/main/java/com/abc/Customer.java b/src/main/java/com/abc/Customer.java index 31571685..ef87335f 100644 --- a/src/main/java/com/abc/Customer.java +++ b/src/main/java/com/abc/Customer.java @@ -23,6 +23,41 @@ public Customer openAccount(Account account) { return this; } + //Transfering amount between two accounts of the customer + public void transfer (Account from, Account to, int amount){ + + int fromExists = 0, toExists = 0; + + for (Account account : accounts){ + if (account == from){ + fromExists = 1; + account.withdraw(amount); + } + if (account == to){ + toExists = 1; + account.deposit(amount); + } + } + + if (fromExists == 0){ + for (Account account : accounts){ + if (account == to){ + account.withdraw(amount); + } + } + throw new IllegalArgumentException("Source account does not exist"); + } + + if (toExists == 0){ + for (Account account : accounts){ + if (account == from){ + account.deposit(amount); + } + } + throw new IllegalArgumentException("Destination account does not exist"); + } + } + public int getNumberOfAccounts() { return accounts.size(); } diff --git a/src/main/java/com/abc/Transaction.java b/src/main/java/com/abc/Transaction.java index c1f7c67e..0b3d9902 100644 --- a/src/main/java/com/abc/Transaction.java +++ b/src/main/java/com/abc/Transaction.java @@ -13,4 +13,11 @@ public Transaction(double amount) { this.transactionDate = DateProvider.getInstance().now(); } + public Date getTransactionDate() { + return this.transactionDate; + } + + public double getAmount() { + return this.amount; + } } diff --git a/src/test/java/com/abc/BankTest.java b/src/test/java/com/abc/BankTest.java index f8a82896..cdf4a8ed 100644 --- a/src/test/java/com/abc/BankTest.java +++ b/src/test/java/com/abc/BankTest.java @@ -1,12 +1,17 @@ package com.abc; +import org.joda.time.DateTime; +import org.joda.time.DateTimeUtils; 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(); @@ -46,9 +51,29 @@ public void maxi_savings_account() { Account checkingAccount = new Account(Account.MAXI_SAVINGS); bank.addCustomer(new Customer("Bill").openAccount(checkingAccount)); + checkingAccount.deposit(3000.0); - assertEquals(170.0, bank.totalInterestPaid(), DOUBLE_DELTA); + + assertEquals(3.0, bank.totalInterestPaid(), DOUBLE_DELTA); + } + @Test + public void maxi_savings_account_10days() { + Bank bank = new Bank(); + Account checkingAccount = new Account(Account.MAXI_SAVINGS); + bank.addCustomer(new Customer("Bill").openAccount(checkingAccount)); + + + checkingAccount.deposit(3000.0); + DateTimeUtils.setCurrentMillisFixed(DateTime.now().plusDays(10).getMillis()); + + + assertEquals(150.0, bank.totalInterestPaid(), DOUBLE_DELTA); + DateTimeUtils.setCurrentMillisSystem(); + } + + + } diff --git a/src/test/java/com/abc/CustomerTest.java b/src/test/java/com/abc/CustomerTest.java index b8df9498..4e72955f 100644 --- a/src/test/java/com/abc/CustomerTest.java +++ b/src/test/java/com/abc/CustomerTest.java @@ -7,6 +7,8 @@ public class CustomerTest { + private static final double DOUBLE_DELTA = 1e-15; + @Test //Test customer statement generation public void testApp(){ @@ -47,11 +49,63 @@ public void testTwoAccount(){ assertEquals(2, oscar.getNumberOfAccounts()); } - @Ignore + @Test public void testThreeAcounts() { Customer oscar = new Customer("Oscar") .openAccount(new Account(Account.SAVINGS)); oscar.openAccount(new Account(Account.CHECKING)); + oscar.openAccount(new Account(Account.MAXI_SAVINGS)); assertEquals(3, oscar.getNumberOfAccounts()); } + + + @Test + public void transferSuccess(){ + Customer oscar = new Customer("Oscar"); + Account saving = new Account(Account.SAVINGS); + saving.deposit(1000); + oscar.openAccount(saving); + + + Account checking = new Account(Account.CHECKING); + oscar.openAccount(checking); + + oscar.transfer(saving,checking,500); + + assertEquals(500, saving.sumTransactions(), DOUBLE_DELTA); + assertEquals(500, checking.sumTransactions(), DOUBLE_DELTA); + } + + @Test(expected = IllegalArgumentException.class) + public void transferInsuccessAccount(){ + Customer oscar = new Customer("Oscar"); + Account saving = new Account(Account.SAVINGS); + saving.deposit(1000); + oscar.openAccount(saving); + + + Account checking = new Account(Account.CHECKING); + + oscar.transfer(saving,checking,500); + + assertEquals(500, saving.sumTransactions(), DOUBLE_DELTA); + assertEquals(500, checking.sumTransactions(), DOUBLE_DELTA); + } + + @Test(expected = IllegalArgumentException.class) + public void transferInsuccessAmount(){ + Customer oscar = new Customer("Oscar"); + Account saving = new Account(Account.SAVINGS); + saving.deposit(1000); + oscar.openAccount(saving); + + + Account checking = new Account(Account.CHECKING); + oscar.openAccount(checking); + + oscar.transfer(saving,checking,-500); + + assertEquals(500, saving.sumTransactions(), DOUBLE_DELTA); + assertEquals(500, checking.sumTransactions(), DOUBLE_DELTA); + } } diff --git a/src/test/java/com/abc/TransactionTest.java b/src/test/java/com/abc/TransactionTest.java index 28983234..83289ebc 100644 --- a/src/test/java/com/abc/TransactionTest.java +++ b/src/test/java/com/abc/TransactionTest.java @@ -10,4 +10,6 @@ public void transaction() { Transaction t = new Transaction(5); assertTrue(t instanceof Transaction); } + + }