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

code refactoring and features added - Sammya Majumdar #332

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
target/
bin/
*.iml

108 changes: 80 additions & 28 deletions src/main/java/com/abc/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
import java.util.ArrayList;
import java.util.List;

import com.abc.DateUtils.DateChecker;

public class Account {

public static final int CHECKING = 0;
public static final int SAVINGS = 1;
public static final int MAXI_SAVINGS = 2;

private static final double DEFAULT_INTEREST_RATE = 0.001;
private static final double HIGHER_INTEREST_RATE_SAVINGS = 0.002;
private static final double HIGHER_INTEREST_RATE_MAXI = 0.05;

private final int accountType;
public List<Transaction> transactions;
DateChecker dateCheck = new DateChecker();

public Account(int accountType) {
this.accountType = accountType;
this.transactions = new ArrayList<Transaction>();

}

/**
* @param amount
* deposits money into account
*/
public void deposit(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("amount must be greater than zero");
Expand All @@ -25,49 +37,89 @@ 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));
/**
* @param amount
* withdraws money from account
*/
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();
switch(accountType){
case SAVINGS:
if (amount <= 1000)
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;
default:
return amount * 0.001;
/**
*
* @return interest earned in checking account
*/
public double interestEarnedChecking() {

double amount = getAccountBalance();
return amount * DEFAULT_INTEREST_RATE;

}

/**
*
* @return interest earned in savings account
*/
public double interestEarnedSavings() {
double amount = getAccountBalance();
if (amount <= 1000)
return amount * DEFAULT_INTEREST_RATE;
else
return 1 + (amount - 1000) * HIGHER_INTEREST_RATE_SAVINGS;
}

/**
*
* @return interest earned in maxi_savings_account
*/
public double interestEarnedMaxiSavings() {
double amount = getAccountBalance();
boolean val = dateCheck.hasTransactionsWithinLastTenDays(transactions);
if (val) {
return amount * DEFAULT_INTEREST_RATE;
} else {
return amount * HIGHER_INTEREST_RATE_MAXI;
}
}

public double sumTransactions() {
return checkIfTransactionsExist(true);
/**
*
* @return account balance
*/
public double getAccountBalance() {
return checkIfTransactionsExist(true);
}

/**
*
* @param checkAll
* @return total amount
*/
private double checkIfTransactionsExist(boolean checkAll) {
double amount = 0.0;
for (Transaction t: transactions)
for (Transaction t : transactions)
amount += t.amount;
return amount;
}

/**
*
* @return accountType (int): 0, 1, 2
*/
public int getAccountType() {
return accountType;
}

/**
*
* @return list of transactions
*/
public List<Transaction> getTransactionList() {
return transactions;
}

}
35 changes: 30 additions & 5 deletions src/main/java/com/abc/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,60 @@ public Bank() {
customers = new ArrayList<Customer>();
}

/**
* @param customer
*/
public void addCustomer(Customer customer) {
customers.add(customer);
}

/**
*
* @return customer summary
*/

public String customerSummary() {
String summary = "Customer Summary";
for (Customer c : customers)
summary += "\n - " + c.getName() + " (" + format(c.getNumberOfAccounts(), "account") + ")";
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
/**
*
* @param number
* @param word
* @return plural of word
* 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");
}

/**
*
* @return amount
* total interest paid to all customers across accounts
*/
public double totalInterestPaid() {
double total = 0;
for(Customer c: customers)
for (Customer c : customers)
total += c.totalInterestEarned();
return total;
}

/**
*
* @return name of first customer of the bank
*/

public String getFirstCustomer() {
try {
customers = null;
return customers.get(0).getName();
} catch (Exception e){
} catch (Exception e) {
e.printStackTrace();
return "Error";
}
Expand Down
51 changes: 45 additions & 6 deletions src/main/java/com/abc/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,76 @@ public Customer(String name) {
this.accounts = new ArrayList<Account>();
}

/**
* @return String customer name
*/
public String getName() {
return name;
}

/**
*
* @param account
* @return account object
*/
public Customer openAccount(Account account) {
accounts.add(account);
return this;
}

/**
*
* @return number of accounts (int)
*/
public int getNumberOfAccounts() {
return accounts.size();
}

/**
*
* @return total interest
*/

public double totalInterestEarned() {
double total = 0;
for (Account a : accounts)
total += a.interestEarned();
if (a.getAccountType() == 0)
total += a.interestEarnedChecking();
else if (a.getAccountType() == 1)
total += a.interestEarnedSavings();
else
total += a.interestEarnedMaxiSavings();
return total;
}

/**
*
* @return statement across accounts (String)
*/

public String getStatement() {
String statement = null;
statement = "Statement for " + name + "\n";
double total = 0.0;
for (Account a : accounts) {
statement += "\n" + statementForAccount(a) + "\n";
total += a.sumTransactions();
total += a.getAccountBalance();
}
statement += "\nTotal In All Accounts " + toDollars(total);
return statement;
}

/**
*
* @param account
* @return statement for individual account (String)
*/

private String statementForAccount(Account a) {
String s = "";

//Translate to pretty account type
switch(a.getAccountType()){
// Translate to pretty account type
switch (a.getAccountType()) {
case Account.CHECKING:
s += "Checking Account\n";
break;
Expand All @@ -62,7 +95,7 @@ private String statementForAccount(Account a) {
break;
}

//Now total up all the transactions
// 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";
Expand All @@ -72,7 +105,13 @@ private String statementForAccount(Account a) {
return s;
}

private String toDollars(double d){
/**
*
* @param amount
* @return formatted String to dollars
*/
private String toDollars(double d) {
return String.format("$%,.2f", abs(d));
}

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

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

import com.abc.Transaction;

public class DateChecker {

/**
* @param transactionDate
* @return boolean if given date is in the last 10 days
*/
public boolean isWithinLast10Days(Date transactionDate) {

Date today = new Date();
long differenceInMillis = today.getTime() - transactionDate.getTime();
long tenDaysInMillis = 10 * 24 * 60 * 60 * 1000;

return differenceInMillis <= tenDaysInMillis;
}

/**
* @param transactions
* @return boolean: true if there is a withdrawal in the last 10 days
*/

public boolean hasTransactionsWithinLastTenDays(List<Transaction> transactions) {
Date today = new Date();

for (Transaction transaction : transactions) {
Date transactionDate = transaction.getTransactionDate();
if (isWithinLast10Days(transactionDate) && transaction.amount <= 0) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package com.abc;
package com.abc.DateUtils;

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

public class DateProvider {
private static DateProvider instance = null;

/**
* @return DateProvider instance
*/
public static DateProvider getInstance() {
if (instance == null)
instance = new DateProvider();
return instance;
}

/**
*
* @return current time (Date)
*/
public Date now() {
return Calendar.getInstance().getTime();
}

}
Loading