-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtickets.py
More file actions
104 lines (97 loc) · 5.1 KB
/
Copy pathtickets.py
File metadata and controls
104 lines (97 loc) · 5.1 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
import datetime
from .routes import ALL_ROUTES, load_users, save_users, load_tickets, save_tickets, load_passes, save_passes
from utils import line, header, now, today, new_ticket_id, calc_fare
def view_profile(u):
header("MY PROFILE")
usr = load_users().get(u, {})
print(" Username :", u)
print(" Member Since :", usr.get("joined_on", "N/A"))
print(" Wallet Bal. : Rs.", usr.get("wallet", 0))
def top_up_wallet(u):
header("PAYMENT / TOP-UP WALLET")
users = load_users()
print("Current Balance: Rs.", users[u]["wallet"])
print("\n [1] UPI\n [2] Debit / Credit Card\n [3] Net Banking\n [0] Back")
ch = input("\nYour choice: ").strip()
methods = {"1": "UPI", "2": "Card", "3": "Net Banking"}
if ch == "0" or ch not in methods: return
while True:
try:
amt = float(input("Enter amount to add (Rs.): "))
if amt > 0: break
print("Amount must be more than 0.")
except ValueError: print("Enter a valid number.")
print(f"\nProcessing {methods[ch]} payment of Rs. {amt} ...\nPayment Successful!")
users[u]["wallet"] += amt
save_users(users)
print("New Balance: Rs.", users[u]["wallet"])
def _print_receipt(t, u, bal):
line("*"); print(" SMART FARE - TICKET RECEIPT"); line("*")
print(f" Ticket ID : {t['ticket_id']}\n Passenger : {u}\n Route : {t['route']}")
print(f" Fare Paid : Rs. {t['fare']}\n Date/Time : {t['booked_on']}\n Status : {t['status']}")
print(f" Balance : Rs. {bal}"); line("*")
print(" Thank you for travelling with Smart Fare!"); line("*")
def book_ticket(u):
header("BOOK A TICKET")
users = load_users()
print("Available Routes:\n")
for code, info in ALL_ROUTES.items():
print(f" [{code}] {info['name']} | Rs.{calc_fare(info['distance_km'])}")
rc = input("\nEnter Route Code (e.g. R01): ").strip().upper()
if rc not in ALL_ROUTES: print("Invalid route code."); return
route, fare, bal = ALL_ROUTES[rc], calc_fare(ALL_ROUTES[rc]["distance_km"]), users[u]["wallet"]
print(f"\n Route : {route['name']}\n Distance : {route['distance_km']} km\n Fare : Rs. {fare}\n Wallet : Rs. {bal}")
if bal < fare: print(f"Not enough balance! You need Rs. {fare - bal} more."); return
if input("\nConfirm booking? (y/n): ").strip().lower() != "y": print("Booking cancelled."); return
users[u]["wallet"] -= fare
save_users(users)
tickets = load_tickets()
new = {"ticket_id": new_ticket_id(), "route": route["name"], "fare": fare, "booked_on": now(), "status": "Active"}
tickets.setdefault(u, []).append(new)
save_tickets(tickets)
_print_receipt(new, u, users[u]["wallet"])
def ticket_history(u):
header("MY TICKET HISTORY")
my = load_tickets().get(u, [])
if not my: print("No tickets booked yet."); return
print(f"Total Tickets: {len(my)}\n")
for i, t in enumerate(my, 1):
print(f" [{i}] {t['ticket_id']} | {t['route']}\n Fare: Rs.{t['fare']} | {t['booked_on']}\n")
ch = input("Enter ticket number for receipt (or 0 to go back): ").strip()
try:
idx = int(ch) - 1
if 0 <= idx < len(my):
_print_receipt(my[idx], u, load_users()[u]["wallet"])
except ValueError: pass
def buy_pass(u):
header("BUY A BUS PASS")
users = load_users()
print(f"Wallet Balance: Rs. {users[u]['wallet']}\n")
print(" [1] Weekly Pass - Rs.299 (20% off, 7 days)")
print(" [2] Monthly Pass - Rs.999 (40% off, 30 days)\n [0] Back")
ch = input("\nChoose: ").strip()
opts = {"1": {"label": "Weekly Pass", "price": 299, "days": 7}, "2": {"label": "Monthly Pass", "price": 999, "days": 30}}
if ch == "0" or ch not in opts: return
sel, bal = opts[ch], users[u]["wallet"]
print(f"\n Pass : {sel['label']}\n Price : Rs. {sel['price']}\n Wallet: Rs. {bal}")
if bal < sel["price"]: print("Not enough balance."); return
if input("Confirm purchase? (y/n): ").strip().lower() != "y": print("Cancelled."); return
users[u]["wallet"] -= sel["price"]
save_users(users)
start, end = datetime.date.today(), datetime.date.today() + datetime.timedelta(days=sel["days"])
new = {"pass_id": new_ticket_id(), "label": sel["label"], "price": sel["price"], "start_date": str(start), "end_date": str(end)}
passes = load_passes()
passes.setdefault(u, []).append(new)
save_passes(passes)
line("*"); print(" SMART FARE - PASS RECEIPT"); line("*")
print(f" Pass ID : {new['pass_id']}\n Type : {new['label']}\n Valid : {new['start_date']} to {new['end_date']}")
print(f" Price : Rs. {new['price']}\n Balance : Rs. {users[u]['wallet']}"); line("*")
def view_passes(u):
header("MY BUS PASSES")
my = load_passes().get(u, [])
if not my: print("No passes bought yet."); return
td = datetime.date.today()
for p in my:
left = (datetime.date.fromisoformat(p["end_date"]) - td).days
status = f"Active - {left} days left" if left >= 0 else "Expired"
print(f" Pass ID : {p['pass_id']}\n Type : {p['label']}\n Valid : {p['start_date']} to {p['end_date']}\n Status : {status}\n")