code.py
code.py
class User:
def __init__(self, username, pin):
self.username = username
self.pin = pin
self.balance = 0.0
self.transaction_history = []
def check_balance(self):
return self.balance
def get_transaction_history(self):
return self.transaction_history
class ATMSimulator:
def __init__(self):
self.users = {}
self.current_user = None
def create_account(self):
username = input("Enter a username: ")
pin = input("Enter a 4-digit PIN: ")
if username in self.users:
print("Username already exists. Please choose a different username.")
else:
self.users[username] = User(username, pin)
print("Account created successfully!")
def authenticate_user(self):
username = input("Enter your username: ")
pin = input("Enter your PIN: ")
user = self.users.get(username)
if user and user.pin == 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 (unittest.TestCase):
def setUp(self):
self.user = User("testuser", "1234")
def test_deposit(self):
self.user.deposit(100)
self.assertEqual(self.user.check_balance(), 100)
def test_withdraw(self):
self.user.deposit(100)
self.user.withdraw(50)
self.assertEqual(self.user.check_balance(), 50)
def test_withdraw_insufficient_funds(self):
with self.assertRaises(ValueError):
self.user.withdraw(50)
def test_deposit_negative_amount(self):
with self.assertRaises(ValueError):
self.user.deposit(-50)
def test_withdraw_negative_amount(self):
with self.assertRaises(ValueError):
self.user.withdraw(-50)
if __name__ == "__main__":
atm = ATMSimulator()
atm.run()
unittest.main()