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

Leave a Reply

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