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

  • Positional-Only Arguments in Python

    Positional-Only Arguments in Python Positional-only arguments are function parameters that must be passed by position (order) and cannot be passed by keyword name. Syntax Use the / symbol in the function definition to indicate that all parameters before it are positional-only: python def function_name(param1, param2, /, param3, param4): # function body Simple Examples Example 1: Basic Positional-Only Arguments python def calculate_area(length,…

  • The print() Function in Python

    The print() Function in Python: Complete Guide The print() function is Python’s built-in function for outputting data to the standard output (usually the console). Let’s explore all its arguments and capabilities in detail. Basic Syntax python print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False) Arguments Explained 1. *objects (Positional Arguments) The values to print. You can pass multiple items separated by commas. Examples:…

  • Python Program to Check Pangram Phrases

    Python Program to Check Pangram Phrases What is a Pangram? A pangram is a sentence or phrase that contains every letter of the alphabet at least once. Method 1: Using Set Operations python def is_pangram_set(phrase): “”” Check if a phrase is a pangram using set operations “”” # Convert to lowercase and remove non-alphabetic characters…

  • Number Manipulation and F-Strings in Python, with examples:

    Python, mathematical operators are symbols that perform arithmetic operations on numerical values. Here’s a breakdown of the key operators: Basic Arithmetic Operators: Other Important Operators: Operator Precedence: Python follows the standard mathematical order of operations (often remembered by the acronym PEMDAS or BODMAS): Understanding these operators and their precedence is essential for performing calculations in…

  • File Handling in Python

    File Handling in Python File handling is a crucial aspect of programming that allows you to read from and write to files. Python provides built-in functions and methods to work with files efficiently. Basic File Operations 1. Opening a File Use the open() function to open a file. It returns a file object. python # Syntax: open(filename,…

Leave a Reply

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