def promptAndSearchForFlights(self, multiple_passengers=False):
        """
        This menu is mainly used to handle sorting for flights. It calls promptForFlightDetails to get user input. It calls searchFlights to access the database and fetch the results for the search. From here, the user can sort by price or by connections (with price as a tie breaker) or access the booking options.

        It also implements round trips as per the extra functionality in the specifications, accessed through user input.
        """
        if multiple_passengers == True:
            num = self.promptNumPassengers()
        else:
            num = 1 

        acode_s, acode_d, date = self.promptForFlightDetails()

        flights = sf.searchFlights(acode_s, acode_d, date, num, displayable=True)

        if flights:
            self.printFlightData(flights, mentionPriceSorted=False)
            print("")
            sort = input("This is currently sorted by price alone.\n"
                         "To sort by connections, and then price, enter 'C'.\n"
                         "('R' -> Main Menu; 'B' -> Book Flight): ")
            while True:
                if sort == 'P':
                    flights = sf.searchFlights(acode_s, acode_d, date, num, displayable=True)
                    if flights:
                        self.printFlightData(flights, mentionPriceSorted=True)
                        sort = input("This is currently sorted by price alone.\n"
                                     "To sort by connections, and then price, enter 'C'.\n"
                                     "('R' -> Main Menu; 'B' -> Book Flight): ")
                elif sort == 'C': 
                    flights = sf.searchFlightsSortedByConnections(acode_s, acode_d, date, num, displayable=True)
                    if flights:
                        self.printFlightData(flights, mentionConnectionSorted=True)
                        print("")
                        sort = input("This is currently sorted by connections, then price.\n"
                                     "To sort by price only, enter 'P'.\n"
                                     "('R' -> Main Menu; 'B' -> Book Flight): \n")
                elif sort == 'B':
                    flights = sf.searchFlights(acode_s, acode_d, date, num, displayable=False)
                    self.bookingOptions(flights, acode_s, acode_d, date, num)    # go to booking option
                elif sort == 'R':
                    self.showMenu()
                else: sort = input("Not a valid option. Please try again: \n ")
        else: 
            print("No flights found, Try again.")
    def bookingOptions(self, searchResults, acode_s, acode_d, date, passengerCount=1):
        """
        For one-way trip and round trip options.
        """
        # get trip type
        print("")      
        if passengerCount == 1: # default setting
            tripType = input("Book one-way trip (1) or book round-trip (2): ")
        else:
            tripType = "1"

        # One way trip
        if tripType == "1":
            rowSelection = input("Please enter row number of trip you would like to book: ")
            
            l = list(searchResults[int(float(rowSelection)) - 1])
            l.append(date)
            flightDetails = tuple(l)

            # make a booking for each passenger
            for i in range(0,passengerCount):
                if (i == passengerCount-1): # the last passenger
                    self.makeABooking(flightDetails, passengerCount, oneOfMany=False)
                else: self.makeABooking(flightDetails, passengerCount, oneOfMany=True) 

        # Round trip
        elif tripType == "2":
            return_date = input("Please enter return date in format DD/MM/YYYY.\n")
            if not util.validate(return_date): # date is invalid. let's you try twice, then shows you the list of availble flights
                return_date = input("Please enter return date in format DD/MM/YYYY.\n")

            returnFlights = sf.searchFlights(acode_d, acode_s, return_date) # search for return flights

            if returnFlights:
                print ("Here are the return flights that match your query: ")
                
                self.printFlightData(returnFlights); 
                rowSelectionDepart = input("Please enter row number of departure flight from first table: ")
                rowSelectionReturn = input("Please enter row number of return flight from second table: ")

                # add some more flight details to tuple
                twoBookings = []
                l = list(searchResults[int(float(rowSelectionDepart))-1])
                l.append(date)
                flightDetails = tuple(l)
                twoBookings.append(flightDetails)
                l = list(returnFlights[int(float(rowSelectionReturn))-1])
                l.append(return_date)
                returnDetails = tuple(l)
                twoBookings.append(returnDetails)

                self.makeABooking(twoBookings, passengerCount, roundTrip=True) # Round trips are handled differently for bookings!
            else: 
                print("No return flights found, Try again.")

        else: # did not pick 1 or 2, prompt again
            self.bookingOptions(searchResults, acode_s, acode_d, date)
    def makeABooking(self, fullFlightDetails, num=1, roundTrip=False, user_name="",  oneOfMany=False):

        """
        Handles booking of flights. Default handles the core functionality as per specifications however, extra functionality is also handled in makeABooking for round trips and parties with size larger than one.
        """
        db = main.getDatabase()
        if roundTrip == True:
            flightDetails = fullFlightDetails[0]
        else: flightDetails = fullFlightDetails

        flightno, flightno2, src, dst, dep_time, arr_time, layover, numStops, fare1, fare2, price, seats, dep_date = flightDetails
        price = str(price)
        seat = self.generateSeatNumber()

        # get name of user, check if in passengers table
        user_name = self.verifyPassenger(user_name)
        tno = str(self.generateTicketNumber())

        # check if seat is still available by searching for the flights again
        flights = sf.searchFlights(src, dst, dep_date, num) 

        if len(flights) == 0:
            print("Sorry, this seat is no longer available. Please try another flight.")
            self.showMenu()
                  
        else: 
            # parties larger than one will go through this, as per the specifications
            if oneOfMany:
                cheap_flight = sf.getCheapestSpecificFlight(flightDetails)[0]
                flightno, flightno2, src, dst, dep_time, arr_time, layover, numStops, fare1, fare2, price, seats = cheap_flight
                print("The cheapest fare for your flight choice is ${0}".format(price))

            try: 
                # executing queries
                db.execute(INSERT_TICKET.format(tno, user_name, self.email, price))
                db.execute(INSERT_BOOKING.format(tno, flightno, fare1, dep_date, seat))
                db.execute("commit")  
                tix = tno   # for printing purposes
                if (flightno2 != None): # connecting flight, need two bookings
                   tno2 = str(self.generateTicketNumber())
                   tix = tno + ", " + tno2
                   seat2 = self.generateSeatNumber()
                   db.execute(INSERT_TICKET.format(tno2, user_name, self.email, price))
                   db.execute(INSERT_BOOKING.format(tno2, flightno2, fare2, dep_date, seat))
                   db.execute("commit")
                print("Your flight has been booked with the ticket number(s): " + tix + ". \n")
                if (roundTrip == True): # round trips will make another booking, user_name was already given
                    self.makeABooking(fullFlightDetails[1], num, False, user_name)  
            except cx_Oracle.DatabaseError as exc:
                error = exc.args
                print( sys.stderr, "Oracle code:", error.code)
                print( sys.stderr, "Oracle message:", error.message)
            if not oneOfMany: # go back to main menu
                self.showMenu()  
