Course: Advanced Programming — Assignment I
Topic: ADT Design — Inheritance, Polymorphism & Exception Handling
Language: Java (JDK 11+)
A minimal bank-transaction system that demonstrates core OOP principles:
| Concept | Where it appears |
|---|---|
| Interface implementation | TransactionInterface → BaseTransaction |
| Concrete vs Abstract class | BaseTransaction (concrete) vs a possible abstract layer |
| Polymorphism / method overriding | apply() in Deposit and Withdrawal subclasses |
| Early vs Late binding | Main.java — Q4 upcasting demonstration |
| Custom checked exception | InsufficientFundsException extends Exception |
throws declaration |
WithdrawalTransaction.apply(BankAccount) |
try…catch…finally |
WithdrawalTransaction.apply(BankAccount, boolean) |
| Reversible transactions | WithdrawalTransaction.reverse() |
BankTransactionSystem/
├── README.md
├── .gitignore
└── src/
└── com/
└── transactions/
├── interfaces/
│ └── TransactionInterface.java # Root contract
├── models/
│ └── BankAccount.java # Account value object
├── exceptions/
│ └── InsufficientFundsException.java # Checked exception
├── transactions/
│ ├── BaseTransaction.java # Q1 — concrete base
│ ├── DepositTransaction.java # Q2 — irreversible credit
│ └── WithdrawalTransaction.java # Q2/Q3 — reversible debit
└── Main.java # Q4 — client / driver code
«interface»
TransactionInterface
+ getAmount(): double
+ getDate(): Calendar
+ getTransactionID(): String
+ printTransactionDetails(): void
+ apply(BankAccount): void throws InsufficientFundsException
▲
│ implements
│
BaseTransaction (concrete)
- amount: double
- date: Calendar
- transactionID: String
+ apply(BankAccount) ← no-op / logs only
▲
│ extends
├──────────────────────────────────────┐
│ │
DepositTransaction WithdrawalTransaction
+ apply(BankAccount) + apply(BankAccount) throws InsufficientFundsException
↳ credits account + apply(BankAccount, boolean) ← overloaded
↳ irreversible ↳ try-catch-finally, partial withdrawal
+ reverse(): boolean
↳ restores debited amount
BankAccount
- accountNumber: String
- accountHolder: String
- balance: double
+ credit(double): void
+ debit(double): void
«exception»
InsufficientFundsException extends Exception
- withdrawalAmount: double
- availableBalance: double
+ getShortfall(): double
# 1. Compile — from the project root
javac -d out $(find src -name "*.java")
# 2. Run
java -cp out com.transactions.Main- Open the project root as a new project.
- Mark
srcas the Sources Root (Right-click → Mark Directory as → Sources Root). - Run
Main.java.
Install the Extension Pack for Java, open the folder, then click ▶ on Main.java.
▓▓▓ Q1 | BaseTransaction — accessors & base apply() ▓▓▓
getAmount() : 250.0
getTransactionID() : 3F9A...
...
[BaseTransaction] apply() called — no mutation performed.
▓▓▓ Q2b | WithdrawalTransaction — reversible debit & reverse() ▓▓▓
╔══ WITHDRAWAL APPLIED ═══╗
║ Balance: 15000 -> 12000 ║
╚═════════════════════════╝
╔══ WITHDRAWAL REVERSED ══╗
║ Restored: KES +3000.00 ║
╚═════════════════════════╝
▓▓▓ Q3a | Exception via 'throws' ▓▓▓
[CAUGHT InsufficientFundsException]
Message : Insufficient funds: attempted to withdraw KES 2000.00 ...
Shortfall : KES 1500.00
| Decision | Rationale |
|---|---|
InsufficientFundsException extends Exception |
Checked exception — the compiler forces every caller to acknowledge the risk of a failed withdrawal at compile time, which is the correct choice for a recoverable business-rule violation. |
apply() declared with throws in the interface |
Allows WithdrawalTransaction to propagate the checked exception while DepositTransaction silently narrows the throws clause (valid in Java overriding rules). |
BaseTransaction is concrete, not abstract |
Required by the assignment to enable direct instantiation for Q4 type-casting demonstration. |
reverse() credits amountDebited, not getAmount() |
Correctly handles the partial-withdrawal case where only a fraction of the requested amount was actually debited. |
Advanced Programming — Assignment I
October 2024, Semester: Sept – Dec 2024