0% found this document useful (0 votes)
44 views6 pages

Arithmetic and Bank Account Classes

Uploaded by

tribalchief1985
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views6 pages

Arithmetic and Bank Account Classes

Uploaded by

tribalchief1985
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experiment 3

1. Write a class having two integer data members. Provide facility to add, subtract,
multiply and divide these numbers. If addition goes above 1000, it generates
TooLongAddition exception. If subtraction is below 0, it generates Negative
Answer exception. If multiplication is above 5000, it generates
TooLongMultiplication exception.
CODE:
# Custom Exceptions
class TooLongAddition(Exception):
pass

class NegativeAnswer(Exception):
pass

class TooLongMultiplication(Exception):
pass

# Class for arithmetic operations


class Arithmetic:
def __init__(self, a, b):
self.a = a
self.b = b

def add(self):
result = self.a + self.b
if result > 1000:
raise TooLongAddition("Addition result exceeds 1000!")
return result

def subtract(self):
result = self.a - self.b
if result < 0:
raise NegativeAnswer("Subtraction result is negative!")
return result

def multiply(self):
result = self.a * self.b
if result > 5000:
raise TooLongMultiplication("Multiplication result exceeds
5000!")
return result

def divide(self):
if self.b == 0:
raise ZeroDivisionError("Cannot divide by zero!")
return self.a / self.b
# Demo with user input
if __name__ == "__main__":
try:
a = int(input("Enter first integer: "))
b = int(input("Enter second integer: "))

calc = Arithmetic(a, b)

print(f"Addition: {[Link]()}")
print(f"Subtraction: {[Link]()}")
print(f"Multiplication: {[Link]()}")
print(f"Division: {[Link]()}")

except TooLongAddition as e:
print(f"Caught Exception: {e}")
except NegativeAnswer as e:
print(f"Caught Exception: {e}")
except TooLongMultiplication as e:
print(f"Caught Exception: {e}")
except ZeroDivisionError as e:
print(f"Caught Exception: {e}")
except ValueError:
print("Invalid input! Please enter integers only.")

OUTPU:
2. Develop a BankAccount class which should contain all methods of Bank i.e.
balanceEnquery(),withdraw() and deposit(). Generate user defined exception
LowBalanceException
CODE:
class LowBalanceException(Exception):
def __init__(self, message="Balance is too low for withdrawal!"):
super().__init__(message)

# BankAccount Class
class BankAccount:
def __init__(self, account_no, holder_name, balance=0):
self.account_no = account_no
self.holder_name = holder_name
[Link] = balance

def balanceEnquiry(self):
print(f"Account Holder: {self.holder_name}")
print(f"Account Number: {self.account_no}")
print(f"Current Balance: ₹{[Link]}")

def deposit(self, amount):


if amount <= 0:
print("Deposit amount must be positive!")
else:
[Link] += amount
print(f"₹{amount} deposited successfully. New balance:
₹{[Link]}")

def withdraw(self, amount):


if amount <= 0:
print("Withdrawal amount must be positive!")
elif amount > [Link]:
raise LowBalanceException(f"Cannot withdraw ₹{amount}. Available
balance: ₹{[Link]}")
else:
[Link] -= amount
print(f"₹{amount} withdrawn successfully. Remaining balance:
₹{[Link]}")

# Demo / User Input


if __name__ == "__main__":
try:
account_no = input("Enter account number: ")
holder_name = input("Enter account holder name: ")
initial_balance = float(input("Enter initial balance: ₹"))

account = BankAccount(account_no, holder_name, initial_balance)

while True:
print("\n--- Bank Menu ---")
print("1. Balance Enquiry")
print("2. Deposit")
print("3. Withdraw")
print("4. Exit")
choice = input("Enter your choice (1-4): ")

if choice == "1":
[Link]()
elif choice == "2":
amt = float(input("Enter amount to deposit: ₹"))
[Link](amt)
elif choice == "3":
amt = float(input("Enter amount to withdraw: ₹"))
[Link](amt)
elif choice == "4":
print("Exiting... Thank you!")
break
else:
print("Invalid choice! Please enter 1-4.")

except LowBalanceException as e:
print(f"LowBalanceException: {e}")
except ValueError:
print("Invalid input! Please enter numeric values for balance and
amounts.")

OUTPUT:

You might also like