2.1k views
1 vote
Create an application that displays the contents of the Teams. Txt file in a ListBox control Make a working version of this program in C#. 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 through 2012. On this Assignment page, you will find the following files inside the attached Assignment File:Teams. Txt—This file contains a list of several Major League baseball teams in alphabetical order. Each team listed in the file has won the World Series at least once. WorldSeriesWinners. Txt—This file contains a chronological list of the World Series’ winning teams from 1903 through 2012. (The first line in the file is the name 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. )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

User EWit
by
7.9k points

1 Answer

5 votes

Answer:

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Windows.Forms;

namespace WorldSeriesWinners

{

public partial class MainForm : Form

{

private List<string> worldSeriesWinners;

public MainForm()

{

InitializeComponent();

LoadTeams();

LoadWorldSeriesWinners();

}

private void LoadTeams()

{

string teamsFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Teams.txt");

List<string> teams = File.ReadAllLines(teamsFile).ToList();

listBoxTeams.DataSource = teams;

}

private void LoadWorldSeriesWinners()

{

string worldSeriesWinnersFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WorldSeriesWinners.txt");

worldSeriesWinners = File.ReadAllLines(worldSeriesWinnersFile).ToList();

}

private void listBoxTeams_SelectedIndexChanged(object sender, EventArgs e)

{

string selectedTeam = listBoxTeams.SelectedItem.ToString();

int wins = worldSeriesWinners.Count(x => x == selectedTeam);

MessageBox.Show(selectedTeam + " has won the World Series " + wins + " times.");

}

}

}

Step-by-step explanation:

User Gbestard
by
7.9k points