Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Visual C# application that displays the contents of the Teams.txt file

ID: 3601894 • Letter: C

Question

Create a Visual C# application that displays the contents of the Teams.txt file in a ListBox control. When the user selects a team in the ListBox, the application should display the number of times that team has won the World Series in the time period from 1903 to 2012. The two files used are Teams.txt , which contains a list of the names of teams that have won the Championship at least once, and WorldSeriesWinners.txt - this file contains a chronological list of the World Series winning teams from 1903 - 2012. The first line in the file is the nae of the team that won in 1903 and the last line is the name of the team that won in 2012. Note that the World Series was not played in 1904 or 1994. This is the question that i'm having problems with.

Actually in this question I must make use of class, but the code is not working This is my code. I hope that you can help me find the problem so that the code woks perfectly

This is the class part

class WorldSeries

{

// Field

private string _wins;   // The team's total number of wins.

// Constructor public WorldSeries()

{

_wins = "";

}

// Wins

property public string Wins

{

get { return _wins; }

set { _wins = value; }

}

}

This is the rest of my code

// The ReadTeam methos reads the team name from the Teams.txt file

        // and displays it in a list box.

        private void ReadTeam()

        {

            try

            {

                // Local Variables

                StreamReader inputTeams;    //To read the file

                string teamName;            // To hold the teams names

                // Open the Teams.txt file.

                inputTeams = File.OpenText("Teams.txt");

                // Read the file's contents.

                while (!inputTeams.EndOfStream)

                {

                    // Read a line and add it to the ListBox.

                    teamName = inputTeams.ReadLine();

                    lst_teams.Items.Add(teamName);

                }

                // Close the file.

                inputTeams.Close();

            }

            catch (Exception ex)

            {

                // Display an error message.

                MessageBox.Show(ex.Message);

            }

      }

        // The GetTeamWin method accepts a WorldSeries object as an argument. It counts

        // the total number of times a team has won the World Series Champions.

        private void GetTeamWin (WorldSeries worldSeries)

        {

            try

            {

                //Local Variables

                int index=0;      // Loop counter, initialized to 0.

                int winCount = 0; // Accumulator, initialized to 0.

                // Open the WorldSeriesWinners.txt file.

                StreamReader inputFile = File.OpenText("WorldSeriesWinners.txt");

                // Create a List object to hold strings.

                List<string> winnerList = new List<string>();

                // Read the file's contents

                while (!inputFile.EndOfStream)

                {

                    // Read a line and add it to the list.

                    winnerList.Add(inputFile.ReadLine());

                }

                // Sort the items in the List.

                winnerList.Sort();

                while (index >=0)

                {

                    // Search the team name in the List

                    index = winnerList.BinarySearch(worldSeries.Selected);

                    winCount++;

                    // Remove the team name from the List

                    winnerList.RemoveAt(index);

                }

                // Store the total number of wins of the team in the Wins

                // parameter.

                worldSeries.Wins = winCount.ToString();

                // Clear the List

                winnerList.Clear();

                // Display the number of times the team has won.

                lbl_results.Text = "The" + worldSeries.Selected + "has won the World Series" +

                winCount + "time(s), between 1903 and 2012.";

                   

            }

            catch (Exception ex)

            {

                // Display an error message.

                MessageBox.Show(ex.Message);

            }

        }

        private void btn_Exit_Click(object sender, EventArgs e)

        {

            // Close the file.

            this.Close();

        }

    }

}

Explanation / Answer

The number of teams wins is easily small enough to hold in memory. You can read the whole file once and store a dictionary of the team name to the number of wins in memory. Something like this:

You could then have simple function that checked if the selected team was in this list and returned the no of wins if so, if not, zero:

which could easily be used in your existing code: