Guest User

Untitled

a guest
Feb 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. class AttendanceClock : Dictionary<Day, List<WorkSession>>
  2.  
  3. enum Day
  4. {
  5. Sunday = 1,
  6. Monday,
  7. Tuesday,
  8. Wednesday,
  9. Thursday,
  10. Friday
  11. }
  12.  
  13. public class WorkSession : IEquatable<WorkSession>
  14. {
  15. public Time Entrance { get; private set; }
  16. public Time Exit { get; private set; }
  17.  
  18. public WorkSession(Time entrance, Time exit)
  19. {
  20. Entrance = entrance;
  21. Exit = exit;
  22. }
  23.  
  24. public override bool Equals(object obj)
  25. {
  26. return Equals(obj as WorkSession);
  27. }
  28. public bool Equals(WorkSession other)
  29. {
  30. return other != null
  31. && Entrance == other.Entrance
  32. && Exit == other.Exit;
  33. }
  34.  
  35. public override int GetHashCode()
  36. {
  37. var hashCode = 1257807568;
  38. hashCode = hashCode * -1521134295 + EqualityComparer<Time>.Default.GetHashCode(Entrance);
  39. hashCode = hashCode * -1521134295 + EqualityComparer<Time>.Default.GetHashCode(Exit);
  40. return hashCode;
  41. }
  42. }
  43.  
  44. class AttendanceClock : Dictionary<Day, List<WorkSession>>, IEquatable<AttendanceClock>
  45. {
  46. public bool Equals(AttendanceClock other)
  47. {
  48. foreach(KeyValuePair<Day, List<WorkSession>> keyValuePair in this)
  49. {
  50. Day day = keyValuePair.Key;
  51. List<WorkSession> workSessions = keyValuePair.Value;
  52.  
  53. if(other.TryGetValue(day, out var otherWorkSessions))
  54. {
  55. // ScrambledEquals is a helper method that compares
  56. // two IEnumerables by value regardless of the items
  57. // order, so {1, 2} is equal to {2, 1}
  58. if(!Helpers.ScrambledEquals(workSessions, otherWorkSessions))
  59. {
  60. return false;
  61. }
  62. }
  63. }
  64.  
  65. return true;
  66. }
  67. }
Add Comment
Please, Sign In to add comment