Untitled

 avatar
unknown
python
5 months ago
12 kB
5
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"]
g_ticketIDGenerator = random.randint(100,999)

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):
    if choice == "1":
        os.system("cls")
        viewTrainSchedules(table)
        return
    elif choice == "2":
        os.system("cls")
        bookTickets(trains, bookings, trainsSeats, table)
        return
    elif choice == "3":
        os.system("cls")
        viewBookings(bookings)
        return
    elif choice == "4":
        os.system("cls")
        cancelTickets(bookings, trainsSeats)
        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
    print(table)
    
    input("\nPress any key to Main Menu >> ")
    os.system("cls")

# Li Hen
def bookTickets(trains, bookings, trainsSeats, table):
    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
        for no in range(len(trains) - 1):
            if trains[no][0] == book:
                selectedTrain = trains[no][0]
                break 
        
        if not selectedTrain:
            input("\nInvalid Input. Please try again >> ")
            os.system("cls")
            continue

        pax = 0
        while True:
            pax = input("How many person? >> ")

            if pax <= "0" or pax > "48":
                print("Your pax is either too much or too less, try again.")
                continue

            if pax.isdigit():
                pax = int(pax)
                break
            else:
                print("Your pax is not valid, try again.")
                continue
        
        isBooked = False
        while pax > 0:
            refresh()
            print(tabulate([[f"\nHere are the available seats for {trains[no][1]} Express (Train No: {selectedTrain})"]], tablefmt="rounded_grid"))
            print()

            # draw the seats
            seatAboveTable = tabulate(
                trainsSeats[no][0],
                tablefmt="heavy_grid"
            )
            print(seatAboveTable)
            print("--> Walk way")

            seatBelowTable = tabulate(
                trainsSeats[no][1],
                tablefmt="heavy_grid"
            )
            print(seatBelowTable)
            print(f"Total pax that haven't choose 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")
                break
            
            if not seatChoice:
                print("Something went wrong here, your seat selection might not be exist. >> ")
                continue

            if len(seatChoice) < 3 or len(seatChoice) > 3:
                print("Something went wrong here, the range is in between (A-H) and (01-04) >> ")
                continue
            
            # get the index of the seat
            row = int(seatChoice[-1]) - 1
            col = alphabetToInt(seatChoice[0])

            if row < 0 or row > 4:
                input("The number of seat is only between(1-4), please try again. (Press any key to re-enter again)")
                continue

            if col == -1:
                input("The alphabet is not in our seat list, please try again with valid alphabet. (Press any key to re-enter again)")
                continue
            

            if row == 0 or row == 1:
                if trainsSeats[no][0][row][col] == "XXX":
                    print("The seat is already booked, please choose another available seat.")
                    continue
                
                trainsSeats[no][0][row][col] = "XXX"
                pax -= 1
                input(f"The seat {seatChoice} is booked successfully.")
                isBooked = True

            elif row == 2 or row == 3:
                if trainsSeats[no][1][row - 2][col] == "XXX":
                    print("The seat is already booked, please choose another available seat.")
                    continue
                
                trainsSeats[no][1][row - 2][col] = "XXX"
                pax -= 1
                input(f"The seat {seatChoice} is booked successfully.")
                isBooked = True
            
        
        if pax == 0 and isBooked:
            print()
            print("Generating your receipt (Ticket ID)...")
            ticketID = f"{trains[no][1]} Express {selectedTrain} - {g_ticketIDGenerator} {totalGeneratedTicketID}"
            totalGeneratedTicketID += 1
            bookings.append(ticketID)
            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

     
# 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()}", 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 == "5":
            os.system("cls")
            refresh()
            print(tabulate([["Thanks for using our service, bye!"]], tablefmt="rounded_grid"))
            break
        else:
            menuOptions(choice, trains, bookings, trainsSeats, table)

# call the main() function
main()
Editor is loading...
Leave a Comment