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

  • What is PyCharm? Uses, History, and Step-by-Step Installation Guide

    What is PyCharm? PyCharm is a popular Integrated Development Environment (IDE) specifically designed for Python development. It is developed by JetBrains and is widely used by Python developers for its powerful features, ease of use, and support for various Python frameworks and tools. PyCharm is available in two editions: Uses of PyCharm PyCharm is a…

  • Vs code

    What is VS Code? 💻 Visual Studio Code (VS Code) is a free, lightweight, and powerful code editor developed by Microsoft. It supports multiple programming languages (Python, JavaScript, Java, etc.) with: VS Code is cross-platform (Windows, macOS, Linux) and widely used for web development, data science, and general programming. 🌐📊✍️ How to Install VS Code…

  • Python and PyCharm Installation on Windows: Complete Beginner’s Guide 2025

    Installing Python and PyCharm on Windows is a straightforward process. Below are the prerequisites and step-by-step instructions for installation. Prerequisites for Installing Python and PyCharm on Windows Step-by-Step Guide to Install Python on Windows Step 1: Download Python Step 2: Run the Python Installer Step 3: Verify Python Installation If Python is installed correctly, it…

  • re.split()

    Python re.split() Method Explained The re.split() method splits a string by the occurrences of a pattern. It’s like the built-in str.split() but much more powerful because you can use regex patterns. Syntax python re.split(pattern, string, maxsplit=0, flags=0) Example 1: Splitting by Multiple Delimiters python import retext1=”The re.split() method splits a string by the occurrences of a pattern. It’s like…

  • re Programs

    The regular expression r’;\s*(.*?);’ is used to find and extract text that is located between two semicolons. In summary, this expression finds a semicolon, then non-greedily captures all characters up to the next semicolon. This is an effective way to extract the middle value from a semicolon-separated string. Title 1 to 25 chars The regular…

  • Sets in Python

    Sets in Python A set in Python is an unordered collection of unique elements. Sets are mutable, meaning you can add or remove items, but the elements themselves must be immutable (like numbers, strings, or tuples). Key Characteristics of Sets: Different Ways to Create Sets in Python Here are various methods to create sets in…

Leave a Reply

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