Menu
Coddy logo textTech

Recap - Bank Account Manager

Part of the Object Oriented Programming section of Coddy's Java journey — lesson 14 of 87.

challenge icon

Challenge

Easy

Let's build a complete BankAccount system that brings together everything you've learned about encapsulation, access modifiers, getters, setters, and the final keyword.

You'll create two files to organize your code:

  • BankAccount.java: Design a secure bank account class that protects sensitive financial data:
    • An immutable account number that can never change once the account is created
    • A private balance that starts at 0.0 and can only be modified through controlled methods
    • A private owner name that can be retrieved but not changed directly
    • A constructor that takes the account number and owner name
    • A getter for the account number
    • A getter for the owner name
    • A getter for the balance
    • A deposit(double amount) method that adds to the balance only if the amount is positive, returning true for success or false if rejected
    • A withdraw(double amount) method that subtracts from the balance only if the amount is positive AND sufficient funds exist, returning true for success or false if rejected
    • A getAccountSummary() method that returns: "Account [accountNumber] | Owner: [ownerName] | Balance: $[balance]" (balance formatted to 2 decimal places)
  • Main.java: Create a BankAccount and perform a series of transactions. You'll receive five inputs: account number, owner name, deposit amount, first withdrawal amount, and second withdrawal amount. Print four lines:
    • The result of the deposit: Deposit: true or Deposit: false
    • The result of the first withdrawal: Withdraw 1: true or Withdraw 1: false
    • The result of the second withdrawal: Withdraw 2: true or Withdraw 2: false
    • The final account summary

You will receive five inputs in order: accountNumber (String), ownerName (String), depositAmount (double), withdrawAmount1 (double), withdrawAmount2 (double).

For formatting the balance to 2 decimal places, you can use String.format("%.2f", balance).

Try it yourself

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Read inputs
        String accountNumber = scanner.nextLine();
        String ownerName = scanner.nextLine();
        double depositAmount = scanner.nextDouble();
        double withdrawAmount1 = scanner.nextDouble();
        double withdrawAmount2 = scanner.nextDouble();
        
        // TODO: Create a BankAccount object with accountNumber and ownerName
        
        // TODO: Perform deposit and print result as "Deposit: true" or "Deposit: false"
        
        // TODO: Perform first withdrawal and print result as "Withdraw 1: true" or "Withdraw 1: false"
        
        // TODO: Perform second withdrawal and print result as "Withdraw 2: true" or "Withdraw 2: false"
        
        // TODO: Print the account summary
    }
}

All lessons in Object Oriented Programming