Untitled
unknown
python
5 months ago
8.1 kB
6
Indexable
import os import random def drawMenu(): print(" Train Ticket System ") print("____________________________") print("| (1) View Train Schedules |") print("----------------------------") print("| (2) Book Tickets |") print("----------------------------") print("| (3) View Bookings |") print("----------------------------") print("| (4) Cancel Tickets |") print("----------------------------") print("| (5) Exit Program |") print("----------------------------") def menuOptions(choice, trains, bookings, seat_s): if choice == "1": os.system("cls") viewTrainSchedules(trains) return elif choice == "2": os.system("cls") bookTickets(trains, bookings, seat_s) return elif choice == "3": os.system("cls") viewBookings() return elif choice == "4": os.system("cls") cancelTickets() return else: input("Invalid input! (Press any key to try again) >>") os.system("cls") return # Yik Yang def viewTrainSchedules(trains): # draw the view train schedules out while (True): print("| Available Train |") print("_________________________\n\n") print(f"| {"Train No":<12} | {"Name":<15} | {"Origin":<15} | {"Destination":<15} | {"Available Seat":<20} | {"Fare":<10} | {"Departure":<12} | {"Arrival":<12} |") print("----------------------------------------------------------------------------------------------------------------------------------------") for train in trains: print(f"| {train["Train No"]:<12} | {train["Name"]:<15} | {train["Origin"]:<15} | {train["Destination"]:<15} | {train["Available Seat"]:<20} | {train["Fare"]:<10} | {train["Departure"]:<12} | {train["Arrival"]:<12} |") break input("\nPress any key to Main Menu >> ") os.system("cls") # Li Hen def bookTickets(trains, bookings, seats): while (True): print("| Implement your book tickets here |") print("______________________________________\n\n") for train in trains: print(f"{train['Train No']} - {train['Name']} (Fare: {train['Fare']}, Available Seats: {train['Available Seat']})") # ticketsId = random.randint(100,1000) 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 train in trains: if train["Train No"] == book: selectedTrain = train break if not selectedTrain: input("\nInvalid Input. Please try again >> ") os.system("cls") continue os.system("cls") print(f"\nHere is the availability for {train["Name"]} ({selectedTrain["Train No"]})") print("-----------------------------------------------------------") for rows in seats: print(f"| {rows['A']:<5} | {rows['B']:<5} | {rows['C']:<5} | {rows['D']:<5} | {rows['E']:<5} | {rows['F']:<5} | {rows['G']:<5} | {rows['H']:<5} |") seatChoice = input("\nSelect a seat (e.g., A01, B04), or 0 to exit >> ").upper() if seatChoice == "0": break if not seatChoice: input("Something went wrong here, your seat selection might not be exist. >> ") os.system("cls") continue seatClass = seatChoice[0] seatNumber = seatChoice[1:] # Start at index 1 # Yik Yang ver # if not any(seatClass == seat for seat in['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']): # print("Invalid input") # continue # wj ver (succeed) invalidInput = False # validate user input to ensure the seatClass is input correctly for x in range(len(seats)): if seatClass not in seats[x].keys(): input("Something went wrong, your seat selection might not be exist. >> ") invalidInput = True break if invalidInput: os.system("cls") continue row = int(seatNumber) - 1 # Row index based on seat number (e.g., A01 -> row 0) # Yik Yang ver # if seat_s[rowIndex][seatClass] == seatNumber: # # Seat is available, book it # seat_s[rowIndex][seatClass] = "XXX" # print(f"Successfully booked seat {seatChoice}!") # ticketsId = f"{selectedTrain['Train No']} - {seatClass} - {len(bookings) + 1}" # bookings.append({ # "Train No": selectedTrain['Train No'], # "Seat Class": seatClass, # "Ticket ID": ticketsId # }) # print(f"Your Ticket ID is: {ticketsId}") # selectedTrain['Available Seat'] -= 1 # else: # print(f"Sorry, seat {seatChoice} is already booked.") # wj ver if seats[row][seatClass] == "XXX": print(f"Sorry, seat {seatChoice} is already booked.") else: seats[row][seatClass] = "XXX" if seats[row][seatClass] == "XXX": print(f"Successfully booked seat {seatChoice}!") ticketID = f"T-{selectedTrain["Train No"]}{seatClass}{len(bookings) + 1}" bookings.append({ "Train No": selectedTrain["Train No"], "Seat Class": seatClass, "Ticket ID": ticketID }) print(f"Your Ticket ID is: {ticketID}") selectedTrain["Available Seat"] -= 1 else: print("Something went wrong, please contact the server.") input("\nPress any key to Main Menu >> ") os.system("cls") # Malcolm def viewBookings(): print("Implement your view bookings here") # QinYi def cancelTickets(): print("Implement your cancel tickets here") # main entry point of the program def main(): # declare variables here trains = [ {"Train No": "101", "Name": "Express A", "Origin": "City A", "Destination": "City D" \ , "Available Seat": 48, "Fare": 50, "Departure": "08.00", "Arrival": "11.00"}, {"Train No": "102", "Name": "Express B", "Origin": "City B", "Destination": "City E" \ , "Available Seat": 48, "Fare": 100, "Departure": "22.00", "Arrival": "23.00"}, {"Train No": "103", "Name": "Express C", "Origin": "City C", "Destination": "City F" \ , "Available Seat": 48, "Fare": 200, "Departure": "18.00", "Arrival": "20.00"}, ] bookings = [] seats = [ {"A": "A01", "B": "B01", "C": "C01","D": "D01", "E": "E01", "F": "F01","G": "G01", "H": "H01"}, {"A": "A02", "B": "B02", "C": "C02","D": "D02", "E": "E02", "F": "F02","G": "G02", "H": "H02"}, {"A": "A03", "B": "B03", "C": "C03","D": "D03", "E": "E03", "F": "F03","G": "G03", "H": "H03"}, {"A": "A04", "B": "B04", "C": "C04","D": "D04", "E": "E04", "F": "F04","G": "G04", "H": "H04"}, {"A": "A05", "B": "B05", "C": "C05","D": "D05", "E": "E05", "F": "F05","G": "G05", "H": "H05"}, {"A": "A06", "B": "B06", "C": "C06","D": "D06", "E": "E06", "F": "F06","G": "G06", "H": "H06"}, ] # main loop while (True): drawMenu() choice = input("\nEnter your choice >> ") if choice == "5": os.system("cls") print("Thanks for using our service, bye!") break else: menuOptions(choice, trains, bookings, seats) # call the main() function main()
Editor is loading...
Leave a Comment