Practice
Bank Account Encapsulation
HARDObject-Oriented Programming12 marks
Declare class BankAccount with a private integer Balance.
- Constructor
NEW(Start)stores the opening balance Deposit(Amount)adds Amount only when Amount > 0Withdraw(Amount)subtracts Amount only when Amount > 0 and Amount <= BalanceGetBalance()returns the current balance
The test harness reads an opening balance, then N operations. Each operation is D (deposit) or W (withdraw) followed by an amount. Output the final balance.
Input: Start, N, then N pairs of operation and amount. Output: One integer.
Example:
Input: 100
4
D
50
W
20
D
-5
W
1000
Output: 130
Premium is coming soon. All grading features are currently unlocked.
Sample Test Cases
Test 1: Invalid deposit and overdraft ignored
Inputs: 100, 4, D, 50, W, 20, D, -5, W, 1000
Expected: 130
Test 2: Deposit into an empty account
Inputs: 0, 1, D, 25
Expected: 25