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

  • Curly Braces {} ,Pipe (|) Metacharacters

    Curly Braces {} in Python Regex Curly braces {} are used to specify exact quantity of the preceding character or group. They define how many times something should appear. Basic Syntax: Example 1: Exact Number of Digits python import re text = “Zip codes: 12345, 9876, 123, 123456, 90210″ # Match exactly 5 digits pattern = r”\d{5}” # Exactly…

  • Predefined Character Classes

    Predefined Character Classes Pattern Description Equivalent . Matches any character except newline \d Matches any digit [0-9] \D Matches any non-digit [^0-9] \w Matches any word character [a-zA-Z0-9_] \W Matches any non-word character [^a-zA-Z0-9_] \s Matches any whitespace character [ \t\n\r\f\v] \S Matches any non-whitespace character [^ \t\n\r\f\v] 1. Literal Character a Matches: The exact character…

  • Keyword-Only Arguments in Python and mixed

    Keyword-Only Arguments in Python Keyword-only arguments are function parameters that must be passed using their keyword names. They cannot be passed as positional arguments. Syntax Use the * symbol in the function definition to indicate that all parameters after it are keyword-only: python def function_name(param1, param2, *, keyword_only1, keyword_only2): # function body Simple Examples Example 1: Basic Keyword-Only Arguments…

  • 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…

  • Operator Overloading

    Operator Overloading in Python with Simple Examples Operator overloading allows you to define how Python operators (like +, -, *, etc.) work with your custom classes. This makes your objects behave more like built-in types. 1. What is Operator Overloading? 2. Basic Syntax python class ClassName: def __special_method__(self, other): # Define custom behavior 3. Common Operator Overloading Methods Operator…

  • Generators in Python

    Generators in Python What is a Generator? A generator is a special type of iterator that allows you to iterate over a sequence of values without storing them all in memory at once. Generators generate values on-the-fly (lazy evaluation) using the yield keyword. Key Characteristics Basic Syntax python def generator_function(): yield value1 yield value2 yield value3 Simple Examples Example…

Leave a Reply

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