Project: Console Bank Account
Bring it all together: build a small interactive bank program with a menu, a class and error handling.
What you will learn
- Combine classes, encapsulation, loops and input
- Build a working menu-driven console app
- Handle bad input gracefully
What you will build
A complete console bank account program the user can interact with. It shows a menu, lets them deposit, withdraw and check the balance, and keeps running until they choose to quit. You will use almost everything from this course in one project.
It pulls together: a class with encapsulation (Unit 3), a while loop and a switch menu (Unit 2), Scanner input (Unit 1), and try / catch for safety (Unit 4).
How the finished program flows
Before we write any code, here is the whole journey the running program takes, in order. Picture the user sitting at the terminal:
- The program starts, creates one
Account(balance 0) and aScannerto read what the user types. - It shows the menu — options 1 to 4 — and asks the user to choose.
- The user types a number and presses Enter; the program reads it with
nextInt(). - A
switchsends that choice to the right action: 1 deposits, 2 withdraws, 3 shows the balance, 4 quits. - For 1 or 2, the program asks for an amount and calls
depositorwithdraw, which check the amount first (no negatives, no overdrawing) before changing the balance. - If the user typed letters instead of a number,
try / catchcatches it, prints a friendly message, and the menu simply comes back. - After the action, the loop repeats from step 2 — the menu shows again — until the user chooses 4, which sets
runningto false and ends the program with "Goodbye!".
Notice this covers the everyday data actions: create/add money (deposit), read the balance (check balance), and update/reduce it (withdraw) — all guarded so the data stays valid. Now we build it in three small steps.
Step 1 — the Account class
First, a class that safely holds the balance. The field is private, and deposits and withdrawals go through checked methods.
public class Account {
private double balance = 0;
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount <= 0) {
System.out.println("Deposit must be positive.");
return;
}
balance = balance + amount;
System.out.println("Deposited " + amount);
}
public void withdraw(double amount) {
if (amount <= 0) {
System.out.println("Withdrawal must be positive.");
} else if (amount > balance) {
System.out.println("Not enough money. Balance is " + balance);
} else {
balance = balance - amount;
System.out.println("Withdrew " + amount);
}
}
}Note: Output: (No output yet — this is the blueprint. The private balance is protected: every change goes through deposit or withdraw, which check the amount first. That is encapsulation guarding our data.)
Step 2 — the menu loop
Now the main program. It creates an Account, then loops forever showing a menu until the user picks 4 (Quit). A switch handles each choice.
import java.util.Scanner;
public class Bank {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Account account = new Account();
boolean running = true;
while (running) {
System.out.println();
System.out.println("1) Deposit");
System.out.println("2) Withdraw");
System.out.println("3) Check balance");
System.out.println("4) Quit");
System.out.print("Choose an option: ");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.print("Amount to deposit: ");
account.deposit(input.nextDouble());
break;
case 2:
System.out.print("Amount to withdraw: ");
account.withdraw(input.nextDouble());
break;
case 3:
System.out.println("Balance: " + account.getBalance());
break;
case 4:
running = false;
System.out.println("Goodbye!");
break;
default:
System.out.println("Please pick 1 to 4.");
}
}
}
}Note: Output (an example session): 1) Deposit 2) Withdraw 3) Check balance 4) Quit Choose an option: 1 Amount to deposit: 100 Deposited 100.0 ... Choose an option: 2 Amount to withdraw: 30 Withdrew 30.0 ... Choose an option: 3 Balance: 70.0 ... Choose an option: 4 Goodbye! The while loop keeps the menu coming back, the switch routes each choice, and the Account methods keep the money safe. Choosing 4 sets running to false, which ends the loop.
Step 3 — guard against bad input
If the user types letters instead of a number, nextInt() throws an exception and crashes. Wrap the choice in a try / catch so the program survives and asks again.
try {
int choice = input.nextInt();
// ... the switch goes here ...
} catch (Exception e) {
System.out.println("That was not a number. Try again.");
input.nextLine(); // clear the bad input
}Note: Output (user typed hello):
Choose an option: hello
That was not a number. Try again.
Instead of crashing, the catch block prints a friendly message and input.nextLine() clears the bad text, so the menu loops back cleanly. Your program is now robust.
Your tasks
- Build the Account class with private balance and checked deposit / withdraw.
- Build the menu loop with a switch and the Quit option.
- Add try / catch so letters do not crash the program.
- Test it: deposit, withdraw too much (should be refused), check balance, then quit.
Stretch goals
- Add an account owner name field, set via the constructor, shown in the menu title.
- Keep a transaction count and show how many actions were done before quitting.
- Store several accounts in an ArrayList and let the user pick which one to use.
Tip: Build in small steps and run after each one. Get Step 1 compiling, then add the menu, then add the safety. A small finished program beats a big broken one.
Q. Why is the bank balance kept as a private field with deposit and withdraw methods?
✍️ Practice
- Build the full program from all three steps and test every menu option.
- Try to withdraw more than the balance and confirm the program refuses it politely.
🏠 Homework
- Add one stretch goal (owner name, transaction count, or multiple accounts) and write 4 to 5 lines on which Java ideas you used and what you found hardest.