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

Chapter 7, Problem 5PP in Starting out with Visual C# (4th Edition) World Series

ID: 3577059 • Letter: C

Question

Chapter 7, Problem 5PP in Starting out with Visual C# (4th Edition)

World Series Champions

Teams.txt - This ile contains a list of several Major League baseball teams in alphabetical order. Each team listed in the file has one the World Series at least once.

WorldSeriesWinners.txt - This file contains a chronological list of the World Series' winning teams from 1903 through 2012.

Create an 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 form 1903 through 2012.

Tip: Read the contents of the WorldSeriesWinners.txt file into a List or an array. When the user selects a team, an algorithm should step through the list or array counting the number of times the selected team appears.

Use a (drop down list) combo box for teams, rather than a list box.

Read the files at Form Load time only.
This requires a private (class level) List or array variable. Keep all other variables local.

In addition, your solution must accommodate not only the provided file (WorldSeriesWinners.txt), but one of any size.

Display the number of wins in the combo box’s SelectedIndexChanged event

Create (Integer) method CalculateWins with a string parameter selectedTeam

Pass the selected item of the combo box as an argument to selectedTeam

Explanation / Answer

// A c# program display the number of times that team has won the world series in the time period form 1903 through 2012.

using System;
using System.Windows.Forms;

namespace WorldSeriesChampions
{
   public partial class Form1 : Form
   {
       public Form1
       {
           InitializeComponent();
       }
      
       // code for the form which has a dropdown combobox list abd label.
       private void Form1_Load(object sender, EventArgs e)
   {      
           comboBox1.Sorted = true;
           comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
      
           // read contents of Team.txt file into a string array.
           string[] lineOfContents = File.ReadAllLines("Teams.txt");
      
           //add each team name into dropdown combobox.
           foreach (var line in lineOfContents)
           {
               comboBox1.Items.Add(line);           
           }
      }

      // code that runs when a team is selected in combobox list by user, and displays no. of times the team won world series.
      private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
           if(selectedTeam(comboBox1.Text) == 0)
               label1.Text = comboBox1.Text + "never WON the world series";
           else
               label1.Text = comboBox1.Text + "WON" + selectedTeam(comboBox1.Text) + "times";
label1.BorderStyle = BorderStyle.FixedSingle;
label1.TextAlign = ContentAlignment.MiddleCenter;
       }

       // function to find no. of times a team won world series.
       private int selectedTeam(string team)
       {
           // read contents of WorldSeriesWinners.txt file into a string array
           string[] listOfWinners = File.ReadAllLines("WorldSeriesWinners.txt");
           int score = 0;
           foreach (var line in listOfWinners)
           {
               if(line.Equals(team))
                   score++;          
           }
           return score;
       }
   }  
}