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

Version_1 #78

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
118 changes: 52 additions & 66 deletions src/main/java/com/abc/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,57 @@
import java.util.ArrayList;
import java.util.List;

public class Account {

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

private final int accountType;
public List<Transaction> transactions;

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

public void deposit(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();
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;
}
}

public double sumTransactions() {
return checkIfTransactionsExist(true);
}

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

public int getAccountType() {
return accountType;
}
public abstract class Account implements AccountIntf{

private final int accountType;
public List<Transaction> transactions;

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

public void deposit(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 if(sumTransactions() < amount) { //Need to check the Balance
throw new IllegalArgumentException(
"Insufficient funds" );
}
else {
transactions.add(new Transaction(-amount));
}
}

public abstract double interestEarned() ;

public double sumTransactions() {
double amount = 0.0;
for (Transaction t : transactions)
amount += t.amount;
return amount;
// return checkIfTransactionsExist(true); Not Needed
}

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

public int getAccountType() {
return accountType;
}

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

package com.abc;

public class AccountFactory {
public static Account create(int accountType) {
Account ac = null;
switch (accountType) {
case AccountTypes.CHECKING:
ac = new CheckingAccount(accountType);
break;
case AccountTypes.SAVINGS:
ac = new SavingsAccount(accountType);
break;
case AccountTypes.MAXI_SAVINGS:
ac = new MaxiSavingsAccount(accountType);
break;
default:
throw new IllegalArgumentException(
"Don't know how to create account of type: " +accountType);
}
return ac;
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/abc/AccountIntf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.abc;

public interface AccountIntf {
public void deposit(double amount);

public void withdraw(double amount);

public double interestEarned();

public double sumTransactions();

public int getAccountType();

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

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

public static String getAccountType(int accountType) {
switch (accountType) {
case CHECKING:
return "Checking Account";
case SAVINGS:
return "Savings Account";
case MAXI_SAVINGS:
return "Maxi Savings Account";
default:
throw new IllegalArgumentException("Unknow Account Type "+accountType);
}
}
}
12 changes: 6 additions & 6 deletions src/main/java/com/abc/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ public void addCustomer(Customer customer) {
}

public String customerSummary() {
String summary = "Customer Summary";
StringBuilder summary = new StringBuilder("Customer Summary");
for (Customer c : customers)
summary += "\n - " + c.getName() + " (" + format(c.getNumberOfAccounts(), "account") + ")";
return summary;
summary.append("\n - " + c.getName() + " (" + format(c.getNumberOfAccounts(), "account") + ")");
return summary.toString();
}

//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 (number == 1 ? number + " " +word : number + " " +word + "s");
}

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

public String getFirstCustomer() {
try {
customers = null;
// customers = null; Need to remove as it throws NPE
return customers.get(0).getName();
} catch (Exception e){
e.printStackTrace();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/abc/CheckingAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.abc;

public class CheckingAccount extends Account{

public CheckingAccount(int accountType) {
super(accountType);
}

public double interestEarned() {
double amount = sumTransactions();
return amount * 0.001;
}
}
39 changes: 16 additions & 23 deletions src/main/java/com/abc/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,35 @@ public double totalInterestEarned() {
return total;
}

/*
* Change String to StringBuilder
*/
public String getStatement() {
String statement = null;
statement = "Statement for " + name + "\n";
StringBuilder statement=new StringBuilder();
statement.append( "Statement for " + name + "\n");
double total = 0.0;
for (Account a : accounts) {
statement += "\n" + statementForAccount(a) + "\n";
statement.append( "\n" + statementForAccount(a) + "\n");
total += a.sumTransactions();
}
statement += "\nTotal In All Accounts " + toDollars(total);
return statement;
statement.append( "\nTotal In All Accounts " + toDollars(total));
return statement.toString();
}

/*
* Move the AccountType Conversion to a Different class AccountTypes
*/
private String statementForAccount(Account a) {
String s = "";

//Translate to pretty account type
switch(a.getAccountType()){
case Account.CHECKING:
s += "Checking Account\n";
break;
case Account.SAVINGS:
s += "Savings Account\n";
break;
case Account.MAXI_SAVINGS:
s += "Maxi Savings Account\n";
break;
}

StringBuilder s=new StringBuilder();
s.append(AccountTypes.getAccountType(a.getAccountType()));
//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";
s.append( " " + (t.amount < 0 ? "withdrawal" : "deposit") + " " + toDollars(t.amount) + "\n");
total += t.amount;
}
s += "Total " + toDollars(total);
return s;
s.append( "Total " + toDollars(total));
return s.toString();
}

private String toDollars(double d){
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/com/abc/DateProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@
import java.util.Calendar;
import java.util.Date;

/*
* Change this to SingleTon as we need to keep only 1 instance
*/
public class DateProvider {
private static DateProvider instance = null;


private DateProvider()
{
}
public static DateProvider getInstance() {
if (instance == null)
instance = new DateProvider();

if (instance != null) {
return instance;
}
synchronized (DateProvider.class) {
if(instance == null)
instance = new DateProvider();
}
return instance;
}

public Date now() {
return Calendar.getInstance().getTime();
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/abc/MaxiSavingsAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.abc;

public class MaxiSavingsAccount extends Account{

public MaxiSavingsAccount(int accountType) {
super(accountType);
}

@Override
public double interestEarned() {
double amount = sumTransactions();
if (amount <= 1000)
return amount * 0.02;
if (amount <= 2000)
return (1000 * 0.02) + ((amount-1000) * 0.05);

return (1000 * 0.02) + (1000 * 0.05)+(amount-2000) * 0.1;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/abc/SavingsAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.abc;

public class SavingsAccount extends Account {
public SavingsAccount(int accountType) {
super(accountType);
}

@Override
public double interestEarned() {
double amount = sumTransactions();
if (amount <= 1000) {
return amount * 0.001;
} else {
return (1000*0.001)+(amount - 1000) * 0.002;
}
}

}
17 changes: 14 additions & 3 deletions src/main/java/com/abc/Transaction.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
package com.abc;

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

public class Transaction {
public final double amount;

private Date transactionDate;
public Date getTransactionDate() {
return transactionDate;
}

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

public double getAmount() {
return amount;
}

private Date transactionDate;

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

}
Loading