示例#1
0
        /// <summary>
        /// This method runs the looped user interaction process that the user chooses what to sort and search for.
        /// </summary>
        /// <param name="media">The list of media objects pulled from the datafile</param>
        private static void Run(List <Media> media)
        {
            int  userInput = 0;    // initialize the user input variable to zero
            int  counter   = 0;    // initialize the counter to zero used trigger the display initializer when app starts
            bool running   = true; // boolean flag for the main program running loop

            // the main app running loop
            while (running)
            {
                // Initializer
                if (counter == 0)
                {
                    displayTitle();   // displays the main title
                    displayChoices(); // displays the users options
                }

                // Loop the user input until a number is entered
                while (!int.TryParse(Console.ReadLine(), out userInput))
                {
                    displayTitle();   // displays the main title
                    displayChoices(); // displays the users options
                }
                if (userInput == 6)   // end main loop if user inputs 6
                {
                    running = false;  // trigger the boolean flag to exit the main program loop
                }
                else
                {
                    displayTitle(); // displays the main title

                    // switch class that displays different media types or the search field depending on user input
                    switch (userInput)
                    {
                    // info is displayed for all books in the file
                    case 1:
                        sortTitle("Book Search");     // executes the sort title method

                        // runs through the media list and displays any book data without summary in the file
                        foreach (Media item in media)
                        {
                            if (item is Book)     // if the item is a book display the book info w/o summary
                            {
                                itemSearch(item); // call the itemsearch method and pass item as argument
                            }
                        }
                        break;     // break out of the switch case

                    // info is displayed for all movies in the file
                    case 2:
                        sortTitle("Movie Search");     // executes the sort title method

                        // runs through the media list and displays any movie data without summary in the file
                        foreach (Media item in media)
                        {
                            if (item is Movie)     // if the item is a movie display the movie info w/o summary
                            {
                                itemSearch(item);  // call the itemsearch method and pass item as argument
                            }
                        }
                        break;     // break out of the switch case

                    // info is displayed for all songs in the file
                    case 3:
                        sortTitle("Song Search");     // executes the sort title method

                        // runs through the media list and displays any song data in the file
                        foreach (Media item in media)
                        {
                            if (item is Song)     // if the item is a song display the song info
                            {
                                itemSearch(item); // call the itemsearch method and pass item as argument
                            }
                        }
                        break;     // break out of the switch case

                    // info is displayed for all media types in the file
                    case 4:
                        sortTitle("Media Search");    // executes the sort title method

                        foreach (Media item in media) // loop all items in the Media List
                        {
                            if (item != null)         // tue if the item is not empty
                            {
                                if (item is Book)     // if the item is a book display the book info w/o summary
                                {
                                    itemSearch(item); // call the itemsearch method and pass item as argument
                                }
                                if (item is Movie)    // if the item is a movie display the movie info w/o summary
                                {
                                    itemSearch(item); // call the itemsearch method and pass item as argument
                                }
                                if (item is Song)     // if the item is a song display the song info
                                {
                                    itemSearch(item); // call the itemsearch method and pass item as argument
                                }
                            }
                        }
                        break;     // break out of the switch case

                    // info is displayed for all items that have the user inputed string in the title
                    case 5:
                        sortTitle("Search all media by title"); // executes the sort title method
                        foreach (Media item in media)           // loop all items in the Media List
                        {
                            // prompt user
                            Console.WriteLine("Please enter a search term:\n ");
                            string query = Console.ReadLine();                    // store user input
                            Console.WriteLine("");                                // create extra space
                            if (query == "0" || query == "")                      // true if user input is not empty or 0
                            {
                                break;                                            // break out of the switch case
                            }
                            foreach (Media m in media)                            // loop all items in the Media List
                            {
                                if (m.Search(query))                              // true if the query is in the title of an item in the Media List
                                {
                                    Console.ForegroundColor = ConsoleColor.Green; // make console text green
                                    Console.WriteLine(m);                         // display the item info not including summary for movie/book
                                    Console.ForegroundColor = ConsoleColor.White; // make console text white

                                    if (m is Book)                                // if item is book displays the decrypted summary of the book
                                    {
                                        Book temp = m as Book;
                                        Console.WriteLine("");
                                        Console.WriteLine(temp.Decrypt());
                                        Console.WriteLine("--------------------");
                                    }
                                    if (m is Movie)     // if item is movie displays the decrypted summary of the movie
                                    {
                                        Movie temp = m as Movie;
                                        Console.WriteLine("");
                                        Console.WriteLine(temp.Decrypt());
                                        Console.WriteLine("--------------------");
                                    }
                                }
                            }
                            break;
                        }
                        break;
                    }

                    displayChoices(); // displays the users options

                    // change output to remind user 1 - 6 are only valid numbers
                    if (userInput != 1 && userInput != 2 && userInput != 3 && userInput != 4 && userInput != 5)
                    {
                        Console.Write("Please Enter a number from 1 - 6: ");
                    }
                    else
                    {
                        Console.Write("Enter choice:");
                    }
                    counter++; // increase the counter used to initialize the first output
                }
            }
        }
