PHW4-5: Bank Account & Bank

So far we’ve been working with composing shape objects with GUI Java classes. Now we’re going to practice composing objects that only output to the console.

Instructions

Part I: BankAccount

Rx: <15 min Av: 15-30 min Sd: 30-45 min DNF: 45+ min

  1. Create a new Java Project called BankAccount_uLogin
  2. Create a new BankAccount class with:
    • Fields to store: current balance, account number, customer name, customer address. Think about what types these should be. For example, should the account number actually be stored as a number? What about leading 0’s?
    • A constructor that takes the customer’s name, address, and account number, and initializes the balance to 0.
    • Getter and setter methods for the name and address fields. No setter for the balance or account number (can you guess why?), just getters.
    • A deposit and a debit method. The method signatures are similar in that they have no return value and one parameter.
    • A print method that prints out the account information, including the current balance. Make sure to use proper formatting so both dollars and cents are displayed correctly. This method should take no parameters and return nothing. (Hint: check out Java’s NumberFormat.getCurrencyInstance() and this usage example.)
  3. Test your BankAccount class by writing a main method that creates a new, empty BankAccount stored in a local variable, myBankAccount.
    • Deposit $5.00 into your BankAccount & print the balance.
    • Debit $1.50 into your BankAccount & print the balance.
  4. What happens if you try to deposit negative amounts or debit more than the balance? Test each of these cases in main. What do you think should happen?
  5. Modify the debit & deposit methods so that negative amounts have no effect. For example, myBankAccount.deposit(5) will add $5, but myBankAccount.deposit(-5) will do nothing.
  6. Modify the debit method to make sure there are sufficient funds before deducting from the balance. Print an error message if there are insufficient funds.

Part II: Bank

Rx: <10 min Av: 10-20 min Sd: 20-30 min DNF: 31+ min

  1. Now let’s create a Bank class that can store multiple BankAccount objects:
    • Fields to store: the Bank’s name and a list of accounts.
    • A constructor that takes the Bank’s name and initializes the list of accounts.
    • Get and set methods for the name, and a getter for the account list.
    • An add method that takes an account as a parameter and adds it to the list of accounts.
    • A print method that prints out the Bank’s name followed by an overview of all the accounts (i.e., by calling print on each account).
  2. Test your Bank class by writing a main method that creates a new Bank. Add 2-3 accounts to this bank and make sure they have balances > 0. Display the Bank information by calling print.

Demonstration

Once you’ve finished doing the WOD a single time, watch me do it:

Standard Practice HW Advice

You’ll learn significantly less from watching me solve the practice HW if you haven’t attempted the HW yourself first.

While it’s an achievement to finish the HW no matter how long it takes, you might experience “diminishing returns” if you work longer than the DNF time. Thus, it might be strategic to stop working at the DNF time and watch my solution.

After watching my solution, I recommend that you repeat the HW if you have not achieved at least Av performance. If so, be sure to:

Feel free to keep trying until you make Rx if that’s of interest to you.