Recap - Bank Account Manager
Part of the Object Oriented Programming section of Coddy's Java journey — lesson 14 of 87.
Challenge
EasyLet'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, returningtruefor success orfalseif rejected - A
withdraw(double amount)method that subtracts from the balance only if the amount is positive AND sufficient funds exist, returningtruefor success orfalseif 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: trueorDeposit: false - The result of the first withdrawal:
Withdraw 1: trueorWithdraw 1: false - The result of the second withdrawal:
Withdraw 2: trueorWithdraw 2: false - The final account summary
- The result of the deposit:
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
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsFields (Attributes)Constructor MethodConstructor OverloadingRecap - Simple Calculator4Inheritance
Basic Inheritance (extends)The super KeywordMethod Overriding (@Override)Constructor ChainingThe Object ClassSingle & Multilevel InheritWhy No Multi Class InheritRecap - Employee Hierarchy7Special Methods & Object Class
toString() Methodequals() and hashCode()clone() MethodcompareTo() and ComparableComparator InterfaceRecap - Custom Sorting2Access Modifiers & Encapsulate
Access Levels OverviewGetter and Setter MethodsInformation HidingThe final KeywordRecap - Bank Account Manager5Polymorphism
Method Overloading BasicsMethod Overriding (Run-Time)Upcasting and DowncastingThe instanceof OperatorAbstract Classes and MethodsRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceAggregation vs CompositionInner Nested & Anonymous ClassEnums and Enum MethodsRecords (Java 16+)Sealed Classes (Java 17+)3Class Props & Static Member
Instance vs Static VariablesStatic MethodsStatic BlocksConstants (static final)Recap - Counter & Utility6Interfaces & Abstract Classes
Introduction to InterfacesImplementing InterfacesMulti Interface ImplemenDefault & Static in InterfaceAbstract Classes vs InterfacesFunctional InterfacesRecap - Payment System