-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
121 lines (98 loc) · 3.28 KB
/
Copy pathpython.py
File metadata and controls
121 lines (98 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
class ATM:
def __init__(self, balance=1000):
self.balance = balance
def check_balance(self):
return self.balance
def deposit(self, amount):
if amount <= 0:
return "Amount must be greater than 0."
self.balance += amount
return f"Deposited ${amount:.2f}. New balance: ${self.balance:.2f}"
def withdraw(self, amount):
if amount <= 0:
return "Amount must be greater than 0."
if amount > self.balance:
return "Insufficient balance."
self.balance -= amount
return f"Withdrew ${amount:.2f}. Remaining balance: ${self.balance:.2f}"
def buy_tickets(atm, adults, kids):
adult_price = 200
kid_price = 100
if adults < 0 or kids < 0:
return "Ticket counts cannot be negative."
total = (adults * adult_price) + (kids * kid_price)
if total > atm.check_balance():
return "Not enough money in your ATM account to buy these tickets."
atm.withdraw(total)
return {
"message": "Tickets purchased successfully!",
"adults": adults,
"kids": kids,
"total": total,
"remaining_balance": atm.check_balance(),
}
def atm_menu(atm):
while True:
print("\nATM Menu")
print("1. Check Balance")
print("2. Deposit Money")
print("3. Withdraw Money")
print("4. Back")
choice = input("Choose an option: ")
if choice == "1":
print(f"Your balance is: ${atm.check_balance():.2f}")
elif choice == "2":
try:
amount = float(input("Enter amount to deposit: "))
print(atm.deposit(amount))
except ValueError:
print("Please enter a valid number.")
elif choice == "3":
try:
amount = float(input("Enter amount to withdraw: "))
print(atm.withdraw(amount))
except ValueError:
print("Please enter a valid number.")
elif choice == "4":
break
else:
print("Invalid option. Please try again.")
def ticket_menu(atm):
print("\nTicket Booking")
print("Adult ticket: $200")
print("Child ticket: $100")
try:
adults = int(input("How many adults? "))
kids = int(input("How many kids? "))
except ValueError:
print("Please enter valid numbers.")
return
result = buy_tickets(atm, adults, kids)
if isinstance(result, dict):
print(f"{result['message']}")
print(f"Adults: {result['adults']}")
print(f"Kids: {result['kids']}")
print(f"Total: ${result['total']:.2f}")
print(f"Remaining balance: ${result['remaining_balance']:.2f}")
else:
print(result)
def main():
atm = ATM()
print("Welcome to the ATM and Ticket System")
while True:
print("\nMain Menu")
print("1. ATM")
print("2. Buy Tickets")
print("3. Exit")
choice = input("Choose an option: ")
if choice == "1":
atm_menu(atm)
elif choice == "2":
ticket_menu(atm)
elif choice == "3":
print("Thank you for using the system!")
break
else:
print("Invalid option. Please try again.")
if __name__ == "__main__":
main()