示例#2
0
        /// <summary>
        /// this method is about Choosing options in the menu
        /// </summary>
        /// <param name="mediaArray">array of Media</param>
        /// <returns>true</returns>
        private static bool Choice(Media[] mediaArray)
        {
            //create Books list
            List <Book> Books = new List <Book>();
            //create Movies list
            List <Movie> Movies = new List <Movie>();
            //create Songs list
            List <Song> Songs = new List <Song>();

            //store the object data in the assigned list according to the type of object
            foreach (Media element in mediaArray)
            {
                if (element is Book)
                {
                    Books.Add((Book)element);
                }
                else if (element is Movie)
                {
                    Movies.Add((Movie)element);
                }
                else if (element is Song)
                {
                    Songs.Add((Song)element);
                }
            }

            //diplay the menu option
            DisplayMenu();


            while (true)
            {
                //get the input from the user
                string input = Console.ReadLine();
                Console.WriteLine();

                //show the error if the user's input is wrong
                if ((int.TryParse(input, out int userSelection) == false) ||
                    userSelection < 1 || userSelection > 6)
                {
                    Console.WriteLine("\n*** Invalid Choice - Try Again ***\n");
                    Console.WriteLine("\nPress any key to continue . . .");
                    input = Console.ReadLine();
                    Console.Clear();
                    DisplayMenu();
                    continue;
                }

                //display the output according to the user's input
                if (userSelection == 1)
                {
                    //foreach statement of the Books list
                    foreach (Book element in Books)
                    {
                        //display the information
                        Console.WriteLine(element.ToString());
                        Console.WriteLine("-----------------------");
                    }
                }
                else if (userSelection == 2)
                {
                    foreach (Movie element in Movies)
                    {
                        Console.WriteLine(element.ToString());
                        Console.WriteLine("-----------------------");
                    }
                }
                else if (userSelection == 3)
                {
                    foreach (Song element in Songs)
                    {
                        Console.WriteLine(element.ToString());
                        Console.WriteLine("-----------------------");
                    }
                }
                else if (userSelection == 4)
                {
                    foreach (Media element in mediaArray)
                    {
                        Console.WriteLine(element.ToString());
                        Console.WriteLine("-----------------------");
                    }
                }
                else if (userSelection == 5)
                {
                    Console.Write("\nEnter a search string: ");

                    //get the keyword from the user
                    string keyword = Console.ReadLine();
                    Console.WriteLine();

                    //foreach statement of the mediaArray array
                    int keywordResultCount = 0;
                    foreach (Media element in mediaArray)
                    {
                        //check the keyword is found or not
                        if (element.Search(keyword))
                        {
                            //check the keyword is Book object
                            if (Books.Contains(element))
                            {
                                Book book = (Book)element;
                                Console.WriteLine(element.ToString());
                                //decrpt the Summary and display on the screen
                                Console.WriteLine("Summary: " + book.Decrypt());
                                Console.WriteLine("-----------------------");
                                book.Encrypt();
                                keywordResultCount++;
                            }
                            //check the keyword is Movies object
                            else if (Movies.Contains(element))
                            {
                                Movie movie = (Movie)element;
                                Console.WriteLine(element.ToString());
                                //decrpt the Summary and display on the screen
                                Console.WriteLine("Summary: " + movie.Decrypt());
                                Console.WriteLine("-----------------------");
                                movie.Encrypt();
                                keywordResultCount++;
                            }
                            //keyword is Song object
                            else
                            {
                                Console.WriteLine(element.ToString());
                                Console.WriteLine("-----------------------");
                                keywordResultCount++;
                            }
                        }
                        else
                        {
                            if (keywordResultCount > 0)
                            {
                                Console.WriteLine(keywordResultCount + " results are found");
                            }
                            else
                            {
                                Console.WriteLine("No search result");
                            }
                            break;
                        }
                        if (keywordResultCount == mediaArray.Length)
                        {
                            Console.WriteLine(keywordResultCount + " results are found");
                        }
                    }
                }
                else
                {
                    // if userSelection is 6, then exit
                    break;
                }
                Console.WriteLine("\nPress any key to continue . . .");

                string continueInput = Console.ReadLine();
                if (continueInput != null)
                {
                    //clear the screen and show the display menu
                    Console.Clear();
                    DisplayMenu();
                }
            }
            return(true);
        }