Hi All, I am new and just got an interview coding question. My code is below. The feedback was unsatisfied but I cannot see why. The code is fully tested.
Definitely need a second eye on it. Can someone tell me what's wrong with it? Thanks a bunch!
using System;
namespace Invision.Test
{
public class Team
{
public enum FieldAdvantage { Home = 100, Away = 200 };
// declare a delegate to wire up score change event to the class
public delegate void ScoreChangeEventHandler(FieldAdvantage side, int newScore, ref bool overrule);
protected string _name;
protected FieldAdvantage _side;
protected int _score;
// declare the score change "event" which will be called whenever score value is changed
// this "event" will be set in the Referee class
public ScoreChangeEventHandler ScoreChange;
// this method will be called from the setter
// after execution, it sets the bool value passed from the caller
protected void OnScoreChanged(FieldAdvantage side, int newScore, ref bool overrule)
{
if (ScoreChange != null)
{
ScoreChange(side, newScore, ref overrule);
}
}
public virtual int NumPlayers
{
get { return 0; }
}
public FieldAdvantage Side
{
get { return _side; }
set { _side = value; }
}
public int Score
{
get { return _score; }
set
{
_score = value;
bool overrule = false;
OnScoreChanged(_side, _score, ref overrule);
if (overrule)
{
_score -= 100;
if (_score < 0)
_score = 0;
}
}
}
public Team(string name, FieldAdvantage side)
{
_name = name;
_side = side;
}
// override ToString method for displaying the name field
public override string ToString()
{
return _name;
}
}
public class FootballTeam : Team
{
public FootballTeam(string name, FieldAdvantage side)
: base(name, side)
{
}
public override int NumPlayers
{
get
{
return 11;
}
}
public void Touchdown()
{
// calls the setter method of property Score
Score += 6;
}
}
public class Game
{
public FootballTeam Home = new FootballTeam("Local Boys", Team.FieldAdvantage.Home);
public FootballTeam Away = new FootballTeam("Tourists", Team.FieldAdvantage.Away);
public static void Main()
{
Game game = new Game();
Referee referee = new Referee(game);
game.Home.Touchdown();
game.Away.Touchdown();
game.Home.Score += 100;
Console.WriteLine("{0} = {1} ({2})", game.Home, game.Home.Score, game.Home.Side.ToString());
Console.WriteLine("{0} = {1} ({2})", game.Away, game.Away.Score, game.Away.Side.ToString());
}
}
public class Referee
{
public Referee(Game game)
{
game.Home.ScoreChange += new Team.ScoreChangeEventHandler(OnScoreChange);
game.Away.ScoreChange += new Team.ScoreChangeEventHandler(OnScoreChange);
}
private void OnScoreChange(Team.FieldAdvantage side, int newScore, ref bool overrule)
{
if ((side == Team.FieldAdvantage.Away) || (newScore > 100))
overrule = true;
}
}
}