示例#1
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);
        }