Esempio n. 1
0
        /// <summary>
        /// This method will store contents of the file
        /// </summary>
        /// <param name="fileData"></param>
        private void storeContent(List <String> fileData)
        {
            foreach (String line in fileData)
            {
                String[] data = line.Split('|')
                                .Select(text => text.Trim())
                                .ToArray();

                switch (data[0].ToUpper())
                {
                case "D":
                {
                    int    Ordinal = Convert.ToInt32(data[1]);
                    string Actor   = data[2];
                    int    Series  = Convert.ToInt32(data[3]);
                    string Age     = data[4];
                    string Debut   = data[5];

                    Doctor doc = new Doctor(Ordinal, Actor, Series, Age, Debut);
                    doctorList.Add(doc);
                }
                break;

                case "C":
                {
                    string Name   = data[1];
                    string Actor  = data[2];
                    int    Doctor = Convert.ToInt32(data[3]);
                    string Debut  = data[4];

                    Companion comp = new Companion(Name, Actor, Doctor, Debut);
                    companionList.Add(comp);
                }
                break;

                case "E":
                {
                    string Story  = data[1];
                    int    Season = Convert.ToInt32(data[2]);
                    int    Year   = Convert.ToInt32(data[3]);
                    string Title  = data[4];

                    Episode episode = new Episode(Story, Season, Year, Title);
                    episodeList.Add(episode);
                }
                break;
                }
            }

            doctorList =                        //sorting doctors in ascending order
                         doctorList.OrderBy
                             (d => d.Ordinal)
                         .ToList();

            companionList =                     //sorting companions in ascending order
                            companionList
                            .OrderBy(c => c.Debut)
                            .ToList();

            episodeList =                       //sorting episodes in ascending order
                          episodeList
                          .OrderBy(e => e.Story)
                          .ToList();
        }
        /// <summary>
        /// Method for event handling which loads the data to program
        /// </summary>
        private void changeDetails(object sender, EventArgs e)
        {
            // Condition returns true if the data is loaded in to the program
            if (dataLoaded)
            {
                // listbox is cleared for storing values as per the doctor
                listBox1.Items.Clear();

                /// <summary>
                /// Object for Doctor class
                /// </summary>
                Doctor doc = null;

                // Looping through doctorList to retrieve all the objects of the Doctor class objects
                for (int i = 0; i < doctorsList.Count; i++)
                {
                    // Condition for checking which doctor is selected in the comboBox
                    if (doctorsList.ElementAt(i).getOrdinal() == comboBox1.SelectedIndex + 1)
                    {
                        // getting the object instance at the selected index in the comboBox
                        doc = doctorsList.ElementAt(i);
                        break;
                    }
                }

                // diplaying the Actor's name in the "Played By" label
                textBox2.Text = doc.getActor();

                // displaying the series in "Series" label
                textBox3.Text = doc.getSeries() + "";

                // displaying age in "Age of Start" Label
                textBox4.Text = doc.getAge() + "";

                /// <summary>
                /// List for storing all objects of Companion Class for the Doctor instance
                /// </summary>
                List <Companion> companionsResultList = new List <Companion>();

                /// <summary>
                /// List for storing all objects of Episode class for the Episode instance
                /// </summary>
                List <Episode> episodesResultList = new List <Episode>();

                // Making query to get Companion objects which has same Doctor instance
                IEnumerable <Companion> companionQuery =
                    from companion in companionsList where companion.getDoctor() == doc.getOrdinal() select companion;

                // Looping through companionQuery Enumerable object
                foreach (Companion companion in companionQuery)
                {
                    // storing all the objects into List of companionResultList
                    companionsResultList.Add(companion);
                }

                // Looping through list companionResultList to get all the episodes for companion instance
                for (int i = 0; i < companionsResultList.Count; i++)
                {
                    // Making query to get episode objects where
                    IEnumerable <Episode> episodeQuery =
                        from episode in episodesList where episode.getStory() == companionsResultList[i].getDebut() select episode;

                    // looping through the IEumerable query to get all the objects of the Episode class
                    foreach (Episode episode in episodeQuery)
                    {
                        episodesResultList.Add(episode);
                    }
                }

                // Looping through the episode list to sort each time the data is loaded into the listbox
                for (int i = 0; i < episodesResultList.Count - 1; i++)
                {
                    // storing the current value of "i" into some variable
                    int tempIndex = i;

                    // companion object is stored into variable
                    Companion tempCompanion = companionsResultList.ElementAt(i);

                    // episode object is stored into variable
                    Episode tempEpisode = episodesResultList.ElementAt(i);

                    // taking flag to compare the values
                    bool flag = false;
                    for (int j = i + 1; j < episodesResultList.Count; j++)
                    {
                        // sorting the fields by year condition returns true if oneis less than antother
                        if (episodesResultList.ElementAt(j).getYear() < tempEpisode.getYear())
                        {
                            // storing the current index of "j"
                            tempIndex = j;
                            // changing the tempEpisode variable and passing new index value into it
                            tempEpisode = episodesResultList.ElementAt(j);
                            // changning the tempCompanion variable and passing new index value into it
                            tempCompanion = companionsResultList.ElementAt(j);
                            // flag is turn to true as it was succesful in sorting
                            flag = true;
                        }

                        // while sorting if the value of both the episode year is same
                        else if (episodesResultList.ElementAt(j).getYear() == tempEpisode.getYear())
                        {
                            // in this case name of the values are compared to sort
                            if (companionsResultList.ElementAt(j).getName().CompareTo(tempCompanion.getName()) == -1)
                            {
                                // storing the current index of "j"
                                tempIndex = j;
                                // changing the tempEpisode variable and passing new index value into it
                                tempEpisode = episodesResultList.ElementAt(j);
                                // changning the tempCompanion variable and passing new index value into it
                                tempCompanion = companionsResultList.ElementAt(j);
                                // flag is turn to true as it was succesful in sorting
                                flag = true;
                            }
                        }
                    }
                    // if the flag value returns true in the result of succesfull sorting
                    if (flag)
                    {
                        // values in the episodeResultList is sorted in order to display sorted result
                        episodesResultList[tempIndex] = episodesResultList.ElementAt(i);
                        // values in the episodeResultList is sorted in order to display sorted result
                        episodesResultList[i] = tempEpisode;
                        // values in the companionResultList is sorted in order to display sorted result
                        companionsResultList[tempIndex] = companionsResultList.ElementAt(i);
                        // values in the companionResultList is sorted in order to display sorted result
                        companionsResultList[i] = tempCompanion;
                    }
                }

                // displaying the year for the debut by artist
                textBox1.Text = episodesResultList.ElementAt(0).getYear() + "";
                // displaying the title of the debut
                textBox5.Text = episodesResultList.ElementAt(0).getTitle();

                // looping thourgh companionResultList to diplsay the data from the sorted list to listbox
                for (int i = 0; i < companionsResultList.Count; i++)
                {
                    // First line will be the Companion name and Actor name
                    string result1 = companionsResultList.ElementAt(i).getName() + "("
                                     + companionsResultList.ElementAt(i).getActor() + ")" + System.Environment.NewLine;

                    // Second line is declared
                    string result2 = "";

                    // condition returns true if the value of the "i" is less than episodeResult length
                    if (i < episodesResultList.Count)
                    {
                        // second line is set to Episode title and Episode year
                        result2 = "\"" + episodesResultList.ElementAt(i).getTitle() + "\"("
                                  + episodesResultList.ElementAt(i).getYear() + ")";
                    }

                    // result is added to listobox in order to display the content
                    listBox1.Items.Add(result1);
                    listBox1.Items.Add(result2);
                    listBox1.Items.Add("");
                }
            }
        }
        /// <summary>
        /// Method for the extracting data
        /// </summary>
        ///<param name="fileName">name of file selected by the user</param>

        private void extractData(string fileName)
        {
            // To retrieve the line from the file and store it to some variable
            string line;

            // handling the file not found exception
            try
            {
                // StreamReader reads provided file instance
                StreamReader file =
                    new StreamReader(fileName);

                // adding values to combobox
                comboBox1.Items.Add("First");
                comboBox1.Items.Add("Second");
                comboBox1.Items.Add("Third");
                comboBox1.Items.Add("Fourth");
                comboBox1.Items.Add("Fifth");
                comboBox1.Items.Add("Sixth");
                comboBox1.Items.Add("Seventh");
                comboBox1.Items.Add("Eighth");
                comboBox1.Items.Add("Ninth");
                comboBox1.Items.Add("Tenth");
                comboBox1.Items.Add("Eleventh");
                comboBox1.Items.Add("Twelfth");
                // selecting one index by default
                comboBox1.SelectedIndex = 0;

                // looping through whole file inorder to read and load lists
                while ((line = file.ReadLine()) != null)
                {
                    // condition returns true if the line has the first element starting with "D" and
                    // to only accept the "D" as character checking the second element after the "D"
                    // must be whitespace
                    if (line.ElementAt(0) == 'D' && Char.IsWhiteSpace(line.ElementAt(1)))
                    {
                        // declaring index variable and initializing it
                        int index = 1;

                        // in the same line if the line encounters with "|" and whitespace character after
                        // it then the condition returns true
                        while (line.ElementAt(index) == '|' || Char.IsWhiteSpace(line.ElementAt(index)))
                        {
                            // index value increased by 1 everytime the loop runs
                            index++;
                        }

                        // temp variable is declared to store whole line ignoring the "|"
                        string temp = "";

                        // condition returns true if the index value is not "|"
                        if (line.ElementAt(index + 1) != '|')
                        {
                            // storing all the values into the temp variable except "|"
                            temp += line.ElementAt(index) + "" + line.ElementAt(index + 1);
                            // and index value is increased by 3 for skiping empty spaces in the file
                            index += 3;
                        }
                        else
                        {
                            // storing all the values into the temp variable
                            temp += line.ElementAt(index);
                            // and index value is increased by 2 for skiping empty spaces in the file
                            index += 2;
                        }
                        // int ordinal is the serial nummber of doctor
                        int ordinal = int.Parse(temp);
                        // string temp to null for stroing another data
                        temp = "";
                        // looping through indexed value of the line to get the seconed value in the row
                        while (!(Char.IsWhiteSpace(line.ElementAt(index)) && Char.IsWhiteSpace(line.ElementAt(index + 1))) &&
                               !(Char.IsWhiteSpace(line.ElementAt(index)) && line.ElementAt(index + 1) == '|'))
                        {
                            // storing all the values into the temp variable
                            temp += line.ElementAt(index);
                            // increasing the value of the index in each run
                            index++;
                        }

                        // actor variable for storing temp value returned above
                        string actor = temp;
                        // delaring the temp value to null
                        temp = "";

                        // looping through indexed value of line if the line found with "|" and whitespace character
                        while (line.ElementAt(index) == '|' || Char.IsWhiteSpace(line.ElementAt(index)))
                        {
                            // index value of the line increased by 1 in every run
                            index++;
                        }
                        // condition returns true if the line element is not "|"
                        if (line.ElementAt(index + 1) != '|')
                        {
                            // storing all the values into the temp variable
                            temp += line.ElementAt(index) + "" + line.ElementAt(index + 1);
                            // and index value is increased by 3 for skiping empty spaces in the file
                            index += 3;
                        }
                        else
                        {
                            // storing all the values into the temp variable
                            temp += line.ElementAt(index);
                            // and index value is increased by 2 for skiping empty spaces in the file
                            index += 2;
                        }

                        // int series is the serial number of doctor
                        int series = int.Parse(temp);
                        // string temp to null for stroing another data
                        temp = "";

                        // looping through indexed value of line if the line found with "|" and whitespace character
                        while (line.ElementAt(index) == '|' || Char.IsWhiteSpace(line.ElementAt(index)))
                        {
                            // index value of the line increased by 1 in every run
                            index++;
                        }

                        // condition returns true if the line element is not "|"
                        if (line.ElementAt(index + 1) != '|')
                        {
                            // storing all the values into the temp variable
                            temp += line.ElementAt(index) + "" + line.ElementAt(index + 1);
                            // and index value is increased by 3 for skiping empty spaces in the file
                            index += 3;
                        }
                        else
                        {
                            // storing all the values into the temp variable
                            temp += line.ElementAt(index);
                            // and index value is increased by 2 for skiping empty spaces in the file
                            index += 2;
                        }

                        // int age is the age of the artist
                        int age = int.Parse(temp);
                        // string temp to null for stroing another data
                        temp = "";

                        // looping through indexed value of line if the line found with "|"
                        while (line.ElementAt(index) != '|')
                        {
                            // storing all the values into the temp variable
                            temp += line.ElementAt(index);
                            // index value of the line increased by 1 in every run
                            index++;
                        }
                        // with all values Anonymous Doctor object is created with Doctor class and
                        // added to doctorList
                        doctorsList.Add(new Doctor(ordinal, actor, series, age, temp));
                    }

                    // condition returns true if the line has the first element starting with "E" and
                    // a whitespace character
                    else if (line.ElementAt(0) == 'E' && Char.IsWhiteSpace(line.ElementAt(1)))
                    {
                        // declaring index variable and initializing it
                        int index = 1;
                        // looping through indexed value of line if the line found with "|"
                        while (line.ElementAt(index) != '|')
                        {
                            // index value of the line increased by 1 in every run
                            index++;
                        }
                        // index value of the line increased by 1 in every run
                        index++;

                        // temp variable is declared to store whole line ignoring the "|"
                        string temp = "";

                        // looping through data till the whitespace charcter is found
                        while (!Char.IsWhiteSpace(line.ElementAt(index)))
                        {
                            // storing all the values into the temp variable
                            temp += line.ElementAt(index);
                            // increasing the value of the index in each run
                            index++;
                        }
                        // story variable for storing temp value returned above
                        string story = temp;
                        // delaring the temp value to null
                        temp = "";

                        // looping through indexed value of line if the line found with "|" and whitespace character
                        while (line.ElementAt(index) == '|' || Char.IsWhiteSpace(line.ElementAt(index)))
                        {
                            // index value of the line increased by 1 in every run
                            index++;
                        }
                        // if whitespace charcter is not found then this condition returns true
                        if (!Char.IsWhiteSpace(line.ElementAt(index + 1)))
                        {
                            // storing all the values into the temp variable
                            temp += line.ElementAt(index) + "" + line.ElementAt(index + 1);
                            // and index value is increased by 2 for skiping empty spaces in the file
                            index += 2;
                        }
                        else
                        {
                            // storing all the values into the temp variable
                            temp += line.ElementAt(index);
                            // increasing the value of the index in each run
                            index++;
                        }

                        // int season is the episode season number
                        int season = int.Parse(temp);
                        // string temp to null for stroing another data
                        temp = "";

                        // looping through indexed value of line if the line found with "|" and whitespace shaarcter
                        while (line.ElementAt(index) == '|' || Char.IsWhiteSpace(line.ElementAt(index)))
                        {
                            // index value of the line increased by 1 in every run
                            index++;
                        }
                        // storing all the values into the temp variable
                        temp += line.ElementAt(index) + "" + line.ElementAt(index + 1) + "" + line.ElementAt(index + 2) + "" + line.ElementAt(index + 3);
                        // int year is the year in which the episode was released
                        int year = int.Parse(temp);
                        // string temp to null for stroing another data
                        temp = "";
                        // and index value is increased by 5 for skiping empty spaces in the file
                        index += 5;

                        // looping till the end of the line
                        while (index < line.Length)
                        {
                            // storing all the values into the temp variable
                            temp += line.ElementAt(index);
                            // increasing the value of the index in each run
                            index++;
                        }
                        // all the information from the file is stored into the Episode objects and
                        // stored into list for further use
                        episodesList.Add(new Episode(story, season, year, temp));
                    }

                    // condition returns true if the line has the first element starting with "C" and
                    // a whitespace character
                    else if (line.ElementAt(0) == 'C' && Char.IsWhiteSpace(line.ElementAt(1)))
                    {
                        // declaring index variable and initializing it
                        int index = 1;
                        // looping through indexed value of line if the line found with "|"
                        while (line.ElementAt(index) != '|')
                        {
                            // index value increased by 1 everytime the loop runs
                            index++;
                        }
                        // increasing the value of the index in each run
                        index++;
                        string temp = "";
                        // if the line encounters with some non-white space character and whitespacecharacter
                        // and follwing that non-white space character and "|" at the same time then the
                        // condtition returns true
                        while (!(Char.IsWhiteSpace(line.ElementAt(index)) && Char.IsWhiteSpace(line.ElementAt(index + 1))) &&
                               !(Char.IsWhiteSpace(line.ElementAt(index)) && line.ElementAt(index + 1) == '|'))
                        {
                            // storing all the values into the temp variable except "|"
                            temp += line.ElementAt(index);
                            // index value increased by 1 everytime the loop runs
                            index++;
                        }
                        // name variable for storing temp value from returnred above
                        string name = temp;
                        // delaring the temp value to null
                        temp = "";
                        // in the same line if the line encounters with "|" and whitespace character after
                        // it then the condition returns true
                        while (line.ElementAt(index) == '|' || Char.IsWhiteSpace(line.ElementAt(index)))
                        {
                            // index value increased by 1 everytime the loop runs
                            index++;
                        }
                        // if the line encounters non-whitespace character and white space character at the same time
                        // then the condition returns true
                        while (!(Char.IsWhiteSpace(line.ElementAt(index)) && Char.IsWhiteSpace(line.ElementAt(index + 1))))
                        {
                            // storing all the values into the temp variable except "|"
                            temp += line.ElementAt(index);
                            // index value increased by 1 everytime the loop runs
                            index++;
                        }
                        // actor variable for storing temp value returned above
                        string actor = temp;
                        // delaring the temp value to null
                        temp = "";
                        // in the same line if the line encounters with "|" and whitespace character after
                        // it then the condition returns true
                        while (line.ElementAt(index) == '|' || Char.IsWhiteSpace(line.ElementAt(index)))
                        {
                            // index value increased by 1 everytime the loop runs
                            index++;
                        }
                        if (!Char.IsWhiteSpace(line.ElementAt(index + 1)))
                        {
                            // storing all the values into the temp variable except "|"
                            temp += line.ElementAt(index) + "" + line.ElementAt(index + 1);
                            // and index value is increased by 2 for skiping empty spaces in the file
                            index += 2;
                        }
                        else
                        {
                            // storing all the values into the temp variable except "|"
                            temp += line.ElementAt(index);
                            // index value increased by 1 everytime the loop runs
                            index++;
                        }
                        // doctor variable to store ordinal doctor number
                        int doctor = int.Parse(temp);
                        // delaring the temp value to null
                        temp = "";
                        // if the line encounters "|" and white space character in the line at the same time
                        while (line.ElementAt(index) == '|' || Char.IsWhiteSpace(line.ElementAt(index)))
                        {
                            // index value increased by 1 everytime the loop runs
                            index++;
                        }
                        // if the indexed character is non-space character
                        while (!Char.IsWhiteSpace(line.ElementAt(index)))
                        {
                            // storing all the values into the temp variable except "|"
                            temp += line.ElementAt(index);
                            // index value increased by 1 everytime the loop runs
                            index++;
                        }
                        // with all values Anonymous Companion object is created and
                        // added to companionList
                        companionsList.Add(new Companion(name, actor, doctor, temp));
                    }
                }
                // Closes so that data in the file is not distorted while program is processing
                file.Close();

                // Doctor object doc is declared set to null for displaying
                Doctor doc = null;

                // looping through doctorList List which has ordinal 12 doctors stored in it
                for (int i = 0; i < doctorsList.Count; i++)
                {
                    // condition returns true for the first ordinal doctor
                    if (doctorsList.ElementAt(i).getOrdinal() == 1)
                    {
                        // retrieving reference of Doctor class in Doctor List
                        // using which we can call other object references to display
                        // other objects
                        doc = doctorsList.ElementAt(i);
                        break;
                    }
                }

                // diplaying the Actor's name in the "Played By" label
                textBox2.Text = doc.getActor();

                // displaying the series in "Series" label
                textBox3.Text = doc.getSeries() + "";

                // displaying age in "Age of Start" Label
                textBox4.Text = doc.getAge() + "";

                /// <summary>
                /// List for storing all objects of Companion Class for the Doctor instance
                /// </summary>
                List <Companion> companionsResultList = new List <Companion>();

                /// <summary>
                /// List for storing all objects of Episode class for the Episode instance
                /// </summary>
                List <Episode> episodesResultList = new List <Episode>();

                // Making query to get Companion objects which has same Doctor instance
                IEnumerable <Companion> companionQuery =
                    from companion in companionsList where companion.getDoctor() == doc.getOrdinal() select companion;

                // Looping through companionQuery Enumerable object
                foreach (Companion companion in companionQuery)
                {
                    // storing all the objects into List of companionResultList
                    companionsResultList.Add(companion);
                }

                // Looping through list companionResultList to get all the episodes for companion instance
                for (int i = 0; i < companionsResultList.Count; i++)
                {
                    // Making query to get episode objects where
                    IEnumerable <Episode> episodeQuery =
                        from episode in episodesList where episode.getStory() == companionsResultList[i].getDebut() select episode;

                    // looping through the IEumerable query to get all the objects of the Episode class
                    foreach (Episode episode in episodeQuery)
                    {
                        episodesResultList.Add(episode);
                    }
                }

                // Looping through the episode list to sort each time the data is loaded into the listbox
                for (int i = 0; i < episodesResultList.Count - 1; i++)
                {
                    // storing the current value of "i" into some variable
                    int tempIndex = i;

                    // companion object is stored into variable
                    Companion tempCompanion = companionsResultList.ElementAt(i);

                    // episode object is stored into variable
                    Episode tempEpisode = episodesResultList.ElementAt(i);

                    // taking flag to compare the values
                    bool flag = false;
                    for (int j = i + 1; j < episodesResultList.Count; j++)
                    {
                        // sorting the fields by year condition returns true if oneis less than antother
                        if (episodesResultList.ElementAt(j).getYear() < tempEpisode.getYear())
                        {
                            // storing the current index of "j"
                            tempIndex = j;
                            // changing the tempEpisode variable and passing new index value into it
                            tempEpisode = episodesResultList.ElementAt(j);
                            // changning the tempCompanion variable and passing new index value into it
                            tempCompanion = companionsResultList.ElementAt(j);
                            // flag is turn to true as it was succesful in sorting
                            flag = true;
                        }

                        // while sorting if the value of both the episode year is same
                        else if (episodesResultList.ElementAt(j).getYear() == tempEpisode.getYear())
                        {
                            // in this case name of the values are compared to sort
                            if (companionsResultList.ElementAt(j).getName().CompareTo(tempCompanion.getName()) == -1)
                            {
                                // storing the current index of "j"
                                tempIndex = j;
                                // changing the tempEpisode variable and passing new index value into it
                                tempEpisode = episodesResultList.ElementAt(j);
                                // changning the tempCompanion variable and passing new index value into it
                                tempCompanion = companionsResultList.ElementAt(j);
                                // flag is turn to true as it was succesful in sorting
                                flag = true;
                            }
                        }
                    }
                    // if the flag value returns true in the result of succesfull sorting
                    if (flag)
                    {
                        // values in the episodeResultList is sorted in order to display sorted result
                        episodesResultList[tempIndex] = episodesResultList.ElementAt(i);
                        // values in the episodeResultList is sorted in order to display sorted result
                        episodesResultList[i] = tempEpisode;
                        // values in the companionResultList is sorted in order to display sorted result
                        companionsResultList[tempIndex] = companionsResultList.ElementAt(i);
                        // values in the companionResultList is sorted in order to display sorted result
                        companionsResultList[i] = tempCompanion;
                    }
                }

                // displaying the year for the debut by artist
                textBox1.Text = episodesResultList.ElementAt(0).getYear() + "";
                // displaying the title of the debut
                textBox5.Text = episodesResultList.ElementAt(0).getTitle();

                // looping thourgh companionResultList to diplsay the data from the sorted list to listbox
                for (int i = 0; i < companionsResultList.Count; i++)
                {
                    // First line will be the Companion name and Actor name
                    string result1 = companionsResultList.ElementAt(i).getName() + "("
                                     + companionsResultList.ElementAt(i).getActor() + ")" + System.Environment.NewLine;

                    // Second line is declared
                    string result2 = "";

                    // condition returns true if the value of the "i" is less than episodeResult length
                    if (i < episodesResultList.Count)
                    {
                        // second line is set to Episode title and Episode year
                        result2 = "\"" + episodesResultList.ElementAt(i).getTitle() + "\"("
                                  + episodesResultList.ElementAt(i).getYear() + ")";
                    }

                    // result is added to listobox in order to display the content
                    listBox1.Items.Add(result1);
                    listBox1.Items.Add(result2);
                    listBox1.Items.Add("");
                }
                // dataloaded is set to true so far the program already load up with
                // data needed to displat and program has even displayed "First" ordnial
                // which is seleceted by default by combobox
                dataLoaded = true;
            }
            catch (Exception)
            {
                // User-frienly Message box displaying that there was error opening file
                MessageBox.Show("Error finding file/opening file");
            }
        }
