Bank Account Operations in Java: Explanation & Practice

Create a full bank account class

Problem summary

Create a BankAccount class with deposit, withdraw, and balance check operations.

Starter code

// Create BankAccount with:
// - private double balance
// - void deposit(double amount)
// - boolean withdraw(double amount) - false if insufficient
// - double getBalance()

public class Main {
    public static void main(String[] args) {
        // Test case 1: start with 1000, deposit 500, withdraw 200
        // Print: Balance: <result>
    }
}

Expected output and test cases

  • 1000+500-200
    Balance: 1300.0
  • After failed withdraw
    Balance: 500.0
  • Multiple deposits
    Balance: 2000.0

Hints

  1. Check balance before withdraw
  2. Return false if insufficient funds
  3. Deposit should always succeed

Related OOP Basics exercises

Practice all OOP Basics exercises · Run this idea in the Java compiler