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

abc-bank: Submitted By Apurva #108

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
135 changes: 103 additions & 32 deletions src/main/java/com/abc/Account.java
Original file line number Diff line number Diff line change
@@ -1,73 +1,144 @@
package com.abc;

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

import com.abc.Transaction.TransactionType;
import com.abc.Utils.DateProvider;
import com.abc.exception.AccountOperationException;

public class Account {

public static final int CHECKING = 0;
public static final int SAVINGS = 1;
public static final int MAXI_SAVINGS = 2;
/*enum added to remove hard coding of the account types with numbers 0,1,2
*/
enum AccountType{
CHECKING,SAVINGS,MAXI_SAVINGS;
}


private final AccountType accountType;
private List<Transaction> transactions;
private Date lastPaymentDate;

public void setLastPaymentDate(Date lastPaymentDate) {
this.lastPaymentDate = lastPaymentDate;
}

public Date getLastPaymentDate() {
return lastPaymentDate;
}

private final int accountType;
public List<Transaction> transactions;
public List<Transaction> getTransactions() {
return transactions;
}

public Account(int accountType) {
public Account(AccountType accountType) {
this.accountType = accountType;
this.transactions = new ArrayList<Transaction>();
//Setting up the last payment date to account opening day.
this.lastPaymentDate=DateProvider.getInstance().now();
}

public void deposit(double amount) {
public void deposit(double amount) throws AccountOperationException {
if (amount <= 0) {
throw new IllegalArgumentException("amount must be greater than zero");
throw new AccountOperationException("amount must be greater than zero");
} else {
transactions.add(new Transaction(amount));
transactions.add(new Transaction(amount,TransactionType.DEPOSIT));
}
}

public void withdraw(double amount) {
public void withdraw(double amount) throws AccountOperationException {

if (amount <= 0) {
throw new IllegalArgumentException("amount must be greater than zero");
} else {
transactions.add(new Transaction(-amount));
throw new AccountOperationException("amount must be greater than zero");
}
else if(amount>sumTransactions()){
throw new AccountOperationException("amount exceeds available balance");
}
else {
transactions.add(new Transaction(-amount,TransactionType.WITHDRAW));
}
}

public double interestEarned() {
/*This method calculates the daily interest including weekends
* Formula - Principal Balance X (Annual Interest Rate* / Year Count**) X Number of Days Since Last Payment
*/
public double dailyInterestEarned() {
double amount = sumTransactions();
int noOfDaysSinceLastPayement=(int)((DateProvider.getInstance().now().getTime() - getLastPaymentDate().getTime())
/ (1000 * 60 * 60 * 24) );

switch(accountType){
case SAVINGS:
if (amount <= 1000)
return amount * 0.001;
return amount * (0.001/365) * noOfDaysSinceLastPayement;
else
return 1 + (amount-1000) * 0.002;
// case SUPER_SAVINGS:
// if (amount <= 4000)
// return 20;
return amount * (0.002/365) * noOfDaysSinceLastPayement;
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;
Transaction lastWithdrawTransaction=getLastWithdrawTransaction();
if(lastWithdrawTransaction==null)
return amount * (0.05/365) * noOfDaysSinceLastPayement;

int lastTransactionDay=(int)((DateProvider.getInstance().now().getTime() - lastWithdrawTransaction.getTransactionDate().getTime())
/ (1000 * 60 * 60 * 24) );

if(lastTransactionDay>10)
return amount * (0.05/365) * noOfDaysSinceLastPayement;
else
return amount * (0.001/365) * noOfDaysSinceLastPayement;

default:
return amount * 0.001;
return amount * (0.001/365) * noOfDaysSinceLastPayement;
}
}

public double sumTransactions() {
return checkIfTransactionsExist(true);
}
private Transaction getLastWithdrawTransaction() {
Transaction lastTransaction= null;

int lastIndex=transactions.size()-1;
lastTransaction = transactions.get(lastIndex);
while(lastIndex>=0){
if(lastTransaction.getTransactionType()==TransactionType.WITHDRAW)
{
return lastTransaction;
}
else
{
lastIndex--;
}
}
return null;

}

private double checkIfTransactionsExist(boolean checkAll) {
double amount = 0.0;
/*Removing checkIfTransactionsExist separate method call , the call not doing anything additional
* other than just forwarding the call.
* Removing parameter checkAll , its not used*/
public double sumTransactions() {
double amount = 0.0;
for (Transaction t: transactions)
amount += t.amount;
amount += t.getAmount();
return amount;
}

public int getAccountType() {

public AccountType getAccountType() {
return accountType;
}
/*
* This method is added , which will be called during the interest payments for the accounts.
* This will set the lastPaymentDate.
* lastPayment date will be used to calculate the daily interest rates
*/
public boolean payIntrest(double amount,Date date){
transactions.add(new Transaction(amount,TransactionType.DEPOSIT));
if(date==null)
setLastPaymentDate(DateProvider.getInstance().now());
else
setLastPaymentDate(date);
return true;
}


}
24 changes: 24 additions & 0 deletions src/main/java/com/abc/AccountOperationException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.abc.exception;
public class AccountOperationException extends Exception {

public AccountOperationException() {
}

public AccountOperationException(String message) {
super(message);
}

public AccountOperationException(Throwable cause) {
super(cause);
}

public AccountOperationException(String message, Throwable cause) {
super(message, cause);
}

public AccountOperationException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

}
14 changes: 7 additions & 7 deletions src/main/java/com/abc/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ public void addCustomer(Customer customer) {
public String customerSummary() {
String summary = "Customer Summary";
for (Customer c : customers)
summary += "\n - " + c.getName() + " (" + format(c.getNumberOfAccounts(), "account") + ")";
summary += "\n - " +c.toString();
return summary;
}

//Make sure correct plural of word is created based on the number passed in:
//If number passed in is 1 just return the word otherwise add an 's' at the end
private String format(int number, String word) {
return number + " " + (number == 1 ? word : word + "s");
}


public double totalInterestPaid() {
double total = 0;
for(Customer c: customers)
total += c.totalInterestEarned();

total=Math.floor(total * 100) / 100;
return total;
}

/*customers = null;<- This statement will cause null pointer, hence removed*/
public String getFirstCustomer() {
try {
customers = null;
return customers.get(0).getName();
} catch (Exception e){
e.printStackTrace();
return "Error";
}
}


}
40 changes: 33 additions & 7 deletions src/main/java/com/abc/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.util.ArrayList;
import java.util.List;

import com.abc.Utils.Utils;
import com.abc.exception.AccountOperationException;

import static java.lang.Math.abs;

public class Customer {
Expand Down Expand Up @@ -30,7 +33,7 @@ public int getNumberOfAccounts() {
public double totalInterestEarned() {
double total = 0;
for (Account a : accounts)
total += a.interestEarned();
total += a.dailyInterestEarned();
return total;
}

Expand All @@ -46,27 +49,28 @@ public String getStatement() {
return statement;
}

/*This logic has been modified to make use of enum constants*/
private String statementForAccount(Account a) {
String s = "";

//Translate to pretty account type
switch(a.getAccountType()){
case Account.CHECKING:
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";
total += t.amount;
for (Transaction t : a.getTransactions()) {
s += " " + (t.getAmount() < 0 ? "withdrawal" : "deposit") + " " + toDollars(t.getAmount()) + "\n";
total += t.getAmount();
}
s += "Total " + toDollars(total);
return s;
Expand All @@ -75,4 +79,26 @@ private String statementForAccount(Account a) {
private String toDollars(double d){
return String.format("$%,.2f", abs(d));
}

public boolean transfer(Account source,Account destination,double amount) throws AccountOperationException{

if(accounts.contains(source) && accounts.contains(destination)){
synchronized(source){
synchronized (destination) {
source.withdraw(amount);
destination.deposit(amount);
}
}
return true;
}
else{
throw new AccountOperationException("Sorry, money transfer is not allowed between these accounts.");
}
}


public String toString(){
return getName() + " (" + Utils.format(getNumberOfAccounts(), "account") + ")";

}
}
2 changes: 1 addition & 1 deletion src/main/java/com/abc/DateProvider.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.abc;
package com.abc.Utils;

import java.util.Calendar;
import java.util.Date;
Expand Down
29 changes: 26 additions & 3 deletions src/main/java/com/abc/Transaction.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
package com.abc;

import java.util.Calendar;
import java.util.Date;

import com.abc.Utils.DateProvider;

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



enum TransactionType{
DEPOSIT,WITHDRAW
}

private Date transactionDate;
private TransactionType transactionType;

public Transaction(double amount) {
public Date getTransactionDate() {
return transactionDate;
}

public double getAmount() {
return amount;
}

public TransactionType getTransactionType() {
return transactionType;
}

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



}
12 changes: 12 additions & 0 deletions src/main/java/com/abc/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.abc.Utils;

public class Utils {
public static final double DOUBLE_DELTA = 1e-5;

//Make sure correct plural of word is created based on the number passed in:
//If number passed in is 1 just return the word otherwise add an 's' at the end
public static String format(int number, String word) {
return number + " " + (number == 1 ? word : word + "s");
}
}

Loading