I am trying to make an application that lets users fill in a prediction and score points based on their prediction and the actual outcome.
I have this method that is called GetPointsPrediction(). For now, the prediction has this form:
Dictionary<int, Driver> predictions = new Dictionary<int, Driver>
{
{1, new Driver(10, "Michael Schumacher")},
{2, new Driver(8, "Jensen Button")},
{3, new Driver(7, "Felipe Massa")},
{4, new Driver(9, "Fernando Alonso")}
};
The integers are the positions users think a driver will finish on. Now, I need to be able to calculate points based on the prediction and the results. To give points, I need three pieces of information in the results: position, driver, and points.
The drivers that have finished exactly at the position they were predicted receives full points. Drivers that have not finished at the predicted position but are in the top ten get reduced points.
All the sequential numbers you see are the positions.
Solution number 1:
Have a separate results set, and a dictionary with points for each position which will be used as a look up:
Dictionary<int, Driver> results = new Dictionary<int, Driver>
{
{1, new Driver(10, "Michael Schumacher")},
{2, new Driver(8, "Jensen Button")},
{3, new Driver(9, "Fernando Alonso")}
};
Dictionary<int, int> points = new Dictionary<int, int>
{
{1, 25},
{2, 18},
{3, 15},
{4, 12},
{5, 10}
};
Solution number 2:
Merging the points and the results.
Dictionary<int, Dictionary<int, Driver>> results = new Dictionary<int, Dictionary<int, Driver>>
Solution number 3:
Coming up with some kind of class that can hold everything:
public class DriverResult
{
public Driver Driver { get; private set; }
public int Points { get; private set; }
public int StartPosition { get; private set; }
public int FinishPosition { get; private set; }
}
And then
IEnumerable<DriverResult> raceResults = new List<DriverResult>
I like solution number 3, but I kind of feel that it isn’t coherent enough, and the name doesn’t feel good either. Solution 2 is probably to ugly to really be used, and solution 1 actually gives me a good way to separate the correctly predicted drivers with Intersect.
Maybe there’s some other solution I haven’t thought. What are the best practices concerning these design decisions?
The Driver class:
public class Driver : IEquatable<Driver>
{
public int DriverId { get; private set; }
public string Name { get; private set; }
public Driver(int driverId, string name)
{
DriverId = driverId;
Name = name;
}
public override bool Equals(object obj)
{
return Equals(obj as Driver);
}
public bool Equals(Driver other)
{
return other != null && other.DriverId == DriverId;
}
public override int GetHashCode()
{
return DriverId.GetHashCode();
}
}
Since predictions are sequential by position, they should be an array. Since points are also by position which is sequential, that should also be an array. There is no sense in using a Dictionary. However, if you want to look up your drivers by ID, you would need to make a Dictionary or SortedList of your drivers with ID being the key, not position which is sequential. So you’d have two arrays:
Index with position – 1, e.g. predictions[0] is prediction for position 1. Similarly, use an array for points:
Access points by position – 1, e.g. points[0] is points for position 1.
Instead of having a DriverResults, consider adding the other fields like DriverPoints and StartPosition to Driver. Then you can just update the Driver classes in the array.
Edit: To store various race, test, season etc. results, create a class called DriverResults and have classes like RaceResults or SeasonResults inherit from it. Add a member to Driver:
Now you can create SeasonResults or RaceResults and add them to this list, storing as many results as you want.