Untitled
unknown
python
a year ago
14 kB
6
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 updateTable(trains):
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")
)
return table
def alphabetToInt(alphabet):
if alphabet == "A":
return 0
elif alphabet == "B":
return 1
elif alphabet == "C":
return 2
elif alphabet == "D":
return 3
elif alphabet == "E":
return 4
elif alphabet == "F":
return 5
elif alphabet == "G":
return 6
elif alphabet == "H":
return 7
else:
return -1 # if the user input the alphabet is not in our seat, throw error
def templateSeats():
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]
return seats
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, trainsSeats, table, fareList):
if choice == "1":
os.system("cls")
viewTrainSchedules(trains)
return
elif choice == "2":
os.system("cls")
bookTickets(trains, bookings, trainsSeats, table, fareList)
return
elif choice == "3":
os.system("cls")
viewBookings(bookings)
return
elif choice == "4":
os.system("cls")
cancelTickets(bookings, trainsSeats)
return
else:
while choice not in ["1", "2", "3", "4"]:
choice = input("Invalid input! (Press any key to try again) >>")
# Yik Yang
def viewTrainSchedules(trains):
refresh()
print(tabulate([["Train Schedules"]], tablefmt="rounded_grid"))
print()
# draw the view train schedules out
print(updateTable(trains))
input("\nPress any key to Main Menu >> ")
os.system("cls")
# Li Hen
def bookTickets(trains, bookings, trainsSeats, table, fareList):
totalGeneratedTicketID = 1
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")
return
selectedTrain = None
trainIndex = None # Index of the selected train
for no in range(len(trains)):
if trains[no][0] == book:
selectedTrain = trains[no][0]
trainIndex = no
break
while not selectedTrain:
book = input("Enter Train Number (101, 102, 103) to view seats, or 0 to exit >> ")
if book == "0":
os.system("cls")
return
selectedTrain = None
trainIndex = None # Index of the selected train
for no in range(len(trains)):
if trains[no][0] == book:
selectedTrain = trains[no][0]
trainIndex = no
break
pax = 0
while True:
pax = input("How many people? >> ")
if pax.isdigit() and 0 < int(pax) <= trains[trainIndex][4]:
pax = int(pax)
break
else:
print("Invalid input! The number must be between 1 and 32. Try again.")
continue
fare = fareList[trainIndex] * pax
isBooked = False
bookedSeats = [] # Keep track of seats booked in this session
while pax > 0:
refresh()
print(tabulate([[f"\nHere are the available seats for {trains[trainIndex][1]} Express (Train No: {selectedTrain})"]], tablefmt="rounded_grid"))
print()
# Draw the seats
seatAboveTable = tabulate(
trainsSeats[trainIndex][0],
tablefmt="heavy_grid"
)
print(seatAboveTable)
print("--> Walk way")
seatBelowTable = tabulate(
trainsSeats[trainIndex][1],
tablefmt="heavy_grid"
)
print(seatBelowTable)
print(f"Total pax that haven't chosen a seat: {pax}")
seatChoice = input("\nSelect a seat (e.g., A01, B04) per pax at a time, or 0 to exit >> ").upper()
if seatChoice == "0":
os.system("cls")
return
while len(seatChoice) != 3 or not seatChoice[1:].isdigit():
seatChoice = input("Invalid format, select a seat (e.g., A01, B04) per pax at a time, or 0 to exit >> ").upper()
if seatChoice == "0":
os.system("cls")
return
row = int(seatChoice[-1]) - 1
col = alphabetToInt(seatChoice[0])
while row < 0 or row >= 4 or col == -1:
seatChoice = input("Invalid format, the range is in between (A-H) and (01-04) >> ").upper()
row = int(seatChoice[-1]) - 1
col = alphabetToInt(seatChoice[0])
if row < 2: # Upper rows
if trainsSeats[trainIndex][0][row][col] == "XXX":
input("The seat is already booked. Please choose another available seat.")
continue
trainsSeats[trainIndex][0][row][col] = "XXX"
else: # Lower rows
if trainsSeats[trainIndex][1][row - 2][col] == "XXX":
input("The seat is already booked. Please choose another available seat.")
continue
trainsSeats[trainIndex][1][row - 2][col] = "XXX"
bookedSeats.append((row, col)) # Track booked seat
pax -= 1
input(f"The seat {seatChoice} has been booked successfully.")
isBooked = True
ticketIDGenerator = random.randint(100,999)
if pax == 0 and isBooked:
print(f"Your total payment is: RM{fare} (RM{fareList[trainIndex]} per pax)")
print()
confirmation = input("Are you confirm with your booking seats? (Y/N) >> ").upper()
while confirmation not in ["Y", "N", "YES", "NO"]:
confirmation = input("Invalid input. Are you confirm with your booking seats? (Y/N) >> ").upper()
if confirmation in ["Y", "YES"]:
person = int(fare / fareList[trainIndex])
print("\nGenerating your receipt (Ticket ID)...")
ticketID = f"{trains[trainIndex][1]} Express {selectedTrain} - {int(fare / fareList[trainIndex])}{ticketIDGenerator}{totalGeneratedTicketID}"
totalGeneratedTicketID += 1
bookings.append(ticketID)
trains[trainIndex][4] -= person
print()
print("Your receipt (Ticket ID) is generated successfully. Please head to 'View Bookings' to check your details.")
input("Press any key to main menu >> ")
return
else: # Cancel the booking
print("Booking canceled. Reverting seat availability...")
for seat in bookedSeats:
row, col = seat
if row < 2:
trainsSeats[trainIndex][0][row][col] = f"{chr(65 + col)}{str(row + 1).zfill(2)}"
else:
trainsSeats[trainIndex][1][row - 2][col] = f"{chr(65 + col)}{str(row + 1).zfill(2)}"
input("Seats have been restored. Returning to the main menu. Press any key to continue >> ")
os.system("cls")
return
# 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(tabulate([["These are the detail(s) about your recent booking"]], tablefmt="rounded_grid"))
for i in range(len(bookings)):
print(f"-> ({i + 1}) {bookings[i]}")
# 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")
refresh()
print(tabulate([[f"Booking Details of {bookings[choice - 1]}"]], tablefmt="rounded_grid"))
print()
input("Press 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()}", 32, f"RM {fareList[0]}", "8.00am", "11.00am"],
["102", f"{tierList[1]}", f"{destinationRandomize()}", f"{destinationRandomize()}", 32, f"RM {fareList[1]}", "22.00pm", "23.00pm"],
["103", f"{tierList[2]}", f"{destinationRandomize()}", f"{destinationRandomize()}", 32, 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")
)
trainsSeats = [ templateSeats(), templateSeats(), templateSeats() ]
bookings = []
# main loop
while (True):
drawMenu()
choice = input("\nEnter your choice >> ")
if choice == "1":
os.system("cls")
viewTrainSchedules(trains)
elif choice == "2":
os.system("cls")
bookTickets(trains, bookings, trainsSeats, table, fareList)
elif choice == "3":
os.system("cls")
viewBookings(bookings)
elif choice == "4":
os.system("cls")
cancelTickets(bookings, trainsSeats)
elif choice == "5":
os.system("cls")
refresh()
print(tabulate([["Thanks for using our service, bye!"]], tablefmt="rounded_grid"))
# call the main() function
main()Editor is loading...
Leave a Comment