static void Main(string[] args) { HockeyTeam currentHockeyTeam = new HockeyTeam(); string inputFile = @"c:\temp\oilers.csv"; LoadHockeyTeam(currentHockeyTeam, inputFile); DisplayTeamAndPlayers(currentHockeyTeam); // Create a new Hocker player HockeyPlayer newHockeyPlayer = new HockeyPlayer(); newHockeyPlayer.FullName = "Dominik Kahun"; newHockeyPlayer.PrimaryNumber = 21; newHockeyPlayer.Position = PlayerPosition.C; // Add the new Hockey player to the team currentHockeyTeam.AddPlayer(newHockeyPlayer); string outputFile = @"c:\temp\flames.csv"; SaveTeam(currentHockeyTeam, outputFile); }
static void LoadHockeyTeam(HockeyTeam currentTeam, String inputFile) { // The input value will have the following format: // team name, division // player name, player number, player position // player name, player number, player position try { using (StreamReader reader = new StreamReader(inputFile)) { // The first line in the file contains the team name and division string currentLine = reader.ReadLine(); // Convert the single into array of values separated by a comma string[] currentLineArray = currentLine.Split(','); // Set the team name and division currentTeam.TeamName = currentLineArray[0]; currentTeam.Division = (HockeyDivision)int.Parse(currentLineArray[1]); // The remaining lines in the file contains the player info formatted: // player name, number, position while ((currentLine = reader.ReadLine()) != null) { currentLineArray = currentLine.Split(','); // Create a new HockeyPlayer to add to the Team HockeyPlayer currentPlayer = new HockeyPlayer(); // Set the player name, number, and position currentPlayer.FullName = currentLineArray[0]; currentPlayer.PrimaryNumber = int.Parse(currentLineArray[1]); currentPlayer.Position = (PlayerPosition)int.Parse(currentLineArray[2]); // Add the currentPlayer to the team currentTeam.AddPlayer(currentPlayer); } } } catch (Exception e) { Console.WriteLine($"Error reading from {inputFile} with exception {e.Message}"); } }