public static List<Player> findTopPlayers(List<Player> listOfPlayers) { Player topCenter = new Player(); Player topRightWing = new Player(); Player topLeftWing = new Player(); Player topDefense = new Player(); Player topGoalie = new Player(); List<Player> topPlayers = new List<Player>(); //TODO CHANGE TO SWITCH STATEMENT FOR BETTER PERFORMANCE foreach(Player player in listOfPlayers) { switch (player.position) { case "C": if (player.avgPoints > topCenter.avgPoints) topCenter = player; continue; case "RW": if (player.avgPoints > topRightWing.avgPoints) topRightWing = player; continue; case "LW": if (player.avgPoints > topLeftWing.avgPoints) topLeftWing = player; continue; case "D": if (player.avgPoints > topDefense.avgPoints) topDefense = player; continue; case "G": if (player.avgPoints > topGoalie.avgPoints) topGoalie = player; continue; default: continue; } } topPlayers.Add(topCenter); topPlayers.Add(topRightWing); topPlayers.Add(topLeftWing); topPlayers.Add(topDefense); topPlayers.Add(topGoalie); return topPlayers; }
public static List<Player> parseCSV(string filename) { List<Player> listOfPlayers = new List<Player>(); List<string[]> csvLines = new List<string[]>(); FileInfo fileInfo = new FileInfo(filename); //parse the csv by reading each line using (TextFieldParser parser = new TextFieldParser(filename)) { parser.Delimiters = new string[] { "," }; while (!parser.EndOfData) { csvLines.Add(parser.ReadFields()); } } foreach (string[] line in csvLines) { if (line[0] == "Position") continue; //populate each players object with information from spreadsheet Player player = new Player(); player.position = line[0]; player.name = line[1]; player.salary = Convert.ToDouble(line[2]); player.gameInfo = line[3]; player.avgPoints = Convert.ToDecimal(line[4]); player.team = line[5]; //compile list of player objects listOfPlayers.Add(player); } return listOfPlayers; }