Bank Account Class with Minimum Balance

Challenge Summary: Bank Account Class with Minimum Balance

Objective:

Create a BankAccount class that automatically assigns account numbers and enforces a minimum balance rule.


1. Custom Exception Class

python

class MinimumBalanceError(Exception):
    """Custom exception for minimum balance violation"""
    pass

2. BankAccount Class Requirements

Properties:

  • Class Variable: account_number (starts from 1001)
  • Instance Variables: nameaccount_numberbalance

Methods:

__init__(self, name, initial_balance)

  • Takes account holder’s name and initial deposit
  • Validation: Initial balance must be ≥ 1000
  • Auto-assigns account number from class variable
  • Increments class variable for next account

deposit(self, amount)

  • Adds amount to current balance

withdraw(self, amount)

  • Validation: After withdrawal, balance must remain ≥ 1000
  • Raises MinimumBalanceError if rule violated
  • Deducts amount from balance if valid

show_details(self)

  • Displays all account information

3. Key Rules:

  • Minimum Balance: Always maintain ≥ 1000
  • Auto Account Numbers: Start from 1001, increment automatically
  • Error Handling: Raise MinimumBalanceError for balance violations

4. Testing Instructions:

  1. Create account objects
  2. Test deposit functionality
  3. Test withdrawal (both valid and invalid amounts)
  4. Verify automatic account numbering
  5. Check minimum balance enforcement

This response is AI-generated, for reference only.

class MinimumBalanceError(Exception):
    pass

class Account:
    AccNumber = 1001

    def __init__(self, name, balance=1000):

        if balance < 1000:
            raise MinimumBalanceError('Account Cannot be Created')
        self.name = name
        self.balance = balance
        self.account_number = Account.AccNumber
        Account.AccNumber += 1

    def deposit(self, amt):
        self.balance += amt

    def withdraw(self, amt):
        if self.balance - amt < 1000:
            raise MinimumBalanceError('Amount cannot be withdrawn')
        self.balance -= amt

    def show_details(self):
        print('Account Number',self.account_number)
        print('Name',self.name)
        print('Balance',self.balance)


a1 = Account('John', 2000)
a1.withdraw(1500)
a1.show_details()

Similar Posts

  • Escape Sequences in Python

    Escape Sequences in Python Escape sequences are special character combinations that represent other characters or actions in strings. Here’s a complete list of Python escape sequences with two examples for each: 1. \\ – Backslash python print(“This is a backslash: \\”) # Output: This is a backslash: \ print(“Path: C:\\Users\\Name”) # Output: Path: C:\Users\Name 2. \’ – Single quote…

  • String Validation Methods

    Complete List of Python String Validation Methods Python provides several built-in string methods to check if a string meets certain criteria. These methods return True or False and are useful for input validation, data cleaning, and text processing. 1. Case Checking Methods Method Description Example isupper() Checks if all characters are uppercase “HELLO”.isupper() → True islower() Checks if all…

  • Why Python is So Popular: Key Reasons Behind Its Global Fame

    Python’s fame and widespread adoption across various sectors can be attributed to its unique combination of simplicity, versatility, and a robust ecosystem. Here are the key reasons why Python is so popular and widely used in different industries: 1. Easy to Learn and Use 2. Versatility Python supports multiple programming paradigms, including: This versatility allows…

  • Special Character Classes Explained with Examples

    Special Character Classes Explained with Examples 1. [\\\^\-\]] – Escaped special characters in brackets Description: Matches literal backslash, caret, hyphen, or closing bracket characters inside character classes Example 1: Matching literal special characters python import re text = “Special chars: \\ ^ – ] [” result = re.findall(r'[\\\^\-\]]’, text) print(result) # [‘\\’, ‘^’, ‘-‘, ‘]’] # Matches…

  • Polymorphism

    Polymorphism is a core concept in OOP that means “many forms” 🐍. In Python, it allows objects of different classes to be treated as objects of a common superclass. This means you can use a single function or method to work with different data types, as long as they implement a specific action. 🌀 Polymorphism…

  • Linear vs. Scalar,Homogeneous vs. Heterogeneous 

    Linear vs. Scalar Data Types in Python In programming, data types can be categorized based on how they store and organize data. Two important classifications are scalar (atomic) types and linear (compound) types. 1. Scalar (Atomic) Data Types 2. Linear (Compound/Sequential) Data Types Key Differences Between Scalar and Linear Data Types Feature Scalar (Atomic) Linear (Compound) Stores Single…

Leave a Reply

Your email address will not be published. Required fields are marked *