Skip to content

Instantly share code, notes, and snippets.

Created October 22, 2012 16:56
Show Gist options
  • Save anonymous/3932550 to your computer and use it in GitHub Desktop.
Save anonymous/3932550 to your computer and use it in GitHub Desktop.
Bank.java
package bank;
import java.util.ArrayList;
/**
This bank contains a collection of bank accounts.
*/
public class Bank
{
/**
Constructs a bank with no bank accounts.
*/
public Bank() {
accounts = new ArrayList<BankAccount>();
}
/**
Adds an account to this bank.
@param anAccountNumber is the new account's unique number
@param initialDeposit is the initial funds put in the bank account
*/
public void addAccount(int anAccountNumber, double initialDeposit) {
accounts.add(new BankAccount(anAccountNumber));
}
/**
Bank uses this to make deposits.
@param anAccountNumber is the account number of account
@param amount is the amount to deposit
*/
public void deposit(int anAccountNumber, double amount) {
BankAccount.deposit(amount);
}
/**
Withdraws money from the bank account.
@param anAccountNumber is the account number of account
@param amount the amount to withdraw
*/
public void withdraw(int anAccountNumber, double amount) {
}
/**
Gets the sum of the balances of all accounts in this bank.
@param anAccountNumber is the number of the account
@return the sum of the balances
*/
public double getBalance(int anAccountNumber) {
double total = 0;
for (BankAccount account : accounts)
{
total = total + account.getBalance();
}
return total;
}
/**
* Changes a given bank account's status to suspended.
* @param anAccountNumber is the account number
*/
public void suspendAccount(int anAccountNumber) {
}
/**
* Changes a given bank account's status to open.
* @param anAccountNumber is the account number
*/
public void openAccount(int anAccountNumber) {
}
/**
Changes a given bank account's status to closed.
@param anAccountNumber is the account number
*/
public void closeAccount(int anAccountNumber) {
}
public String getAccountStatus(int anAccountNumber) {
String accountStatus = getAccountStatus(anAccountNumber);
return accountStatus;
}
public String accountTransactionsString(int anAccountNumber) {
String accountTransactions = accountTransactionsString(anAccountNumber);
return accountTransactions;
}
/**
Printout string of account summary
*/
public String summarizeAllAccounts(){
String acctSum = "Bank Account Summary \n \n" + "Account \t \t Balance \t #Transactions \t \t Status \n";
for (BankAccount account : accounts)
{
acctSum += account.transactionsString() + "\t\t " + account.getBalance() + "\t\t\t " + account.numTransactions() + "\t\t\t\t" + account.getStatus() + "\n";
}
return acctSum;
}
/**
Finds a bank account with a given number.
@param anAccountNumber the number to find
@return the account with the given number, or null if there
is no such account
*/
public BankAccount find(int anAccountNumber)
{
for (BankAccount account : accounts)
{
if (account.getAccountNumber() == anAccountNumber) // Found a match
return account;
}
return null; // No match in the entire array list
}
private ArrayList<BankAccount> accounts;
}
package bank;
import java.util.ArrayList;
/**
* A bank account has a balance that can be changed by
* deposits and withdrawals.
*/
public class BankAccount
{
public static final String OPEN_STATE = "open";
public static final String SUSPENDED_STATE = "suspended";
public static final String CLOSED_STATE = "closed";
public static String status = "";
static ArrayList<Double> accountTransactions;
private static int accountNumber;
private static double balance;
/**
* Constructs a bank account with a zero balance
* @param anAccountNumber the account number for this account
*/
public BankAccount(int anAccountNumber)
{
BankAccount.setStatus(OPEN_STATE);
accountTransactions = new ArrayList<Double>();
accountNumber = anAccountNumber;
balance = 0;
}
/**
* Constructs a bank account with a given balance
* @param anAccountNumber the account number for this account
* @param initialBalance the initial balance
*/
public BankAccount(int anAccountNumber, double initialBalance)
{
BankAccount.setStatus(OPEN_STATE);
accountTransactions = new ArrayList<Double>();
accountNumber = anAccountNumber;
balance = initialBalance;
BankAccount.deposit(balance);
}
/**
* Gets the account number of a bank account.
* @return the account number
*/
public int getAccountNumber()
{
return accountNumber;
}
/**
* Returns the number of transactions in the list.
* @return the number of transactions
*/
public int numTransactions()
{
return accountTransactions.size() - 1;
}
/**
* Returns a String of bank account's transactions.
* @return the String of bank account transactions
*/
public String transactionsString()
{
String header = "Account #" + getAccountNumber() + " " + "transactions:" + "\n";
System.out.println(header);
String footer = "End of transactions";
String balanceStr = "balance = " + getBalance();
int counter = 0;
int index = 0;
for (Double transaction : accountTransactions)
{
counter++;
counter = counter++;
String str = counter + ": " + accountTransactions.get(index);
System.out.println(str);
index++;
index = index++;
}
return balanceStr + "\n" + footer;
}
/**
* Makes deposits to a bank account.
* @param amount the amount to deposit
*/
public static void deposit(double amount)
{
if (BankAccount.isOpen() && amount > 0)
{
double newBalance = balance + amount;
balance = newBalance;
accountTransactions.add(amount);
}
}
/**
* If legal state change, updates a bank account's status to open, otherwise does nothing.
*/
public void open()
{
if (!BankAccount.isClosed())
{
BankAccount.setStatus(OPEN_STATE);
}
}
/**
* If a bank account is not already closed, opens the account (if suspended) and withdraws the current balance then changes its status to closed.
*/
public void close()
{
if (!BankAccount.isClosed())
{
if (BankAccount.isSuspended())
{
BankAccount.setStatus(OPEN_STATE);
BankAccount.withdraw(balance);
}
BankAccount.setStatus(CLOSED_STATE);
}
}
/**
* If legal state change, updates a bank account's status to suspended, otherwise does nothing.
*/
public void suspend()
{
if (!BankAccount.isClosed())
{
BankAccount.setStatus(SUSPENDED_STATE);
}
}
/**
* Returns true if a bank account is in the open state.
* Must use this method to check account's current state.
* @return the truth
*/
public static boolean isOpen()
{
if (status.equals(OPEN_STATE)) {
return true;
}
return false;
}
/**
* Returns true if a bank account is in the suspended state.
* Must use this method to check account's current state.
* @return the truth value
*/
public static boolean isSuspended()
{
if (status.equals(SUSPENDED_STATE)) {
return true;
}
return false;
}
/**
* Returns true if a bank account is in the closed state.
* Must use this method to check account's current state.
* @return the truth value
*/
public static boolean isClosed()
{
if (status.equals(CLOSED_STATE)) {
return true;
}
return false;
}
/**
* Adds an amount to the bank account's list (+ for deposits, - for withdrawals) of transactions
*/
public void addTransaction(double amount)
{
accountTransactions.add(amount);
accountTransactions.add(-amount);
}
/**
* Withdraws money from a bank account.
* @param amount the amount to withdraw
*/
public static void withdraw(double amount)
{
if (BankAccount.isOpen() && amount > 0 && amount <= balance)
{
double newBalance = balance - amount;
balance = newBalance;
accountTransactions.add(-amount);
}
}
/**
* Gets the status of a bank account
* @return a bank account's status
*/
public String getStatus()
{
return status;
}
/**
* Ensures proper state changes.
*/
private static void setStatus(String status)
{
status = "";
}
/**
* Gets the current balance of a bank account.
* @return the current balance
*/
public double getBalance()
{
return balance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment