-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge 2.1.py
43 lines (40 loc) · 1.96 KB
/
challenge 2.1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'''Implement a class called BankAccount that represents a bank account. The class should have private
attributes for account number, account holder name, and account balance. Include methods to
deposit money, withdraw money, and display the account balance. Ensure that the account balance
cannot be accessed directly from outside the class. Write a program to create an instance of the
BankAccount class and test the deposit and withdrawal functionality.'''
class BankAccount:
def __init__(self, account_number, account_holder_name, initial_balance=0.0):
self.__account_number = account_number
self.__account_holder_name = account_holder_name
self.__account_balance = initial_balance
def deposit(self, amount):
if amount > 0:
self.__account_balance += amount
# self.__account_balance = self.__account_balance+amount
print("Deposited ₹{}. New balance: ₹{}".format(amount,
self.__account_balance))
else:
print("Invalid deposit amount.")
def withdraw(self, amount):
if amount > 0 and amount <= self.__account_balance:
self.__account_balance -= amount
# self.__account_balance = self.__account_balance - amount
print("Withdrew ₹{}. New balance: ₹{}".format(amount,
self.__account_balance))
else:
print("Invalid withdrawal amount or insufficient balance.")
def display_balance(self):
print("Account balance for {} (Account #{}): ₹{}".format(
self.__account_holder_name, self.__account_number,
self.__account_balance))
# Create an instance of the BankAccount class
account = BankAccount(account_number="123456789",
account_holder_name="Hari Prabu",
initial_balance=5000.0)
# Test deposit and withdrawal functionality
account.display_balance()
account.deposit(500.0)
account.withdraw(200.0)
account.withdraw(20000.0)
account.display_balance()