// Function that takes a member and movie object and removes the movie // from the members movie collection if the movie exists. // No output. public void RemoveMovieFromMember(Member member, Movie movie) { // Flag used to check if movie is currently already rented out. bool check = false; // Check to see if movie is an object or null based off parameter input. if (movie != null) { // Use CheckMovie function to check if the member current has // the movie rented out check = CheckMovie(member, movie); // If they do have the movie rented out, remove from their // collection. if (check) { member.RemoveBorrowedMovie(movie); movie.IncreaseCopiesAvail(1); Console.WriteLine(movie.GetTitle() + " returned to database by " + member.GetUsername() + "."); } else { Console.WriteLine("You do not have that movie currently rented out."); } } else { Console.WriteLine("The movie you entered does not exist."); } }
// Function that takes a member and movie object and adds the movie to // members movie collection if the movie exists. // No output. public void Insert2Member(Member member, Movie movie) { // Flag used to check if movie is currently already rented out. bool check = false; // Check to see if movie is an object or null based off parameter input. if (movie != null) { // Use CheckMovie function to check if the member current has // the movie rented out check = CheckMovie(member, movie); // If they do not currently have this movie rented out, add to // their collection. if (!check) { // Check to see if there are enough copies available. // there is, use the member function to add the borrowed // movie to its own movie collection. // Decrease amount of copies available and increase times rented. if (movie.GetCopiesAvail() >= 1) { check = member.AddBorrowedMovie(movie); if (check) { movie.DecreaseCopiesAvail(); movie.IncreaseTimesRented(); Console.WriteLine(movie.GetTitle() + " rented to user " + member.GetUsername()); } } else { Console.WriteLine("There are no copies of that " + "movie available"); } } else { Console.WriteLine("You already currently have that movie rented."); } } else { Console.WriteLine("The movie you entered does not exist."); } }
// Helper function to check if movie is currently rented out by a // member. public bool CheckMovie(Member member, Movie movie) { bool check = false; // Grab the current movies borrwed by the member entered // into parameters. Movie[] movies = member.GetBorrowedMovies(); // If user has this movie in their current borrowed movies, // mark check as true if they do. foreach (Movie mov in movies) { if (mov.GetTitle() == movie.GetTitle()) { check = true; return(check); } } return(check); }
// Function that controls user input when prompting user to enter new // movie details. public static void Insert(MovieCollection.MovieTree BST) { Console.WriteLine(); Console.WriteLine(); Console.WriteLine("----ENTER MOVIE DETAILS----"); Console.WriteLine(); // Create flag used to check user input. bool correct = false; // Prompt user to enter movie title Console.Write("Enter movie title: "); string movieTitle = Console.ReadLine();; // Enter movie title. while (!correct) { correct = CheckInput(movieTitle, 0); if (!correct) { Console.Write("Enter movie title: "); movieTitle = Console.ReadLine(); } } // Check to see if the movie exists // in the BST before creating a new copy. Movie movieExists = BST.ReturnMovie(BST.ReturnRoot(), movieTitle); // If the movie exists, prompt user to enter an amount to add to database. if (movieExists != null) { Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Movie already" + " exists in database: How many" + " copies do you want to add?"); correct = false; int n = 0; correct = int.TryParse(Console.ReadLine(), out int movieAmount); // Check user input while (!correct || movieAmount <= 0 || movieAmount >= 1000) { if (!correct || movieAmount <= 0 || movieAmount >= 1000) { Console.Write("Please enter a number for copies " + "between 1 and 1000: "); } correct = int.TryParse(Console.ReadLine(), out movieAmount); } n = movieAmount; Console.WriteLine(); Console.WriteLine(); // Increase copies available by number movieExists.IncreaseCopiesAvail(n); Console.WriteLine(movieExists.GetTitle() + " copies increased by " + n); } // If movie doesnt exist prompt user for input about movie. else { // Check user input for actors Console.WriteLine(); correct = false; Console.Write("Enter starring actors: "); string starring = Console.ReadLine(); while (!correct) { correct = CheckInput(starring, 1); if (!correct) { Console.Write("Enter starring actors: "); starring = Console.ReadLine(); } } // Check user input for directors Console.WriteLine(); correct = false; Console.Write("Enter movie director: "); string director = Console.ReadLine(); while (!correct) { correct = CheckInput(director, 1); if (!correct) { Console.Write("Enter movie director: "); director = Console.ReadLine(); } } // Check user input for genre Console.WriteLine(); correct = false; string genre = "empty"; Console.WriteLine("Choose Genre: \n" + "1. Drama\n" + "2. Family\n" + "3. Action\n" + "4. SciFi\n" + "5. Comedy\n" + "6. Animated\n" + "7. Thriller\n" + "8. Other\n" + "Enter Number between 1-8: "); correct = int.TryParse(Console.ReadLine(), out int num); while (!correct || num <= 0 || num >= 9) { Console.WriteLine("Please enter a number between 1 and 8."); correct = int.TryParse(Console.ReadLine(), out num); } // Set genre based off switch case switch (num) { case 1: genre = "Drama"; break; case 2: genre = "Family"; break; case 3: genre = "Action"; break; case 4: genre = "SciFi"; break; case 5: genre = "Comedy"; break; case 6: genre = "Animated"; break; case 7: genre = "Thriller"; break; case 8: genre = "Other"; break; default: break; } // Check user input for classification Console.WriteLine(); correct = false; string classification = "empty"; Console.Write("Enter classification: \n" + "1. G\n" + "2. PG\n" + "3. M 15+\n" + "4. MA 15+\n" + "5. R 18+\n" + "Enter Number between 1-5: "); correct = int.TryParse(Console.ReadLine(), out num); while (!correct || num <= 0 || num >= 6) { Console.WriteLine("Please enter a number between 1 and 5."); correct = int.TryParse(Console.ReadLine(), out num); } // Set classification based off switch case switch (num) { case 1: classification = "G"; break; case 2: classification = "PG"; break; case 3: classification = "M 15+"; break; case 4: classification = "MA 15+"; break; case 5: classification = "R 18+"; break; default: break; } // Check user input for duration Console.WriteLine(); correct = false; int duration = 0; Console.Write("Enter duration: "); correct = int.TryParse(Console.ReadLine(), out num); while (!correct || num <= 0 || num >= 600) { Console.WriteLine("Please enter a number for duration " + "- 1.30 hr = 90 min for example."); Console.WriteLine(); Console.Write("Enter duration: "); correct = int.TryParse(Console.ReadLine(), out num); } duration = num; // Check user input for date Console.WriteLine(); DateTime releaseDate = DateTime.Now; correct = false; while (!correct) { try { Console.Write("Enter release date (dd/mm/yyyy): "); releaseDate = DateTime.Parse(Console.ReadLine()); correct = true; } catch (FormatException) { Console.WriteLine("Wrong date and time format - please enter dd/mm/yyyy"); Console.WriteLine(); } } // Check user input for amount of copies Console.WriteLine(); correct = false; int copiesAvail = 1; Console.Write("Enter amount of copies: "); correct = int.TryParse(Console.ReadLine(), out num); while (!correct || num <= 0 || num >= 1000) { Console.WriteLine("Please enter a number for copies " + "between 1 and 1000."); Console.WriteLine(); Console.Write("Enter amount of copies: "); correct = int.TryParse(Console.ReadLine(), out num); } copiesAvail = num; //Create new movie and it to BST tree. Console.WriteLine(); Movie movie = new Movie(movieTitle, starring , director, duration, genre, classification, releaseDate, copiesAvail, 0); Console.WriteLine("New movie " + movie.GetTitle() + " added to database."); BST.Insert(movie); } }
// Function that adds the movie object into the BST. public void Insert(Movie movie) { // Setup a new TreeNode to add to BST. TreeNode newTreeNode = new TreeNode(); // Assign the movie object to the movie object in the TreeNode. newTreeNode.movie = movie; bool exit = false; // If there is no root in the BST, set the root to the new tree // node. // Increment the count of movies in the tree. if (root == null) { root = newTreeNode; movieCount++; } else { // Setup a new treeNode for the current node and parent node // to start comparisons when travelling in tree. TreeNode current = root; TreeNode parent; while (!exit) { // Assign parent node to the current node being compared. parent = current; // If the comparison returns -1, traverse left side of // current node. if (string.Compare (movie.GetTitle(), current.movie.GetTitle()) == -1) { // Assign the left node of the current node to the // current variable current = current.left; //If its empty add at this location if (current == null) { // Add treeNode to parent of current. parent.left = newTreeNode; // Increase count of movies in BST. movieCount++; exit = true; } } else { //Assign the right node of the current node to the // current variable current = current.right; //If its empty add at this location if (current == null) { // Add treeNode to parent of current. parent.right = newTreeNode; // Increase count of movies in BST. movieCount++; exit = true; } } } } }
// Function that deletes the movie from the BST - accounts for 0 // children, 1 child on L or R and 2 children deletion. // No output. public bool Delete(Movie movie, MemberCollection memberCollection) { // Check to see if the movie is already rented to a user. Movie[] rentedMovies = memberCollection.ReturnCurrentBorrowedMovies(); bool check = false; // Mark check as true if the entered movie is currently rented. foreach (Movie m in rentedMovies) { if (m.GetTitle() == movie.GetTitle()) { check = true; } } //If it is not currently rented out to user, delete the movie // from BST. if (!check) { TreeNode current = root; TreeNode parent = root; bool leftNode = true; // Check to see if the current movie is the user entered // movie then traverse the BST based off name comparison. while (current.movie.GetTitle() != movie.GetTitle()) { parent = current; // If searched name is less than current node, travel // left tree. if (string.Compare(movie.GetTitle(), current.movie.GetTitle()) == -1) { leftNode = true; current = current.left; } // If searched name is greater than current node, travel // right tree. else { leftNode = false; current = current.right; } if (current == null) { return(false); } } if ((current.left == null) && (current.right == null)) { // Delete the node as it has no children. if (current == root) { root = null; return(true); } // Delete the left child else if (leftNode) { parent.left = null; return(true); } // Delete the right child else { parent.right = null; return(true); } } // If current node's right node is empty, replace with left // node else if (current.right == null) { if (current == root) { root = current.left; return(true); } // Replace current node with its left child in BST. else if (leftNode) { parent.left = current.left; return(true); } // Replace current node with its right child in BST. else { parent.right = current.left; return(true); } } // If current node's left node is empty, replace with right // node else if (current.left == null) { if (current == root) { root = current.right; return(true); } // Replace current node with its left child in BST. else if (leftNode) { parent.left = current.right; return(true); } // Replace current node with its right child in BST. else { parent.right = current.right; return(true); } } // Node has 2 children else { // If far right most node of current node is empty, // current node becomes left node. if (current.left.right == null) { current.movie = current.left.movie; current.left = current.left.left; return(true); } // Replace current node with the far right most node. else { TreeNode node = current.left; TreeNode parentNode = current; while (node.right != null) { parentNode = node; node = node.right; } current.movie = node.movie; parentNode.right = node.left; return(true); } } } else { Console.WriteLine("You cannot delete a " + "movie that is currently rented out."); return(false); } }