def main(): #MAIN FUNCTION OF THE PROGRAM movieList = [] #Main movie list of the program userList = [] #Main user list of the programs shouldExit = False #Set to true to exit loop initializationText = """ Hello! What would you like to do? User menu: 1. Add a user 2. Change a user's name 3. Remove a user Movie menu: 4. Add a movie 5. Update a movie's price 6. List all movies Miscellaneous menu: 7. Order a movie 8. List user's movie orders 9. Search movies by rating 10. Filter movies by actors 0. Exit program """ #Run Unit tests TestClass.RunTests() #The main lists of the program movieList = [] userList = [] #Populate the user list from the database try: userInfile = open("userDatabase", "rb") except: print("No user database found!") else: while True: try: userList.append(UserManager.DictionaryToUser(pickle.load(userInfile))) except: break #Populate the movie list from the database try: movieInfile = open("movieDatabase", "rb") except: print("No movie database found!") else: while True: try: movieList.append(MovieManager.DictionaryToMovie(pickle.load(movieInfile))) except: break while shouldExit == False: #MAIN LOOP OF THE PROGRAM #Print the user menu print(initializationText) #Get user input try: userChoice = int( input("Your choice: ") ) except ValueError: print("Choice is not an integer!") continue if userChoice == 0: #Exits program #Save the users to the database userOutfile = open("userDatabase", "wb") for user in userList: dict = UserManager.UserToDictionary(user) pickle.dump(dict, userOutfile) userOutfile.close() #Save the movies to the database movieOutfile = open("movieDatabase", "wb") for movie in movieList: dict = MovieManager.MovieToDictionary(movie) pickle.dump(dict, movieOutfile) movieOutfile.close() print("Goodbye!") shouldExit = True elif userChoice == 1: #Add user option newUser = UserManager.getUserInput() userList = UserManager.addUser(userList, newUser) elif userChoice == 2: #Change a user's name userList = UserManager.updateLastname(userList) elif userChoice == 3: #Remove a user #Check the list isn't empty if len(userList) == 0: print("The user list is empty!") continue UserManager.printUsers(userList) try: removeUserChoice = int(input("Which user would you like to remove? Index: ")) except ValueError: print("Choice is not an integer!") continue #Clamp the value of the index between the min and the max indexes if removeUserChoice < 0: removeUserChoice = 0 elif removeUserChoice > len(userList) - 1: removeUserChoice = len(userList) - 1 #Remove the selected user del userList[removeUserChoice] elif userChoice == 4: #Add a movie newMovie = MovieManager.getMovieInput() movieList = MovieManager.addMovie(movieList, newMovie) elif userChoice == 5: #Update a movie's price movieList = MovieManager.updatePrice(movieList) elif userChoice == 6: #List all movies MovieManager.printMovies(movieList) elif userChoice == 7: #Order a movie userList = MiscManager.orderMovies(userList, movieList) elif userChoice == 8: #List all user's movie orders UserManager.printUsers(userList) elif userChoice == 9: #Search all movies by rating MiscManager.searchMoviesByRating(movieList) elif userChoice == 10: #Filter movies by actors MiscManager.searchMoviesByActor(movieList)
def orderMovies(userList, movieList): """ Add an order to a user's order list. Print the final price In: List of users, List of movies Out: List of users """ localUserList = userList.copy() localMovieList = movieList.copy() #Check if the lists are empty if len(localMovieList) == 0: print("The movie list is empty!") return localUserList if len(localUserList) == 0: print("The user list is empty!") return localUserList #USER PART UserManager.printUsers(localUserList) #Get the user for which to place the order while True: try: userChoice = int( input( "Select the user for which to place the order; Index:") ) except ValueError: print("Choice is not an integer!") else: break #Clamp the index between the min and max indexes if userChoice < 0: userChoice = 0 elif userChoice > len(localUserList) - 1: userChoice = len(localUserList) - 1 selectedUser = localUserList[userChoice] #MOVIE PART MovieManager.printMovies(localMovieList) totalPrice = 0 currentOrder = [] while True: try: nrOfMovies = int( input("How many movies would you like to order? ")) except ValueError: print("Choice is not an integer!") else: break for i in range(nrOfMovies): while True: try: movieIndex = int( input("Which movie would you like to order? Index: ")) except ValueError: print("Choice is not an integer!") else: break #Clamp the index between the min and max indexes if movieIndex < 0: movieIndex = 0 elif movieIndex > len(localMovieList) - 1: movieIndex = len(localMovieList) - 1 currentOrder.append(localMovieList[movieIndex].title) totalPrice += localMovieList[movieIndex].price userOrders = selectedUser.orders.copy() userOrders.append(currentOrder) localUserList[userChoice].orders = userOrders print("Total price of the order: " + str(totalPrice)) return localUserList