-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRankListElement.cs
75 lines (61 loc) · 2.03 KB
/
RankListElement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
namespace Millionaire
{
/// <summary>
/// Stores the information of a user
/// </summary>
class RankListElement
{
/// <summary>
/// Gets the username
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Gets the score of the user
/// </summary>
public int Score { get; private set; }
/// <summary>
/// Gets the date of the last game
/// </summary>
public DateTime Date { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="RankListElement"/> class
/// </summary>
/// <remarks><c>Constructor</c></remarks>
/// <param name="name">username</param>
/// <param name="score">score of the user</param>
/// <param name="date">date of the game</param>
public RankListElement(string name, int score, DateTime date)
{
Name = name;
Score = score;
Date = date;
}
public override bool Equals(object obj)
{
if (obj == null) return false;
if (obj.GetType() != this.GetType()) return false;
if (obj.GetHashCode() != this.GetHashCode()) return false;
if (obj.ToString() != this.ToString()) return false;
return true;
}
public override int GetHashCode()
{
// String folding hash function
// SOURCE: https://opendsa-server.cs.vt.edu/ODSA/Books/Everything/html/HashFuncExamp.html#string-folding
string temp = ToString();
long hash = 0;
long multiplier = 1;
for (int i = 0; i < temp.Length; i++)
{
multiplier = i % 4 == 0 ? 1 : multiplier * 256;
hash += temp[i] * multiplier;
}
return (int)(Math.Abs(hash) % int.MaxValue);
}
public override string ToString()
{
return $"{Name};{Score};{Date}";
}
}
}