Sunday, February 26, 2017

Bank.java

Bank.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/**
 *
 * @author rex
 * @version 1.0
 * @date Feb 5 2017
 *
 */
public class Bank {
    public static HashMap<String, BankCustomer> theBank;
    public InputReader scanner;

    /**
     * Default constructor for Bank class. Initializes the HashMap
     */
    public Bank() {
        theBank = new HashMap<String, BankCustomer>();
        scanner = new InputReader();
    }

    /**
     * Removes an BankCustomer from the HashMap.
     *
     * @param accountNumber, The key of the element to remove from the HashMap.
     */
    public void closeAccount(String accountNumber) {
        theBank.remove(accountNumber);
    }

    /**
     * Add a new BankCustomer element to the HashMap.
     *
     * @param newCustomer, The new element to add to the HashpMap using the
     * account number as the key and the new BankCustomer as the value.
     */
    public void createAccount(BankCustomer newCustomer) {
        String userAccountNumber = newCustomer.getAccountNumber();
        theBank.put(userAccountNumber, newCustomer);
    }

    /**
     * Gets the BankCustomer from the HashMap and adds a double amount to a
     * BankCustomer's balance.
     *
     * @param accountNumber, The account number of the BankCustomer.
     * @param amount, The amount to deposit.
     */
    public void deposit(java.lang.String accountNumber, double amount) {
    theBank.get(accountNumber).addToBalance(amount);
    }

    /**
     * Displays the details of a BankCustomer element in the HshMap. Uses
     * BankCustomer.toString() implementation.
     *
     * @param customer, the BankCustomer chosen to display.
     */
    public static void displayCustomerInformation(BankCustomer customer) {
        ArrayList array = new ArrayList();
        array.add(customer.toString());
        System.out.println(array.toString());
    }
 
    public BankCustomer getCustomerDetail(String customer) {
        return theBank.get(customer);
    }

    /**
     * Displays all elements of the HashMap by using BankCustomer.toString()
     * implementation of each.
     *
     */
    public static void displayAllCustomers() {
        Set<String> keys = theBank.keySet();
        for (String item : keys) {
            System.out.println(theBank.get(item).toString());
        }
    }

    /**
     * Gets the BankCustomer from the HashMap and subtracts an amount from a
     * BankCustomer's balance as long as it does not leave a negative balance.
     *
     *
     * @param accountNumber, The account number of the BankCustomer.
     * @param amout, The amount to subtract from a BankCustomer's balance.
     */
    public void withdraw(java.lang.String accountNumber, double amount) {
        double balance = theBank.get(accountNumber).getBalance();
        if ((balance - amount) < 0) {
            System.out.println("Invalid Transaction: This will result in a negative balance: "+ balance);
        } else {
            double newBalance = balance - amount;
            theBank.get(accountNumber).setBalance(newBalance);
        }
    }
}

No comments:

Post a Comment