import unittest
class User:
def __init__(self, username, pin):
[Link] = username
[Link] = pin
[Link] = 0.0
self.transaction_history = []
def deposit(self, amount):
if amount <= 0:
raise ValueError("Deposit amount must be positive.")
[Link] += amount
self.transaction_history.append(f"Deposited: ${amount:.2f}")
def withdraw(self, amount):
if amount <= 0:
raise ValueError("Withdrawal amount must be positive.")
if amount > [Link]:
raise ValueError("Insufficient funds.")
[Link] -= amount
self.transaction_history.append(f"Withdrew: ${amount:.2f}")
def check_balance(self):
return [Link]
def get_transaction_history(self):
return self.transaction_history
class ATMSimulator:
def __init__(self):
[Link] = {}
self.current_user = None
def create_account(self):
username = input("Enter a username: ")
pin = input("Enter a 4-digit PIN: ")
if username in [Link]:
print("Username already exists. Please choose a different username.")
else:
[Link][username] = User(username, pin)
print("Account created successfully!")
def authenticate_user(self):
username = input("Enter your username: ")
pin = input("Enter your PIN: ")
user = [Link](username)
if user and [Link] == pin:
self.current_user = user
print(f"Welcome, {username}!")
else:
print("Invalid username or PIN.")
def display_menu(self):
print("\nATM Menu:")
print("1. Check Balance")
print("2. Deposit Money")
print("3. Withdraw Money")
print("4. Transaction History")
print("5. Logout")
def run(self):
while True:
print("\n1. Create Account")
print("2. Login")
print("3. Exit")
choice = input("Select an option (1-3): ")
if choice == '1':
self.create_account()
elif choice == '2':
self.authenticate_user()
if self.current_user:
while True:
self.display_menu()
option = input("Select an option (1-5): ")
try:
if option == '1':
print(f"Your current balance is: $
{self.current_user.check_balance():.2f}")
elif option == '2':
amount = float(input("Enter amount to deposit: $"))
self.current_user.deposit(amount)
print(f"${amount:.2f} has been deposited.")
elif option == '3':
amount = float(input("Enter amount to withdraw:
$"))
self.current_user.withdraw(amount)
print(f"${amount:.2f} has been withdrawn.")
elif option == '4':
history =
self.current_user.get_transaction_history()
print("Transaction History:")
for transaction in history:
print(transaction)
elif option == '5':
print("Logging out...")
self.current_user = None
break
else:
print("Invalid choice. Please select a valid
option.")
except ValueError as e:
print(f"Error: {e}")
elif choice == '3':
print("Thank you for using the ATM. Goodbye!")
break
else:
print("Invalid choice. Please select a valid option.")
# Unit Testing
class TestUser ([Link]):
def setUp(self):
[Link] = User("testuser", "1234")
def test_deposit(self):
[Link](100)
[Link]([Link].check_balance(), 100)
def test_withdraw(self):
[Link](100)
[Link](50)
[Link]([Link].check_balance(), 50)
def test_withdraw_insufficient_funds(self):
with [Link](ValueError):
[Link](50)
def test_deposit_negative_amount(self):
with [Link](ValueError):
[Link](-50)
def test_withdraw_negative_amount(self):
with [Link](ValueError):
[Link](-50)
if __name__ == "__main__":
atm = ATMSimulator()
[Link]()
[Link]()