From e3855eb6694f1e8f04e706e09a72bf3c2acb9b7b Mon Sep 17 00:00:00 2001 From: Howard Pinsley Date: Sat, 5 Jan 2013 21:01:56 -0500 Subject: [PATCH] Show how the grade for a single team is computed. --- NFLRanking/Program.cs | 28 +++++++++++++++++++++++++++- ScoreParser/Game.cs | 12 +++++++++--- ScoreParser/Team.cs | 22 ++++++++++++++++++++-- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/NFLRanking/Program.cs b/NFLRanking/Program.cs index be84142..a3532d7 100644 --- a/NFLRanking/Program.cs +++ b/NFLRanking/Program.cs @@ -28,9 +28,35 @@ public static void GetStats() { } foreach (Team team in teamManager.GetTeams()) { - LogMsg("Team Manager has {0}", team); + LogMsg("{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); + } + } + LogMsg("\n{0} final grade is {1}", team.Name, team.Grade); + } } } diff --git a/ScoreParser/Game.cs b/ScoreParser/Game.cs index 712e026..acd36b2 100644 --- a/ScoreParser/Game.cs +++ b/ScoreParser/Game.cs @@ -8,14 +8,15 @@ public class Game { public Team Winner { get; private set; } public Team Loser { get; private set; } public bool IsTie { get; private set; } + public string Score { get; private set; } public Game(ITeamManager teamManager) { _teamManager = teamManager; } public override string ToString() { - return string.Format("Game:[{0}]; Winner:{1} Loser:{2} {3}", - GameDescription, Winner, Loser, IsTie ? "A Tie!" : ""); + return string.Format("{0} over {1} {2} {3}", + Winner, Loser, Score, IsTie ? "A Tie!" : ""); } public void InitFromGameDescription(string gameDescription) { @@ -35,7 +36,10 @@ public void InitFromGameDescription(string gameDescription) { Loser = _teamManager.GetTeam(teamAndScore2.Item1); } - IsTie = teamAndScore1.Item2 == teamAndScore2.Item2; + int score1 = teamAndScore1.Item2; + int score2 = teamAndScore2.Item2; + Score = score1 > score2 ? string.Format("{0}-{1}", score1, score2) : string.Format("{0}-{1}", score2, score1); + IsTie = score1 == score2; if (IsTie) { Winner.AddTie(); @@ -45,6 +49,8 @@ public void InitFromGameDescription(string gameDescription) { Winner.AddWin(); Loser.AddLoss(); } + Winner.AddGame(this); + Loser.AddGame(this); } private Tuple GetTeamAndScore(string teamAndScore) { diff --git a/ScoreParser/Team.cs b/ScoreParser/Team.cs index edf247b..9691dac 100644 --- a/ScoreParser/Team.cs +++ b/ScoreParser/Team.cs @@ -1,4 +1,6 @@ -namespace ScoreParser { +using System.Collections.Generic; + +namespace ScoreParser { public class Team { public string Name { get; private set; } @@ -6,6 +8,11 @@ public class Team { public int Losses { get; private set; } public int Ties { get; private set; } public int Grade { get; private set; } + public string Record { + get { return string.Format("{0}-{1}-{2}", Wins, Losses, Ties); } + } + + private List _games = new List(); public Team(string name) { Name = name; @@ -14,19 +21,30 @@ public Team(string name) { public void AddWin() { ++Wins; } + public void AddLoss() { ++Losses; } + public void AddTie() { ++Ties; } + public void AddGame(Game game) { + _games.Add(game); + } + public void IncrementGrade(int amount) { Grade += amount; } + public IEnumerable GetGames() { + return _games; + } + public override string ToString() { - return string.Format("{0} {1}-{2}-{3}: Grade: {4}", Name, Wins, Losses, Ties, Grade); + return Name; } + } } \ No newline at end of file