Exemple #4
0
    def promptAndSearchForFlights(self, multiple_passengers=False):
        """
        This menu is mainly used to handle sorting for flights. It calls promptForFlightDetails to get user input. It calls searchFlights to access the database and fetch the results for the search. From here, the user can sort by price or by connections (with price as a tie breaker) or access the booking options.

        It also implements round trips as per the extra functionality in the specifications, accessed through user input.
        """
        if multiple_passengers == True:
            num = self.promptNumPassengers()
        else:
            num = 1

        acode_s, acode_d, date = self.promptForFlightDetails()

        flights = sf.searchFlights(acode_s,
                                   acode_d,
                                   date,
                                   num,
                                   displayable=True)

        if flights:
            self.printFlightData(flights, mentionPriceSorted=False)
            print("")
            sort = input("This is currently sorted by price alone.\n"
                         "To sort by connections, and then price, enter 'C'.\n"
                         "('R' -> Main Menu; 'B' -> Book Flight): ")
            while True:
                if sort == 'P':
                    flights = sf.searchFlights(acode_s,
                                               acode_d,
                                               date,
                                               num,
                                               displayable=True)
                    if flights:
                        self.printFlightData(flights, mentionPriceSorted=True)
                        sort = input(
                            "This is currently sorted by price alone.\n"
                            "To sort by connections, and then price, enter 'C'.\n"
                            "('R' -> Main Menu; 'B' -> Book Flight): ")
                elif sort == 'C':
                    flights = sf.searchFlightsSortedByConnections(
                        acode_s, acode_d, date, num, displayable=True)
                    if flights:
                        self.printFlightData(flights,
                                             mentionConnectionSorted=True)
                        print("")
                        sort = input(
                            "This is currently sorted by connections, then price.\n"
                            "To sort by price only, enter 'P'.\n"
                            "('R' -> Main Menu; 'B' -> Book Flight): \n")
                elif sort == 'B':
                    flights = sf.searchFlights(acode_s,
                                               acode_d,
                                               date,
                                               num,
                                               displayable=False)
                    self.bookingOptions(flights, acode_s, acode_d, date,
                                        num)  # go to booking option
                elif sort == 'R':
                    self.showMenu()
                else:
                    sort = input("Not a valid option. Please try again: \n ")
        else:
            print("No flights found, Try again.")
