コード例 #1
0
            // 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);
                }
            }
コード例 #2
0
        // Function that controls user input when prompting user to enter new
        // user details.
        public static void AddUser(MemberCollection memberCollection)
        {
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("----ENTER USER DETAILS----");
            Console.WriteLine();


            // Prompt for first name and checks input
            bool correct = false;

            Console.Write("Enter First Name: ");
            string firstName = Console.ReadLine();;

            while (!correct)
            {
                correct = HelperFunctions.CheckInput(firstName, 1);
                if (!correct)
                {
                    Console.Write("Enter First Name: ");
                    firstName = Console.ReadLine();
                }
            }

            // Prompt for last name and checks input
            Console.WriteLine();
            correct = false;
            Console.Write("Enter Last Name: ");
            string lastName = Console.ReadLine();;

            while (!correct)
            {
                correct = CheckInput(lastName, 1);
                if (!correct)
                {
                    Console.Write("Enter Last Name: ");
                    lastName = Console.ReadLine();
                }
            }


            // Prompt for address and checks input
            Console.WriteLine();
            correct = false;
            Console.Write("Enter Address: ");
            string address = Console.ReadLine();;

            while (!correct)
            {
                correct = CheckInput(address, 0);
                if (!correct)
                {
                    Console.Write("Enter Address: ");
                    address = Console.ReadLine();
                }
            }

            // Prompt for phone number and checks input
            Console.WriteLine();
            correct = false;
            Console.Write("Enter Phone Number: ");
            string phNo = Console.ReadLine();

            correct = int.TryParse(phNo, out int num);
            int phoneNumber = num;

            while (!correct)
            {
                correct = int.TryParse(phNo, out num);
                if (!correct)
                {
                    Console.WriteLine("Incorrect Phone Number" +
                                      " type - Phone Number must be only digits");
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.Write("Enter Phone Number: ");
                    phNo = Console.ReadLine();
                }
                else
                {
                    phoneNumber = num;
                }
            }

            // Prompt for password and checks input
            Console.WriteLine();
            Console.Write("Enter Password: "******"Incorrect password" +
                                      " type - password must be 4 digits");
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.Write("Enter Password: "******"User " +
                                  member.GetUsername() +
                                  " has been added to the database.");
            }
            else
            {
                Console.WriteLine("User already exists in database.");
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: adsy/TextBasedDVDLibrary
        static void Main(string[] args)
        {
            // Create a movieTree BST to hold movies and also a new
            // MemberCollection to hold members.
            MovieCollection.MovieTree BST = new MovieCollection.MovieTree();
            MemberCollection          memberCollection = new MemberCollection();
            Member testMember = new Member("test", "test", "1", 1, 2414);

            memberCollection.AddMember(testMember);

            // Flags used to run program
            bool quit   = false;
            bool logout = false;

            // Login details for admin user access.
            string adminUser = "******";
            string adminPass = "******";


            Movie movie1 = new Movie("movie6", "actor1",
                                     "director1", 90, "Horror", "MA 15+", DateTime.Now, 5, 3);

            BST.Insert(movie1);


            Movie movie2 = new Movie("movie5", "actor1",
                                     "director1", 90, "Horror", "MA 15+", DateTime.Now, 5, 7);

            BST.Insert(movie2);


            Movie movie3 = new Movie("movie10", "actor1",
                                     "director1", 90, "Horror", "MA 15+", DateTime.Now, 5, 2);

            BST.Insert(movie3);


            Movie movie4 = new Movie("movie9", "actor1",
                                     "director1", 90, "Horror", "MA 15+", DateTime.Now, 5, 4);

            BST.Insert(movie4);


            Movie movie5 = new Movie("movie2", "actor1",
                                     "director1", 90, "Horror", "MA 15+", DateTime.Now, 5, 1);

            BST.Insert(movie5);

            Movie movie6 = new Movie("movie4", "actor1",
                                     "director1", 90, "Horror", "MA 15+", DateTime.Now, 5, 3);

            BST.Insert(movie6);


            Movie movie7 = new Movie("movie7", "actor1",
                                     "director1", 90, "Horror", "MA 15+", DateTime.Now, 5, 9);

            BST.Insert(movie7);


            Movie movie8 = new Movie("movie11", "actor1",
                                     "director1", 90, "Horror", "MA 15+", DateTime.Now, 5, 0);

            BST.Insert(movie8);


            Movie movie9 = new Movie("movie8", "actor1",
                                     "director1", 90, "Horror", "MA 15+", DateTime.Now, 5, 4);

            BST.Insert(movie9);


            Movie movie10 = new Movie("movie1", "actor1",
                                      "director1", 90, "Horror", "MA 15+", DateTime.Now, 5, 3);

            BST.Insert(movie10);

            Movie movie11 = new Movie("movie3", "actor1",
                                      "director1", 90, "Horror", "MA 15+", DateTime.Now, 5, 1);

            BST.Insert(movie11);



            // Strings to hold the menus to prompt user input for Main Menu,
            // Staff Menu and Member Menu

            string mainMenu = "Welcome to the Community Library\n" +
                              "==============Main Menu===========\n" +
                              "1. Staff Login\n" +
                              "2. Member Login\n" +
                              "0. Exit\n" +
                              "===================================";



            string staffMenu = "============Staff Menu============\n" +
                               "1. Add a new movie DVD\n" +
                               "2. Remove a movie DVD\n" +
                               "3. Register a new Member\n" +
                               "4. Find a registered member's phone number\n" +
                               "0. Return to main menu\n" +
                               "===================================";



            string memberMenu = "==============Member Menu==============\n" +
                                "1. Display all movies\n" +
                                "2. Borrow a movie DVD\n" +
                                "3. Return a movie DVD\n" +
                                "4. List current borrowed movie DVDs\n" +
                                "5. Display top 10 most popular movies\n" +
                                "0. Return to main menu\n" +
                                "===================================";



            // While quit is not set to true, run program.
            while (!quit)
            {
                Console.WriteLine(mainMenu);
                string input = Console.ReadLine();


                // Exit Program if user input is 0.
                if (input == "0")
                {
                    quit = true;
                }



                // Show staff login screen if user input is 1.
                else if (input == "1")
                {
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine("Please enter the admin username:"******"Please enter the admin password:"******"admin" && password == "adminPass")
                    {
                        logout = false;
                        while (!logout)
                        {
                            Console.WriteLine();
                            Console.WriteLine();
                            Console.WriteLine(staffMenu);

                            input = Console.ReadLine();



                            //Admin enters 0 and goes back to main menu
                            if (input == "0")
                            {
                                logout = true;
                                Console.WriteLine();
                                Console.WriteLine();
                            }

                            //Admin enters 1 to add a new movie.
                            if (input == "1")
                            {
                                HelperFunctions.Insert(BST);
                            }

                            //Admin enters 2 to remove a movie.
                            if (input == "2")
                            {
                                Console.WriteLine();
                                Console.WriteLine();
                                Console.WriteLine("Please enter movie name " +
                                                  "which you wish to remove: ");
                                string title = Console.ReadLine();

                                // Call return movie function to get Movie info.
                                Movie movie = BST.ReturnMovie(BST.ReturnRoot(), title);

                                if (movie == null)
                                {
                                    Console.WriteLine("There is no movie with that name.");
                                }
                                else
                                {
                                    bool check = BST.Delete(movie, memberCollection);
                                    if (check)
                                    {
                                        BST.movieCount--;
                                        Console.WriteLine(title + " has been deleted from the database.");
                                    }
                                }
                            }

                            //Admin enters 3 to register a new user.
                            if (input == "3")
                            {
                                // Call the helper function that contains the calls for user input.
                                // Function has input parsing to check for incorrect user input.
                                // Adds user to MemberCollection if it passes.
                                HelperFunctions.AddUser(memberCollection);
                            }

                            //Admin enters 4 to search for users phone number by entering username
                            if (input == "4")
                            {
                                Console.WriteLine("Please enter the persons " +
                                                  "username to find their phone " +
                                                  "number: ");
                                string user = Console.ReadLine();

                                int phNo = memberCollection.ReturnPhNo(user);

                                if (phNo != -1)
                                {
                                    Console.WriteLine();
                                    Console.WriteLine();
                                    Console.WriteLine("The users phone number " +
                                                      "is " + phNo);
                                }
                                else
                                {
                                    Console.WriteLine("There are no users" +
                                                      " registered with that username.");
                                }
                            }
                        }
                    }

                    //Incorrect login details entered - return to main menu.
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine();
                        Console.WriteLine("Incorrect login details - " +
                                          "returning to main menu");
                        Console.WriteLine();
                        Console.WriteLine();
                    }
                }


                //Member Login
                else if (input == "2")
                {
                    Console.WriteLine();
                    Console.WriteLine();

                    logout = false;
                    string username = "******";
                    int    password = 0;
                    int    num      = 0;

                    Console.WriteLine("Please enter username:"******"Please enter password:"******"1":
                                Console.WriteLine();
                                Console.WriteLine();
                                Console.WriteLine("----ALL MOVIE INFO----");
                                BST.InOrderDisplayInfo(BST.ReturnRoot());
                                break;


                            //Borrow a movie
                            case "2":
                                Console.WriteLine();
                                Console.WriteLine();
                                Console.WriteLine("Please enter the " +
                                                  "name of the movie you " +
                                                  "wish to rent: ");
                                movieTitle = Console.ReadLine();
                                movie      = BST.ReturnMovie
                                                 (BST.ReturnRoot(), movieTitle);
                                memberCollection.Insert2Member
                                    (currentUser, movie);
                                break;


                            //Return a movie
                            case "3":
                                Console.WriteLine();
                                Console.WriteLine();
                                Console.WriteLine("Please enter the " +
                                                  "name of the movie you " +
                                                  "wish to return: ");
                                movieTitle = Console.ReadLine();
                                movie      = BST.ReturnMovie
                                                 (BST.ReturnRoot(), movieTitle);
                                memberCollection.RemoveMovieFromMember
                                    (currentUser, movie);
                                break;


                            //Show borrowed movies
                            case "4":
                                memberCollection.ShowBorrowedMovies
                                    (currentUser);
                                break;


                            //Display top 10 rented movies.
                            case "5":
                                Console.WriteLine();
                                Console.WriteLine();
                                Console.WriteLine("----T0P 10 RENTED MOVIES----");
                                BST.DisplayTop10();
                                break;


                            //Return to main menu.
                            case "0":
                                logout = true;
                                Console.WriteLine();
                                Console.WriteLine();
                                break;
                            }
                        }
                        else
                        {
                            logout = true;
                        }
                    }
                }



                //Incorrect input
                else
                {
                    Console.WriteLine("Incorrect number entered - you must " +
                                      "enter a number between 0 - 2.");
                }
            }
        }