| Concept | How It Appears |
|---|---|
| Class | User, BankUser, Admin |
| Object | user1 = BankUser(...) |
| Encapsulation | __atm_pin (private) and accessed via methods |
| Abstraction | Hides PIN logic and transaction logic via methods |
| Inheritance | BankUser and Admin inherit from User |
| Polymorphism | show_user_type() behaves differently based on object type |
class User: def init(self, name, atm_pin): self.name = name self.__atm_pin = atm_pin # Encapsulated (private) attribute
def authenticate(self):
"""š Method to check PIN (Abstraction)"""
pin = input("Enter your ATM PIN: ")
if pin == self.__atm_pin:
print("ā
Access Granted!")
return True
else:
print("ā Access Denied!")
return False
def change_pin(self):
"""š Change PIN securely"""
if self.authenticate():
new_pin = input("Enter your NEW ATM PIN: ")
self.__atm_pin = new_pin
print("š PIN changed successfully!")
def show_user_type(self):
"""š Demonstrates Polymorphism (to be overridden)"""
print("š¤ I am a general user.")
class BankUser(User): def init(self, name, atm_pin, balance=0.0): super().init(name, atm_pin) self.balance = balance # New property in child class
def withdraw(self):
"""šø Withdraw money"""
if self.authenticate():
try:
amount = float(input("Enter amount to withdraw: "))
if amount > self.balance:
print("ā Insufficient balance.")
else:
self.balance -= amount
print(f"ā
Withdrawn ā¹{amount}. New Balance: ā¹{self.balance}")
except ValueError:
print("ā Invalid amount entered.")
def deposit(self):
"""š° Deposit money"""
try:
amount = float(input("Enter amount to deposit: "))
self.balance += amount
print(f"ā
Deposited ā¹{amount}. New Balance: ā¹{self.balance}")
except ValueError:
print("ā Invalid amount entered.")
def show_user_type(self):
"""š¤ Overridden method (Polymorphism)"""
print("š¦ I am a Bank User.")
class Admin(User): def show_user_type(self): """š¤ Overridden method""" print("š ļø I am an Admin.")
if name == "main":
user1 = BankUser("Alice", "1234", balance=5000)
# Create an Admin
admin1 = Admin("AdminUser", "0000")
# Simple Menu
print("\\nWelcome to Python Bank š¦")
print("-------------------------")
# Choose a user
print("\\nš¹ Choose a user:")
print("1. Bank User (Alice)")
print("2. Admin User")
choice = input("Enter your choice (1 or 2): ")
if choice == "1":
print("\\n--- Bank User Menu ---")
user1.show_user_type()
print("1. Deposit")
print("2. Withdraw")
print("3. Change PIN")
action = input("Choose action: ")
if action == "1":
user1.deposit()
elif action == "2":
user1.withdraw()
elif action == "3":
user1.change_pin()
else:
print("ā Invalid Option.")
elif choice == "2":
print("\\n--- Admin Menu ---")
admin1.show_user_type()
admin1.change_pin()
else:
print("ā Invalid User Choice.")