Tutorialspoint
Problem
Solution
Submissions

Bank Account Representing

Certification: Advanced Level Accuracy: 100% Submissions: 2 Points: 10

Design a Python class called BankAccount that represents a basic bank account. The class should have methods to deposit funds, withdraw funds, and check the current balance. The account should not allow withdrawals that would result in a negative balance.

  • Example 1
    • Input: account = BankAccount("12345", "John Doe", 1000)
    • Output: 1300
  • Example 2
    • Input: account = BankAccount("67890", "Jane Smith", 500)
    • Output: "Insufficient funds. Current balance: 500"
  • Constraints
    • Account number must be a non-empty string
    • Account holder name must be a non-empty string
    • Initial balance must be non-negative
    • Time Complexity: O(1) for all operations
    • Space Complexity: O(1) for all operations
Variables and Data TypesObject-Oriented ProgrammingPwCSnowflake
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use instance variables to store account details and balance
  • Implement validation in the withdraw method to prevent negative balance
  • Return appropriate messages or status for failed operations
  • Consider implementing a transaction history (optional)
  • Use proper encapsulation principles

Steps to solve by this approach:

 Step 1: Create a BankAccount class with account number, holder name, and balance attributes.
 Step 2: Implement initialization with validation to prevent negative initial balances.
 Step 3: Add a deposit method that validates the amount and updates the balance.
 Step 4: Develop a withdraw method that checks for sufficient funds before updating balance.
 Step 5: Implement a get_balance method to retrieve the current balance.
 Step 6: Return informative messages about transaction results.
 Step 7: Handle edge cases like negative deposits or withdrawals exceeding available balance.

Submitted Code :