|
| 1 | +class BankAccount: |
| 2 | + def __init__(self, account_holder, balance): |
| 3 | + self.account_holder = account_holder |
| 4 | + self.__balance = balance |
| 5 | + |
| 6 | + def deposit(self, amount): |
| 7 | + if amount > 0: |
| 8 | + self.__balance += amount |
| 9 | + print(f"Deposited {amount}") |
| 10 | + else: |
| 11 | + print("Invalid deposit amount") |
| 12 | + |
| 13 | + def withdraw(self, amount): |
| 14 | + if 0 < amount <= self.__balance: |
| 15 | + self.__balance -= amount |
| 16 | + print(f"Withdrawn {amount}") |
| 17 | + else: |
| 18 | + print("Insufficient balance or invalid amount") |
| 19 | + |
| 20 | + def get_balance(self): |
| 21 | + return self.__balance |
| 22 | + |
| 23 | + def display_balance(self): |
| 24 | + print(f"Account Holder: {self.account_holder}") |
| 25 | + print(f"Balance: {self.__balance}") |
| 26 | + |
| 27 | +class SavingsAccount(BankAccount): |
| 28 | + def __init__(self, account_holder, balance, interest_rate): |
| 29 | + super().__init__(account_holder, balance) |
| 30 | + self.interest_rate = interest_rate |
| 31 | + |
| 32 | + def apply_interest(self): |
| 33 | + interest = self.get_balance() * self.interest_rate |
| 34 | + self.deposit(interest) |
| 35 | + print("Interest applied") |
| 36 | + |
| 37 | + def display_balance(self): |
| 38 | + super().display_balance() |
| 39 | + print(f"Interest Rate: {self.interest_rate * 100}%") |
| 40 | + |
| 41 | +if __name__ == "__main__": |
| 42 | + print("=== Welcome to the Savings Account System ===") |
| 43 | + name = input("Enter account holder name: ") |
| 44 | + |
| 45 | + try: |
| 46 | + balance = float(input("Enter initial balance: ")) |
| 47 | + interest_rate = float(input("Enter interest rate (e.g., 0.05 for 5%): ")) |
| 48 | + except ValueError: |
| 49 | + print("Invalid input. Exiting.") |
| 50 | + exit() |
| 51 | + except Exception as e: |
| 52 | + print(f"An error occurred: {str(e)}") |
| 53 | + exit() |
| 54 | + |
| 55 | + acc = SavingsAccount(name, balance, interest_rate) |
| 56 | + |
| 57 | + while True: |
| 58 | + print("\nSelect an option:") |
| 59 | + print("1. Deposit") |
| 60 | + print("2. Withdraw") |
| 61 | + print("3. Apply Interest") |
| 62 | + print("4. Display Balance") |
| 63 | + print("5. Exit") |
| 64 | + |
| 65 | + choice = input("Enter your choice (1-5): ") |
| 66 | + |
| 67 | + if choice == '1': |
| 68 | + try: |
| 69 | + amount = float(input("Enter amount to deposit: ")) |
| 70 | + acc.deposit(amount) |
| 71 | + except ValueError: |
| 72 | + print("Invalid amount.") |
| 73 | + elif choice == '2': |
| 74 | + try: |
| 75 | + amount = float(input("Enter amount to withdraw: ")) |
| 76 | + acc.withdraw(amount) |
| 77 | + except ValueError: |
| 78 | + print("Invalid amount.") |
| 79 | + elif choice == '3': |
| 80 | + acc.apply_interest() |
| 81 | + elif choice == '4': |
| 82 | + acc.display_balance() |
| 83 | + elif choice == '5': |
| 84 | + print("Thank you! Exiting.") |
| 85 | + break |
| 86 | + else: |
| 87 | + print("Invalid choice. Please select again.") |
0 commit comments