Exemple #5
0
    def makeABooking(self,
                     fullFlightDetails,
                     num=1,
                     roundTrip=False,
                     user_name="",
                     oneOfMany=False):
        """
        Handles booking of flights. Default handles the core functionality as per specifications however, extra functionality is also handled in makeABooking for round trips and parties with size larger than one.
        """
        db = main.getDatabase()
        if roundTrip == True:
            flightDetails = fullFlightDetails[0]
        else:
            flightDetails = fullFlightDetails

        flightno, flightno2, src, dst, dep_time, arr_time, layover, numStops, fare1, fare2, price, seats, dep_date = flightDetails
        price = str(price)
        seat = self.generateSeatNumber()

        # get name of user, check if in passengers table
        user_name = self.verifyPassenger(user_name)
        tno = str(self.generateTicketNumber())

        # check if seat is still available by searching for the flights again
        flights = sf.searchFlights(src, dst, dep_date, num)

        if len(flights) == 0:
            print(
                "Sorry, this seat is no longer available. Please try another flight."
            )
            self.showMenu()

        else:
            # parties larger than one will go through this, as per the specifications
            if oneOfMany:
                cheap_flight = sf.getCheapestSpecificFlight(flightDetails)[0]
                flightno, flightno2, src, dst, dep_time, arr_time, layover, numStops, fare1, fare2, price, seats = cheap_flight
                print(
                    "The cheapest fare for your flight choice is ${0}".format(
                        price))

            try:
                # executing queries
                db.execute(
                    INSERT_TICKET.format(tno, user_name, self.email, price))
                db.execute(
                    INSERT_BOOKING.format(tno, flightno, fare1, dep_date,
                                          seat))
                db.execute("commit")
                tix = tno  # for printing purposes
                if (flightno2 != None):  # connecting flight, need two bookings
                    tno2 = str(self.generateTicketNumber())
                    tix = tno + ", " + tno2
                    seat2 = self.generateSeatNumber()
                    db.execute(
                        INSERT_TICKET.format(tno2, user_name, self.email,
                                             price))
                    db.execute(
                        INSERT_BOOKING.format(tno2, flightno2, fare2, dep_date,
                                              seat))
                    db.execute("commit")
                print(
                    "Your flight has been booked with the ticket number(s): " +
                    tix + ". \n")
                if (
                        roundTrip == True
                ):  # round trips will make another booking, user_name was already given
                    self.makeABooking(fullFlightDetails[1], num, False,
                                      user_name)
            except cx_Oracle.DatabaseError as exc:
                error = exc.args
                print(sys.stderr, "Oracle code:", error.code)
                print(sys.stderr, "Oracle message:", error.message)
            if not oneOfMany:  # go back to main menu
                self.showMenu()
Exemple #6
0
    def bookingOptions(self,
                       searchResults,
                       acode_s,
                       acode_d,
                       date,
                       passengerCount=1):
        """
        For one-way trip and round trip options.
        """
        # get trip type
        print("")
        if passengerCount == 1:  # default setting
            tripType = input("Book one-way trip (1) or book round-trip (2): ")
        else:
            tripType = "1"

        # One way trip
        if tripType == "1":
            rowSelection = input(
                "Please enter row number of trip you would like to book: ")

            l = list(searchResults[int(float(rowSelection)) - 1])
            l.append(date)
            flightDetails = tuple(l)

            # make a booking for each passenger
            for i in range(0, passengerCount):
                if (i == passengerCount - 1):  # the last passenger
                    self.makeABooking(flightDetails,
                                      passengerCount,
                                      oneOfMany=False)
                else:
                    self.makeABooking(flightDetails,
                                      passengerCount,
                                      oneOfMany=True)

        # Round trip
        elif tripType == "2":
            return_date = input(
                "Please enter return date in format DD/MM/YYYY.\n")
            if not util.validate(
                    return_date
            ):  # date is invalid. let's you try twice, then shows you the list of availble flights
                return_date = input(
                    "Please enter return date in format DD/MM/YYYY.\n")

            returnFlights = sf.searchFlights(
                acode_d, acode_s, return_date)  # search for return flights

            if returnFlights:
                print("Here are the return flights that match your query: ")

                self.printFlightData(returnFlights)
                rowSelectionDepart = input(
                    "Please enter row number of departure flight from first table: "
                )
                rowSelectionReturn = input(
                    "Please enter row number of return flight from second table: "
                )

                # add some more flight details to tuple
                twoBookings = []
                l = list(searchResults[int(float(rowSelectionDepart)) - 1])
                l.append(date)
                flightDetails = tuple(l)
                twoBookings.append(flightDetails)
                l = list(returnFlights[int(float(rowSelectionReturn)) - 1])
                l.append(return_date)
                returnDetails = tuple(l)
                twoBookings.append(returnDetails)

                self.makeABooking(
                    twoBookings, passengerCount, roundTrip=True
                )  # Round trips are handled differently for bookings!
            else:
                print("No return flights found, Try again.")

        else:  # did not pick 1 or 2, prompt again
            self.bookingOptions(searchResults, acode_s, acode_d, date)