Java OOP · Module 13
Stage 1 — Customer, Account and safe money
Classes, constructors, encapsulation and validation working together
What you'll be able to do
- See the whole system before writing any of it
- Give each class exactly one responsibility
- Protect money with private fields plus validating methods
- Make rejected operations leave the object completely unchanged
Project — Accounts & Encapsulation
The plan, then the foundation
You now know every concept this system needs. The only new skill in Module 13 is deciding *which* concept to use where.
The four stages
| Stage | What you add | Concepts exercised |
|---|---|---|
| 1. Foundation | Customer, Account with validated deposit/withdraw | classes, constructors, encapsulation, validation, composition |
| 2. Hierarchy | abstract Account → SavingsAccount, CurrentAccount | abstract classes, inheritance, super, overriding, polymorphism |
| 3. Transactions | Transactable contract + Transaction history | interfaces, composition, arrays, encapsulation |
| 4. Bank | Bank that owns the accounts and reports | aggregation, polymorphic loops, lookups, abstraction |
Each stage is a complete, runnable program judged by its own hidden tests — you never have to keep four stages working at once.
Step 1: give every class one job
Before typing, write the responsibilities down. This is the design step professionals do on paper:
| Class | Knows | Does | Never does |
| --- | --- | --- | --- |
| Customer | a name | expose it through a getter | nothing about money |
| Account | id, owner, balance | deposit, withdraw, statement | print the customer's internals |
Two design decisions follow immediately:
1. Account HAS-A Customer (a field), because an account is not a kind of customer — the Module 12 test.
2. balance is private, because Module 4 showed what happens when money is a public field: account.balance = -5000; compiles and destroys your data.
Step 2: the rules money must obey
- A deposit must be positive — otherwise print
Invalid amountand change nothing. - A withdrawal must be positive and available — otherwise print
Insufficient funds, returnfalse, and change nothing. - A successful withdrawal returns
true, so the caller can react to the outcome. - Validation lives *inside* the class. If callers had to check first, one forgetful caller would corrupt the balance.
Study this wallet, then build the bank version yourself in the challenge below. Try `wallet.add(-10.0);` — the guard prints and returns before touching the amount.
Why the guard is written this way
if (account.withdraw(100)) { printReceipt(); }. A void method would throw that information away.Read the expected output like a specification
The challenge expects Insufficient funds and then ACC-1 | Sara | 650.0. That single line tells you: the rejected withdrawal printed nothing else, the successful ones did not print at all, and statement() uses | as the separator with the balance formatted as a double. Matching the specification exactly is part of the job.
Prove it — coding challenges
Common mistakes
Printing `Insufficient funds` and then subtracting anyway.Storing the owner's name in `Account` as a `String`.Making `balance` public 'just for the statement'.Lesson playground
A rejected deposit must not move the total *or* the counter. Add a `withdraw` with the same discipline and check that a rejected withdrawal leaves both untouched.
Check understanding
Quiz
Question 1 of 3
Two withdrawals, one of them impossible. What is printed?
Summary
- Design first: one responsibility per class, written down before any code.
- `Account` HAS-A `Customer`; money is private and reached only through methods.
- Guard, print, return — a rejected operation must leave the object exactly as it was.
- Return `boolean` from operations that can fail so callers can react.