示例#1
0
        private static Media[] ReadData(int choice)
        {
            List <Media> result = new List <Media>(); // The final result that will be returned

            // Validate if there is a valid textfile in the specific directory and checks if the content of the file is valid
            try
            {
                // Initialize variables
                string file = @"Data.txt";                    // The path and name of the file string line;
                                                              // the current line of the stream
                StreamReader reader = new StreamReader(file); // Used for reading the file
                                                              // Reads file until the end of the stream
                while (!reader.EndOfStream)
                {
                    // Initialize variables that will be used for reading data, and will reset every time a new line is read
                    List <string> summary   = new List <string>();
                    string        line      = reader.ReadLine();
                    string[]      splitLine = line.Split('|');
                    // Makes sure the data being read is actually data, not a separator
                    if (!line.Equals("-----"))
                    {
                        // Check the type of media, and if it is the selected option by the user, then add it to the list of media that will be displayed
                        if (splitLine[0].ToUpper() == "BOOK" && (choice == 4 || choice == 1))
                        {
                            // Reads multiple lines of summary, until the next separator
                            while (!(line = reader.ReadLine()).Equals("-----"))
                            {
                                summary.Add(line);
                            }
                            result.Add(new Book(splitLine[3], Convert.ToInt32(splitLine[2]), splitLine[1], String.Join(' ', summary)));
                            foreach (var Book in result)
                            {
                                Console.WriteLine(Book.ToString());
                            }
                        }

                        else if (splitLine[0].ToUpper() == "SONG" && (choice == 4 || choice == 3))
                        {
                            // Reads multiple lines of summary, until the next separator
                            while (!(line = reader.ReadLine()).Equals("-----"))
                            {
                                summary.Add(line);
                            }
                            result.Add(new Song(splitLine[3], Convert.ToInt32(splitLine[2]), splitLine[1], String.Join(' ', summary)));
                            foreach (var Song in result)
                            {
                                Console.WriteLine(Song.ToString());
                            }
                        }
                        else if (splitLine[0].ToUpper() == "MOVIE" && (choice == 4 || choice == 2))
                        {
                            // Reads multiple lines of summary, until the next separator
                            while (!(line = reader.ReadLine()).Equals("-----"))
                            {
                                summary.Add(line);
                            }
                            result.Add(new Movie(splitLine[3], Convert.ToInt32(splitLine[2]), splitLine[1], String.Join(' ', summary)));
                            foreach (var Movie in result)
                            {
                                Console.WriteLine(Movie.ToString());
                            }
                        }
                        else if (choice == 6)
                        {
                        }
                    }
                }

                // Make the search case insensitive by treating strings as lowercase
                // Didn't find it
            }
            catch (Exception ex)
            {
            }
            return(result.ToArray());
        }
示例#2
0
        /// <summary>
        /// This method is for create the specific media object
        /// </summary>
        /// <param name="info">List of string</param>
        ///
        private static Media CreateMedia(List <string> info)
        {
            //neMedia for the return value
            Media newMedia;

            //values string array  for the splitting the value based on '|'
            string[] values = info[0].Split('|');
            //category string to store the value of index 0 of array
            string category = values[0];

            //check the category
            if (category == "BOOK" || category == "MOVIE")
            {
                //title string to store the value of index 1 of array
                string title = values[1];
                //year string to store the value of index 2 of array
                string year = values[2];
                //arthor string to store the value of index 3 of array
                string arthor = values[3];
                //yearInt for the Parsing the string year
                int yearInt = short.Parse(year);
                //summary string for the storing the remaining data
                string summary = "";
                for (int i = 1; i < info.Count; i++)
                {
                    summary += info[i] + Environment.NewLine;
                }

                if (category == "BOOK")
                {
                    //create the Book object and return
                    newMedia = new Book(title, yearInt, arthor, summary);
                    return(newMedia);
                }
                else
                {
                    //create the Movie object and return
                    newMedia = new Movie(title, yearInt, arthor, summary);
                    return(newMedia);
                }
            }
            else if (category == "SONG")
            {
                //title string to store the value of index 1 of array
                string title = values[1];
                //year string to store the value of index 2 of array
                string year = values[2];
                //album string to store the value of index 3 of array
                string album = values[3];
                //director string to store the value of index 3 of array
                string director = values[4];
                //yearInt for the Parsing the string year
                int yearInt = short.Parse(year);

                //create the Song object and return
                newMedia = new Song(title, yearInt, album, director);
                return(newMedia);
            }

            return(null);
        }