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

Sridhu1988 - BOFA test #107

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
72 changes: 61 additions & 11 deletions src/main/java/com/abc/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

public class Account {

Expand All @@ -21,15 +22,21 @@ public void deposit(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("amount must be greater than zero");
} else {
transactions.add(new Transaction(amount));
transactions.add(new Transaction(amount),"DEPOSIT");
}
}

public void withdraw(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("amount must be greater than zero");
} else {
transactions.add(new Transaction(-amount));
}
// Added condition for checking the amount should be lesser than account balance
else if(amount> sumTransactions()){
throw new IllegalArgumentException("The amount you entered is greater than your account balance");
}
else
{
transactions.add(new Transaction(-amount),"WITHDRAW");
}
}

Expand All @@ -41,20 +48,63 @@ public double interestEarned() {
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;
if(isMoneyWithdrawnPastTenDays())
return amount * 0.001;
else
return amount * 0.005;
default:
return amount * 0.001;
}
}

// This method calculates daily rate of interest earned from the accounts

public double interestEarnedDaily() {
double amount = sumTransactions();
double interest;
switch(accountType){
case SAVINGS:
if (amount <= 1000)
interest= (amount * 0.001)/365;
else
interest= (1 + (amount-1000) * 0.002)/365;
case MAXI_SAVINGS:
if(isMoneyWithdrawnPastTenDays())
interest= (amount * 0.001)/365;
else
interest= (amount * 0.005)/365;
default:
interest= (amount * 0.001)/365;
}
Date currDate = newDate();
for(Transaction trans: transactions){
if(!trans.getTransactionDate().equals(currDate)&& trans.getTransactionType().equalsIgnoreCase("INTEREST"))
transactions.add(new Transaction(interest),"INTEREST");
}
return interest;
}

// This method checks if there are any transactions for the past 10 days.
// Returns true if there are any, else returns false

public boolean isMoneyWithdrawnPastTenDays(){

Date date = new Date();
for(Transaction trans : transactions){
if(getDaysBetween(trans.getTransactionDate(), date)<10 && trans.getTransactionType().equalsIgnoreCase("WITHDRAW"){
return true;
}
}
return false;
}

// This method gets the number of days between transactions and current date

public int getDaysBetween(Date transDate, Date currDate){
return daysBetween = (currDate.getTime()- transDate.getTime())/(1000 * 60 * 60 * 24);
}

public double sumTransactions() {
return checkIfTransactionsExist(true);
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/abc/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ public double totalInterestPaid() {

public String getFirstCustomer() {
try {
customers = null;
return customers.get(0).getName();
//Changed the logic behind getting the first customer from the list
if(customers != null && customers.size()!=0){
return customers.get(0).getName();
}
else{
return "Customer List is empty";
}
} catch (Exception e){
e.printStackTrace();
return "Error";
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/abc/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,28 @@ private String statementForAccount(Account a) {
private String toDollars(double d){
return String.format("$%,.2f", abs(d));
}

// This method allows customer to transfer money between their accounts

public boolean transferAmount(Account fromAccount, Account toAccount, double amount){

if (amount <= 0) {
throw new IllegalArgumentException("The amount must be greater than 0");
}
else{

if(accounts.contains(fromAccount)&&accounts.contains(toAccount)){
if (amount > fromAccount.sumTransactions()) {
throw new Exception("No suffecient money in your account");
}
else{
synchronized(this){ // Putting the transactions in synchronized block to avoid deadlocks
fromAccount.withdraw(amount);
toAccount.deposit(amount);
return true;
}
}
return false;
}
}
}
31 changes: 29 additions & 2 deletions src/main/java/com/abc/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,40 @@
import java.util.Date;

public class Transaction {
public final double amount;
private final double amount;

private Date transactionDate;

private String transactionType;

public Transaction(double amount) {
public Transaction(double amount, String transactionType) {
this.amount = amount;
this.transactionDate = DateProvider.getInstance().now();
this.transactionType=transactionType;
}

public double getAmount(){
return amount;
}

public void setAmount(double amount){
this.amount=amount;
}

public Date getTransactionDate(){
return transactionDate;
}

public void setTransactionDate(Date transactionDate){
this.transactionDate=transactionDate;
}

public String getTransactionType(){
return transactionType;
}

public void setTransactionType(String transactionType){
this.transactionType=transactionType;
}

}
55 changes: 55 additions & 0 deletions src/test/java/com/abc/CustomerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,59 @@ public void testThreeAcounts() {
oscar.openAccount(new Account(Account.CHECKING));
assertEquals(3, oscar.getNumberOfAccounts());
}

@Test
public void testForMulSavingsAccounts() {
Customer oscar = new Customer("Oscar").openAccount(new Account(Account.SAVINGS));
oscar.openAccount(new Account(Account.SAVINGS));
assertEquals(2, oscar.getNumberOfAccounts());
}

@Test
public void testForMulCheckingAccounts() {
Customer oscar = new Customer("Oscar").openAccount(new Account(Account.CHECKING));
oscar.openAccount(new Account(Account.CHECKING));
assertEquals(2, oscar.getNumberOfAccounts());
}


@Test
public void testMulMaxiSavingsAccounts() {
Customer oscar = new Customer("Oscar").openAccount(new Account(Account.MAXI_SAVINGS));
oscar.openAccount(new Account(Account.MAXI_SAVINGS));
assertEquals(2, oscar.getNumberOfAccounts());
}

@Test
public void testSavingsInterest() {

Account savingsAccount = new Account(Account.SAVINGS);

Customer henry = new Customer("Henry").openAccount(savingsAccount);

savingsAccount.deposit(5000.0);
assertEquals(9.0, savingsAccount.interestEarned(), Constants.DOUBLE_DELTA);
}

@Test
public void testCheckingsInterest() {

Account checkingAccount = new Account(Account.CHECKING);

Customer henry = new Customer("Henry").openAccount(checkingAccount);

checkingAccount.deposit(5000.0);
assertEquals(5.0, checkingAccount.interestEarned(), Constants.DOUBLE_DELTA);
}

@Test
public void testMaxiSavingsInterest() {

Account maxiSavingsAccount = new Account(Account.MAXI_SAVINGS);

Customer henry = new Customer("Henry").openAccount(maxiSavingsAccount);

maxiSavingsAccount.deposit(5000.00);
assertEquals(250.00, maxiSavingsAccount.interestEarned(), Constants.DOUBLE_DELTA);
}
}