1+
2+ class ATM :
3+ def __init__ (self ):
4+ self .balance = 0
5+
6+ def check_balance (self ):
7+ return self .balance
8+
9+ def deposit (self , amount ):
10+ if amount <= 0 :
11+ raise ValueError ('Deposit amount must be positive.' )
12+
13+ self .balance += amount
14+
15+ def withdraw (self , amount ):
16+ if amount <= 0 :
17+ raise ValueError ('Withdrawal amount must be positive.' )
18+ if amount > self .balance :
19+ raise ValueError ('Insufficient funds.' )
20+
21+ self .balance -= amount
22+
23+ class ATMController :
24+ def __init__ (self ):
25+ self .atm = ATM ()
26+
27+ def get_number (self , prompt ):
28+ while True :
29+ try :
30+ number = float (input (prompt ))
31+ return number
32+ except ValueError :
33+ print ('Please enter a valid number.' )
34+
35+
36+ def display_menu (self ):
37+ print ('\n Welcome to the ATM!' )
38+ print ('1. Check Balance' )
39+ print ('2. Deposit' )
40+ print ('3. Withdraw' )
41+ print ('4. Exit' )
42+
43+ def check_balance (self ):
44+ balance = self .atm .check_balance ()
45+ print (f'Your current balance is: ${ balance } ' )
46+
47+ def deposit (self ):
48+ while True :
49+ try :
50+ amount = self .get_number ('Enter the amount to deposit: ' )
51+ self .atm .deposit (amount )
52+ print (f'Successfully deposited ${ amount } .' )
53+ break
54+ except ValueError as error :
55+ print (error )
56+
57+ def withdraw (self ):
58+ while True :
59+ try :
60+ amount = self .get_number ('Enter the amount to withdraw: ' )
61+ self .atm .withdraw (amount )
62+ print (f'Successfully withdrew ${ amount } .' )
63+ break
64+ except ValueError as error :
65+ print (error )
66+
67+ def run (self ):
68+ while True :
69+ self .display_menu ()
70+
71+ choice = input ('Please choose an option: ' )
72+ if choice == '1' :
73+ self .check_balance ()
74+ elif choice == '2' :
75+ self .deposit ()
76+ elif choice == '3' :
77+ self .withdraw ()
78+ elif choice == '4' :
79+ print ('Thank you for using the ATM.' )
80+ break
81+ else :
82+ print ('Invalid choice. Please try again.' )
83+
84+
85+ def main ():
86+ atm = ATMController ()
87+ atm .run ()
88+
89+
90+ if __name__ == '__main__' :
91+ main ()
92+
0 commit comments