我正在嘗試製作一個應用程序,讓用戶根據他們的預測和實際結果填寫預測並評分。什麼設計決策更適合計算預測和評分系統?
我有這種稱爲GetPointsPrediction()的方法。目前,預測有這樣的形式:
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")}
};
整數是用戶認爲司機會完成的職位。現在,我需要能夠根據預測和結果來計算積分。爲了給出積分,我需要結果中的三條信息:位置,駕駛員和積分。
準確完成預測位置的驅動程序會獲得滿分。尚未完成預測位置但進入前十的車手可獲得減分。
你看到的所有序號都是位置。
方案編號1:
有一個單獨的結果集,並且與其中將用作查找每個位置點字典:
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}
};
方案編號2:
合併點和結果。
Dictionary<int, Dictionary<int, Driver>> results = new Dictionary<int, Dictionary<int, Driver>>
解數3:
想出某種類的,可以容納一切:
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; }
}
然後
IEnumerable<DriverResult> raceResults = new List<DriverResult>
我喜歡解3號,但我覺得它不夠連貫,名字也不舒服。解決方案2可能難以真正使用,解決方案1實際上給了我一個很好的方法來將正確預測的驅動程序與Intersect分開。
也許還有一些其他的解決方案,我沒有想到。有關這些設計決策的最佳實踐是什麼?
驅動程序類:
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();
}
}
我不清楚你在做什麼。 Driver類中有什麼?爲什麼你所有的指數是連續的?如果他們是,爲什麼不使用數組而不是字典?例如,int points [] = new int [] {25,18,15,12,10}; –
我添加了驅動程序類,並解釋了順序事件是關於什麼的(它們是完成位置);我需要一本字典(我認爲),因爲我需要屬於某個位置的點。 –
但是,如果職位是連續的,那麼你會使用數組而不是字典。字典(或SortedLists)適用於您有稀疏數據並需要查找的情況。例如,您可能會爲自己的驅動程序自己使用字典,因此您可以通過ID查找一個字符串,因爲ID可能爲1或1002. –