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:ย
name,ยaccount_number,ยbalance
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:
- Create account objects
- Test deposit functionality
- Test withdrawal (both valid and invalid amounts)
- Verify automatic account numbering
- 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()