Esempio n. 4
0
        int val            = 0;                      // initializes the doctor picked value

        /// <summary>
        /// This method loads the txt file the user chooses, reads the information and sets up the lists
        /// </summary>
        /// <param name="sender">The sender parameter</param>
        /// <param name="e">The event Parameter</param>
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string filePath; // initializes the fileContent and filePath variable

            // Starts the open file dialog
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.InitialDirectory = "c:\\";                    // initializes the initial directory
                openFileDialog.Filter           = "txt files (*.txt)|*.txt"; // initializes the file filter
                openFileDialog.FilterIndex      = 2;                         // initializes the index filter
                openFileDialog.RestoreDirectory = true;                      // initializes the restore directory to true

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // Get the path of specified file
                    filePath = openFileDialog.FileName;

                    // Get the name of the file
                    fileName = Path.GetFileName(filePath);

                    ////Read the contents of the file into a stream
                    var fileStream = openFileDialog.OpenFile();

                    // create the stream reader
                    try                                                 // exception handling for the file IO
                    {
                        StreamReader data = new StreamReader(filePath); // read data from file
                        string       line;                              // initialize line string
                        while ((line = data.ReadLine()) != null)        // true while the line read is not empty
                        {
                            string[] exploded = line.Split('|');        // split the line read by the | character and save into array

                            if (exploded[0].Trim() == "E")
                            {                // create new episode object
                                Episode temp = new Episode(exploded[1].Trim(), int.Parse(exploded[2].Trim()), int.Parse(exploded[3].Trim()), exploded[4].Trim());
                                p.Add(temp); // add to episode list
                            }
                            if (exploded[0].Trim() == "C")
                            {                // create new companion object
                                Companion temp = new Companion(exploded[1].Trim(), exploded[2].Trim(), int.Parse(exploded[3].Trim()), exploded[4].Trim());
                                c.Add(temp); // add to companion list
                            }
                            if (exploded[0].Trim() == "D")
                            {
                                // create doctor object
                                Doctor temp = new Doctor(int.Parse(exploded[1].Trim()), exploded[2].Trim(), int.Parse(exploded[3].Trim()), int.Parse(exploded[4].Trim()), exploded[5].Trim());
                                d.Add(temp); // add to doctor list
                                // create chosen object
                                Chosen chosen = new Chosen(int.Parse(exploded[1].Trim()), exploded[2].Trim(), int.Parse(exploded[3].Trim()), int.Parse(exploded[4].Trim()), exploded[5].Trim());
                                ch.Add(chosen); // add to chosen list
                            }
                        }
                        data.Close();                                      // close the IO
                        d.Sort((x, y) => x.Ordinal.CompareTo(y.Ordinal));  // sort doctor list
                        ch.Sort((x, y) => x.Ordinal.CompareTo(y.Ordinal)); // sort chosen list
                        foreach (Chosen chosen in ch)
                        {
                            // use linq to get matching title
                            var joinedItem = d.Join(p.Where(_ => _.Story == chosen.Debut),
                                                    D => D.Debut,            // "Outer" key expression
                                                    P => P.Story,            // "Inner" key expression
                                                    (D, P) => String.Format(P.Title));
                            // set property value with item
                            foreach (string item in joinedItem)
                            {
                                chosen.Episode = item;
                            }
                            // use linq to get matching year
                            var joinedItem2 = d.Join(p.Where(_ => _.Story == chosen.Debut),
                                                     D => D.Debut,           // "Outer" key expression
                                                     P => P.Story,           // "Inner" key expression
                                                     (D, P) => String.Format((P.Year).ToString()));
                            // set property value with item
                            foreach (string item in joinedItem2)
                            {
                                chosen.Year = int.Parse(item);
                            }
                            // use linq to get matching year
                            var joinedItem6 = p.Join(c.Where(_ => _.Doctor == chosen.Ordinal),
                                                     P => P.Story,           // "Outer" key expression
                                                     C => C.Debut,           // "Inner" key expression
                                                     (P, C) => String.Format(P.Year.ToString()));
                            // set property with value of item
                            foreach (string item in joinedItem6)
                            {
                                foreach (Companion ck in c)
                                {
                                    if (chosen.Ordinal == ck.Doctor)
                                    {
                                        ck.Year = int.Parse(item);
                                    }
                                }
                            }
                            // use linq to get matching title
                            var joinedItem5 = p.Join(c.Where(_ => _.Doctor == chosen.Ordinal),
                                                     P => P.Story,           // "Outer" key expression
                                                     C => C.Debut,           // "Inner" key expression
                                                     (P, C) => String.Format(P.Title));

                            // set property with value of item
                            foreach (string item in joinedItem5)
                            {
                                foreach (Companion ck in c)
                                {
                                    if (chosen.Ordinal == ck.Doctor)
                                    {
                                        ck.Episode = item;
                                    }
                                }
                            }
                            // use linq to get matching actor
                            var joinedItem3 = d.Join(c.Where(_ => _.Debut == chosen.Debut && _.Doctor == chosen.Ordinal),
                                                     D => D.Debut,           // "Outer" key expression
                                                     C => C.Debut,           // "Inner" key expression
                                                     (D, C) => String.Format((C.Actor).ToString()));
                            // set property with value of item
                            foreach (string item in joinedItem3)
                            {
                                foreach (Companion co in c)
                                {
                                    if (co.Actor == item)
                                    {
                                        ts1.Add(co.Actor);
                                    }
                                }
                            }
                            // use linq to get matching name
                            var joinedItem4 = d.Join(c.Where(_ => _.Debut == chosen.Debut && _.Doctor == chosen.Ordinal),
                                                     D => D.Debut,           // "Outer" key expression
                                                     C => C.Debut,           // "Inner" key expression
                                                     (D, C) => String.Format((C.Name).ToString()));
                            // set property to value of item
                            foreach (string item in joinedItem3)
                            {
                                foreach (Companion co in c)
                                {
                                    if (co.Actor == item)
                                    {
                                        ts2.Add(co.Name);
                                    }
                                }
                            }
                            chosen.CompanionName  = ts1;                             // set temp string 1
                            chosen.CompanionActor = ts2;                             // set temp string 2
                            doctorComboBox.Items.Add(NumberToWords(chosen.Ordinal)); // display the word ordinal values
                        }

                        // initialize the starting info
                        doctorComboBox.SelectedIndex = 0;
                        yearTextBox.Text             = "1963";
                        actorTextBox.Text            = d[0].Actor;
                        seriesTextBox.Text           = (d[0].Series).ToString();
                        ageTextBox.Text     = (d[0].Age).ToString();
                        episodeTextBox.Text = "An Unearthly Child";
                    }
                    catch (Exception)                                                             // catch the exception if the file was not read properly
                    {
                        Console.WriteLine("the file was either not found or could not be read."); // exception error message
                    }
                }
            }
        }