Untitled
unknown
python
a year ago
8.6 kB
8
Indexable
from tabulate import tabulate
import os
import random
# global variables
g_destinationList = ["Perlis", "Kedah", "Penang", "Perak", "Selangor", "Negeri Sembilan", "Melaka", "Johor", "Kelantan", "Terengganu", "Pahang"]
def refresh():
print("\n\n\n\n\n\n\n\n\n\n")
def destinationRandomize():
destination = random.choice(g_destinationList)
g_destinationList.remove(destination)
return destination
def fareRandomize():
fare = random.randint(1,4)
return fare * 50
def drawMenu():
menuChoice = [ ["(1) View Train Schedules"],
["(2) Book Tickets"],
["(3) View Bookings"],
["(4) Cancel Tickets"],
["(5) Exit Program"]
]
menuTable = tabulate(
menuChoice,
tablefmt="fancy_grid",
headers=[" Train Ticket System"]
)
refresh()
print(menuTable)
def menuOptions(choice, trains, bookings, seats, table):
if choice == "1":
os.system("cls")
viewTrainSchedules(table)
return
elif choice == "2":
os.system("cls")
bookTickets(trains, bookings, seats, table)
return
elif choice == "3":
os.system("cls")
viewBookings(bookings)
return
elif choice == "4":
os.system("cls")
cancelTickets(bookings, seats)
return
else:
input("Invalid input! (Press any key to try again) >>")
os.system("cls")
return
# Yik Yang
def viewTrainSchedules(table):
refresh()
print(tabulate([["Train Schedules"]], tablefmt="rounded_grid"))
print()
# draw the view train schedules out
while (True):
print(table)
break
input("\nPress any key to Main Menu >> ")
os.system("cls")
# Li Hen
def bookTickets(trains, bookings, seats, table):
while (True):
refresh()
print(tabulate([["Available Trains to Book"]], tablefmt="rounded_grid"))
print()
print(table)
book = input("\nEnter Train Number (101, 102, 103) to view seats, or 0 to exit >> ")
if book == "0":
os.system("cls")
break
selectedTrain = None
for x in range(len(trains) - 1):
if trains[x][0] == book:
selectedTrain = trains[x][0]
break
if not selectedTrain:
input("\nInvalid Input. Please try again >> ")
os.system("cls")
continue
os.system("cls")
refresh()
print(tabulate([[f"\nHere are the available seats for {trains[x][1]} Express (Train No: {selectedTrain})"]], tablefmt="rounded_grid"))
print("\n")
# draw the seats
seatAboveTable = tabulate(
seats[0],
tablefmt="heavy_grid"
)
print(seatAboveTable)
print("--> Walk way")
seatBelowTable = tabulate(
seats[1],
tablefmt="heavy_grid"
)
print(seatBelowTable)
seatChoice = input("\nSelect a seat (e.g., A01, B04), or 0 to exit >> ").upper()
if seatChoice == "0":
os.system("cls")
break
if not seatChoice:
input("Something went wrong here, your seat selection might not be exist. >> ")
os.system("cls")
continue
if len(seatChoice) < 3 or len(seatChoice) > 3:
input("Something went wrong here, your seat selection might not be exist. >> ")
os.system("cls")
continue
input("\nPress any key to Main Menu >> ")
os.system("cls")
# Malcolm
def viewBookings(bookings):
while True:
refresh()
if not bookings:
print(tabulate([["You don't have any booking yet."]], tablefmt="rounded_grid"))
input("\nPress any key to Main Menu >> ")
os.system("cls")
return
else:
print("These are the ticket ID you had purchased.")
for i in range(len(bookings)):
print(f"-> ({i + 1}) {bookings[i]["Ticket ID"]}")
# enter a Booking ID
try:
choice = int(input(f"\nInput 1 to {len(bookings)} to view corresponding details, or 0 to exit >> "))
except ValueError:
input("Invalid input, please try again >> ")
os.system("cls")
continue
if choice == 0:
os.system("cls")
return
# if input out of boundary, throw exception
if choice > len(bookings):
input(f"Invalid input, please input from 1 to {len(bookings)} >> ")
os.system("cls")
continue
# Display the booking details if found
else:
os.system("cls")
print("\n Booking Details ")
print("_________________________\n\n")
print(f"| {'Train No':<10} | {'Seat Class':<12} | {'Ticket ID':<15} |")
print("-------------------------------------------------")
print(f"| {bookings[choice - 1]['Train No']:<10} | {bookings[choice - 1]['Seat Class']:<12} | {bookings[choice - 1]['Ticket ID']:<15} |")
input("\nPress any key to Main Menu >> ")
os.system("cls")
return
# Qinyi
def cancelTickets(bookings, seats):
while True:
refresh()
print(tabulate([["Cancel Your Ticket(s) Here"]], tablefmt="rounded_grid"))
if not bookings:
print("You haven't purchased your ticket yet.")
input("Enter any key to main menu >> ")
os.system("cls")
break
else:
print("Here is the ticket that you purchased.")
print(f"(1) {bookings[0]["Ticket ID"]}")
choice = input("Enter the number to cancel : ")
if choice == "1":
answer = input("Are you sure you want to cancel? (YES/NO) >> ").upper()
if answer == "YES":
input(f"Ticket ID {bookings[0]["Ticket ID"]} has been cancelled successfully.")
del bookings[0]
os.system("cls")
elif answer == "NO":
os.system("cls")
# main entry point of the program
def main():
# declare variables here
fareList = [fareRandomize(), fareRandomize(), fareRandomize()]
tierList = ["Gold" , "Silver", "Platinum"]
# check train tier list
for i in range(len(fareList) - 1):
if fareList[i] < 100:
tierList[i] = "Silver"
elif fareList[i] < 150:
tierList[i] = "Gold"
else:
tierList[i] = "Platinum"
trains = [ ["101", f"{tierList[0]}", f"{destinationRandomize()}", f"{destinationRandomize()}", 48, f"RM {fareList[0]}", "8.00am", "11.00am"],
["102", f"{tierList[1]}", f"{destinationRandomize()}", f"{destinationRandomize()}", 48, f"RM {fareList[1]}", "22.00pm", "23.00pm"],
["103", f"{tierList[2]}", f"{destinationRandomize()}", f"{destinationRandomize()}", 48, f"RM {fareList[2]}", "18.00pm", "20.00pm"]
]
table = tabulate(
trains,
headers=["Train No", "Train Type", "Origin", "Destination", "Available Seat", "Fare/Pax", "Departure", "Arrival"],
tablefmt="fancy_grid",
numalign="right",
colalign=("center", "center", "center", "center", "center", "center", "center")
)
bookings = []
seatsAbove = [ ["A01", "B01", "C01", "D01", "E01", "F01", "G01", "H01"],
["A02", "B02", "C02", "D02", "E02", "F02", "G02", "H02"]
]
seatsBelow = [ ["A03", "B03", "C03", "D03", "E03", "F03", "G03", "H03"],
["A04", "B04", "C04", "D04", "E04", "F04", "G04", "H04"]
]
seats = [seatsAbove, seatsBelow]
# main loop
while (True):
drawMenu()
choice = input("\nEnter your choice >> ")
if choice == "5":
os.system("cls")
refresh()
print(tabulate([["Thanks for using our service, bye!"]], tablefmt="rounded_grid"))
break
else:
menuOptions(choice, trains, bookings, seats, table)
# call the main() function
main()Editor is loading...
Leave a Comment