Bank Account System - Problem

Design a bank account system using object-oriented programming principles. Implement three classes: Account, SavingsAccount, and CheckingAccount.

The system should support the following operations:

  • deposit(amount): Add money to the account
  • withdraw(amount): Remove money from the account (if sufficient balance)
  • transfer(amount, targetAccount): Transfer money between accounts
  • getBalance(): Get current account balance

Each account has a unique account number and tracks its balance. The SavingsAccount has a minimum balance requirement, while CheckingAccount allows overdrafts up to a limit.

Given a series of operations, return the final state of all accounts as a JSON object.

Input & Output

Example 1 — Basic Account Operations
$ Input: operations = [{"type":"create","accountNumber":"ACC001","accountType":"savings","initialBalance":1000,"minBalance":100},{"type":"deposit","accountNumber":"ACC001","amount":200},{"type":"withdraw","accountNumber":"ACC001","amount":50}]
Output: {"ACC001":{"type":"savings","balance":1150}}
💡 Note: Create savings account with $1000, deposit $200 (balance = $1200), withdraw $50 (balance = $1150). Withdrawal allowed since $1150 > $100 minimum.
Example 2 — Transfer Between Accounts
$ Input: operations = [{"type":"create","accountNumber":"ACC001","accountType":"savings","initialBalance":1000},{"type":"create","accountNumber":"ACC002","accountType":"checking","initialBalance":500},{"type":"transfer","fromAccount":"ACC001","toAccount":"ACC002","amount":200}]
Output: {"ACC001":{"type":"savings","balance":800},"ACC002":{"type":"checking","balance":700}}
💡 Note: Transfer $200 from savings to checking. Both accounts updated: savings $1000→$800, checking $500→$700.
Example 3 — Minimum Balance Constraint
$ Input: operations = [{"type":"create","accountNumber":"ACC001","accountType":"savings","initialBalance":150,"minBalance":100},{"type":"withdraw","accountNumber":"ACC001","amount":60}]
Output: {"ACC001":{"type":"savings","balance":150}}
💡 Note: Withdrawal of $60 denied because $150-$60=$90 would be below $100 minimum balance. Balance remains $150.

Constraints

  • 1 ≤ operations.length ≤ 1000
  • Account numbers are unique strings
  • All monetary amounts are non-negative numbers
  • Minimum balance for savings accounts ≥ 0
  • Overdraft limit for checking accounts ≥ 0

Visualization

Tap to expand
INPUTOperations:create ACC001 savings $1000create ACC002 checking $500deposit ACC001 $200transfer ACC001→ACC002 $300Account constraints:• Savings: min balance $100• Checking: overdraft -$500• All amounts non-negativeALGORITHM1Create Class HierarchyAccount → SavingsAccount, CheckingAccount2Store in Hash MapaccountNumber → Account object3Process OperationsEach class handles own validation4Return Final StateJSON with all account balancesSavingsAccountbalance ≥ minBalanceCheckingAccountbalance ≥ -overdraftLimitRESULTFinal Account Balances:ACC001 (Savings)$900$1000+$200-$300=$900ACC002 (Checking)$800$500+$300=$800JSON Output:{"ACC001":{"type":"savings","balance":900},"ACC002":{"type":"checking","balance":800}}Key Insight:Object-oriented design with inheritance lets each account type handle its own validation rules,creating clean, maintainable code that's easy to extend with new account types.TutorialsPoint - Bank Account System | Object-Oriented Design
Asked in
Amazon 35 Google 28 Microsoft 22 Apple 18
28.5K Views
High Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen