Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

my-bank Diogo Carrapato Sousa #202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -20,5 +20,10 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
</project>
37 changes: 26 additions & 11 deletions src/main/java/com/abc/Account.java
Original file line number Diff line number Diff line change
@@ -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);
}
35 changes: 35 additions & 0 deletions src/main/java/com/abc/Customer.java
Original file line number Diff line number Diff line change
@@ -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();
}
7 changes: 7 additions & 0 deletions src/main/java/com/abc/Transaction.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
27 changes: 26 additions & 1 deletion src/test/java/com/abc/BankTest.java
Original file line number Diff line number Diff line change
@@ -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();
}



}
56 changes: 55 additions & 1 deletion src/test/java/com/abc/CustomerTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions src/test/java/com/abc/TransactionTest.java
Original file line number Diff line number Diff line change
@@ -10,4 +10,6 @@ public void transaction() {
Transaction t = new Transaction(5);
assertTrue(t instanceof Transaction);
}


}