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:
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
MinimumBalanceErrorif 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
MinimumBalanceErrorfor 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()