Skip to content

Commit

Permalink
Put grade analsysis in one place and refactored the analysis of how a…
Browse files Browse the repository at this point in the history
… team got its grade into separate classes.
  • Loading branch information
hpinsley committed Jan 6, 2013
1 parent e3855eb commit cec0d32
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 101 deletions.
72 changes: 27 additions & 45 deletions NFLRanking/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,42 @@

namespace NFLRanking {
class Program {
static void Main() {
GetStats();
}


private const string NFL2012 = @"C:\Users\pinsley\Google Drive\NFL Results\NFL-2012-Results.txt";

protected static void LogMsg(string fmt, params object[] args) {
string msg = string.Format(fmt, args);
LogMsg(msg);
}

protected static void LogMsg(string msg) {
Console.WriteLine(msg);
}

public static void GetStats() {
static void Main() {
ITeamManager teamManager = new TeamManager();
ResultsParser parser = new ResultsParser(teamManager);
Games games = parser.ParseGameResults(NFL2012);

foreach (Game game in games.GetGames()) {
LogMsg(game.ToString());
}
IGameManager gameManager = new GameManager(teamManager);
ResultsParser parser = new ResultsParser(gameManager);

parser.ParseGameResults(NFL2012);
Console.WriteLine();
ListTeams(teamManager);
Console.WriteLine();
AnalyzeTeam(teamManager.GetTeam("NY Giants"));
Console.WriteLine();
AnalyzeTeam(teamManager.GetTeam("Washington"));
}

private static void ListTeams(ITeamManager teamManager) {
foreach (Team team in teamManager.GetTeams()) {
LogMsg("{0} is graded a {1}", team, team.Grade);
Console.WriteLine("{0} is graded a {1}", team, team.Grade);
}

Team giants = teamManager.GetTeam("NY Giants");
ShowTeamGames(giants);

}

private static void ShowTeamGames(Team team) {
LogMsg("Games for {0}", team.Name);
int cumGrade = 0;
int delta;
foreach (Game game in team.GetGames()) {
if (game.IsTie) {
LogMsg("{0} tied {1}",
team.Name, (team == game.Winner) ? game.Loser.Name : game.Winner.Name);
}
else if (team == game.Winner) {
delta = game.Loser.Wins;
cumGrade += delta;
Console.WriteLine("{0} beat {1} {2} {3}\tCredit:{4} Total:{5}", team.Name, game.Loser.Record, game.Loser, game.Score, delta, cumGrade);
}
else if (team == game.Loser) {
delta = -1 * game.Winner.Losses;
cumGrade += delta;
Console.WriteLine("{0} lost to {1} {2} {3}\tCredit:{4} Total:{5}", team.Name, game.Winner.Record, game.Winner, game.Score, delta, cumGrade);
}

private static void AnalyzeTeam(Team team) {
TeamAnalysis analysis = Grader.GetTeamGradeAnalysis(team);
foreach (TeamGameAnalysis tga in analysis.GetGameAnalyses()) {
Console.WriteLine("{0} {1} {2} {3} {4}\tCredit:{5} Total:{6}",
tga.Team.Name,
tga.RelationshipToOtherTeam,
tga.OtherTeam.Record,
tga.OtherTeam,
tga.Game.Score,
tga.GameGrade,
tga.CumulativeGrade);
}
LogMsg("\n{0} final grade is {1}", team.Name, team.Grade);
}
}
}
12 changes: 5 additions & 7 deletions ScoreParser.Tests/FileParserTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ public class FileParserTester : TestBase {
[Test]
public void CanGetStats() {
ITeamManager teamManager = new TeamManager();
ResultsParser parser = new ResultsParser(teamManager);
Games games = parser.ParseGameResults(NFL2012);
Assert.IsNotNull(games);
Assert.AreEqual(256, games.Count);
IGameManager gameManager = new GameManager(teamManager);

foreach (Game game in games.GetGames()) {
LogMsg(game.ToString());
}
ResultsParser parser = new ResultsParser(gameManager);
parser.ParseGameResults(NFL2012);

foreach (Team team in teamManager.GetTeams()) {
LogMsg("Team Manager has {0}", team);
}

Assert.AreEqual(32, teamManager.TeamCount);
Team giants = teamManager.GetTeam("NY Giants");
Assert.AreEqual(21, giants.Grade);
}
}
}
56 changes: 28 additions & 28 deletions ScoreParser/Games.cs → ScoreParser/GameManager.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
using System.Collections.Generic;

namespace ScoreParser {
public class Games {
private readonly ITeamManager _teamManager;

public Games(ITeamManager teamManager) {
_teamManager = teamManager;
}

private List<Game> games = new List<Game>();

public int Count {
get { return games.Count; }
}

public void AddMultiple(string[] lines) {
foreach (string gameDescription in lines) {
Game game = new Game(_teamManager);
game.InitFromGameDescription(gameDescription);
games.Add(game);
}
}

public IEnumerable<Game> GetGames() {
return games;
}
}
using System.Collections.Generic;

namespace ScoreParser {
public class GameManager : IGameManager {
private readonly ITeamManager _teamManager;

public GameManager(ITeamManager teamManager) {
_teamManager = teamManager;
}

private List<Game> games = new List<Game>();

public int Count {
get { return games.Count; }
}

public void AddMultiple(string[] lines) {
foreach (string gameDescription in lines) {
Game game = new Game(_teamManager);
game.InitFromGameDescription(gameDescription);
games.Add(game);
}
}

public IEnumerable<Game> GetGames() {
return games;
}
}
}
26 changes: 26 additions & 0 deletions ScoreParser/Grader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace ScoreParser {
public static class Grader {

public static int GradeTeamInGame(Team team, Game game) {
if (game.IsTie)
return 0;

if (team == game.Winner)
return game.Loser.Wins;

return -1*game.Winner.Losses;
}

public static TeamAnalysis GetTeamGradeAnalysis(Team team) {
TeamAnalysis analysis = new TeamAnalysis();
int cumGrade = 0;
foreach (Game game in team.GetGames()) {
TeamGameAnalysis teamGameAnalysis = new TeamGameAnalysis(team, game);
cumGrade += teamGameAnalysis.GameGrade;
teamGameAnalysis.CumulativeGrade = cumGrade;
analysis.AddTeamGameAnalysis(teamGameAnalysis);
}
return analysis;
}
}
}
9 changes: 9 additions & 0 deletions ScoreParser/IGameManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace ScoreParser {
public interface IGameManager {
int Count { get; }
void AddMultiple(string[] lines);
IEnumerable<Game> GetGames();
}
}
31 changes: 11 additions & 20 deletions ScoreParser/ResultsParser.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
using System;
using System.IO;
using System.IO;

namespace ScoreParser {
public class ResultsParser {
private IGameManager _gameManager;

private ITeamManager _teamManager;

public ResultsParser(ITeamManager teamManager) {
_teamManager = teamManager;
public ResultsParser(IGameManager gameManager) {
_gameManager = gameManager;
}

public Games ParseGameResults(string gameResultsFile) {
public void ParseGameResults(string gameResultsFile) {
string[] lines = File.ReadAllLines(gameResultsFile);

Games games = new Games(_teamManager);
games.AddMultiple(lines);

GradeTheTeams(games);
return games;
_gameManager.AddMultiple(lines);
GradeTheTeams();
}

private void GradeTheTeams(Games games) {
foreach (Game game in games.GetGames()) {
if (game.IsTie)
continue;

game.Winner.IncrementGrade(game.Loser.Wins);
game.Loser.IncrementGrade(-1 * game.Winner.Losses);
private void GradeTheTeams() {
foreach (Game game in _gameManager.GetGames()) {
game.Winner.IncrementGrade(Grader.GradeTeamInGame(game.Winner, game));
game.Loser.IncrementGrade(Grader.GradeTeamInGame(game.Loser, game));
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion ScoreParser/ScoreParser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Game.cs" />
<Compile Include="Games.cs" />
<Compile Include="GameManager.cs" />
<Compile Include="Grader.cs" />
<Compile Include="IGameManager.cs" />
<Compile Include="ITeamManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ResultsParser.cs" />
<Compile Include="Team.cs" />
<Compile Include="TeamAnalysis.cs" />
<Compile Include="TeamGameAnalysis.cs" />
<Compile Include="TeamManager.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
15 changes: 15 additions & 0 deletions ScoreParser/TeamAnalysis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;

namespace ScoreParser {
public class TeamAnalysis {
private List<TeamGameAnalysis> _teamGameAnalyses = new List<TeamGameAnalysis>();

public void AddTeamGameAnalysis(TeamGameAnalysis teamGameAnalysis) {
_teamGameAnalyses.Add(teamGameAnalysis);
}

public IEnumerable<TeamGameAnalysis> GetGameAnalyses() {
return _teamGameAnalyses;
}
}
}
31 changes: 31 additions & 0 deletions ScoreParser/TeamGameAnalysis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace ScoreParser {
public class TeamGameAnalysis {
public Game Game { get; private set; }
public Team Team { get; private set; }
public Team OtherTeam { get; private set; }
public string RelationshipToOtherTeam { get; private set; }
public int GameGrade { get; private set; }
public int CumulativeGrade { get; set; }

public TeamGameAnalysis(Team team, Game game) {
Team = team;
Game = game;

if (team == game.Winner) {
OtherTeam = game.Loser;
RelationshipToOtherTeam = "defeated";
}
else {
OtherTeam = game.Winner;
RelationshipToOtherTeam = "lost to";
}

if (game.IsTie) {
RelationshipToOtherTeam = "tied";
}

GameGrade = Grader.GradeTeamInGame(team, game);
}

}
}

0 comments on commit cec0d32

Please sign in to comment.