Untitled
unknown
python
5 months ago
9.8 kB
1
Indexable
import os import random def destinationRandomize(): destinationList = ["Perlis", "Kedah", "Penang", "Perak", "Selangor", "Negeri Sembilan", "Melaka", "Johor", "Kelantan", "Terengganu", "Pahang", "Sabah", "Sarawak"] destination = random.choice(destinationList) return destination 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(bookings) 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":<13} | {"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} | RM {train["Fare"]:<10.2f} | {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") # ticketPrice age = int(input("Enter the age of the passenger")) print("This is the price of the ticket") childFare = 2 * fare adultFare = 3 * fare seniorFare = 2 * fare if age < 6: print(f"Total Payment: {childFare} )") elif age > 6 or age < 60: print(f"Total Payment: {adultFare} )") elif age > 60 or age < 200: print(f"Total Payment: {seniorFare} )") else: print("invalid input") 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": 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: 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 # 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) 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(bookings): while True: if not bookings: print("No bookings found.") 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(): 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": f"{destinationRandomize()}", "Destination": f"{destinationRandomize()}" \ , "Available Seat": 48, "Fare": 50, "Departure": "08.00", "Arrival": "11.00"}, {"Train No": "102", "Name": "Express B", "Origin": f"{destinationRandomize()}", "Destination": f"{destinationRandomize()}" \ , "Available Seat": 48, "Fare": 100, "Departure": "22.00", "Arrival": "23.00"}, {"Train No": "103", "Name": "Express C", "Origin": f"{destinationRandomize()}", "Destination": f"{destinationRandomize()}" \ , "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