Projekt

Obecné

Profil

Stáhnout (2.24 KB) Statistiky
| Větev: | Tag: | Revize:
1 7a998d66 Eliška Mourycová
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
7
namespace ServerApp.DataDownload
8
{
9
	public class Date
10
	{
11
		public uint Month { get; }
12
		public uint Year { get; }
13
		public Date(uint month, uint year)
14
		{
15
			if (month == 0)
16
				throw new ArgumentOutOfRangeException("month", "Month must be positive and not zero.");
17
			this.Month = month;
18
			this.Year = year;
19
		}
20
21
22
23
		public Date IncreaseMonthByOne()
24
		{
25
			uint newMonth = Month;
26
			newMonth++;
27
			uint newYear = Year;
28
			if (newMonth > 12)
29
			{
30
				// newMonth must be 13
31
				newMonth = 1;
32
				newYear++;
33
			}
34
35
			return new Date(newMonth, newYear);
36
		}
37
38
39
		#region OVERRIDEN METHODS FOR OF THE OBJECT CLASS
40
		public override bool Equals(object obj)
41
		{
42
			if (obj.GetType() != typeof(Date))
43
				return false;
44
45
			Date other = obj as Date;
46
			if (other.Month == this.Month && other.Year == this.Year)
47
				return true;
48
			return false;
49
		}
50
51
		public override int GetHashCode()
52
		{
53
			int hashCode = -994906903;
54
			hashCode = hashCode * -1521134295 + Month.GetHashCode();
55
			hashCode = hashCode * -1521134295 + Year.GetHashCode();
56
			return hashCode;
57
		}
58
59
		public override string ToString()
60
		{
61
			string mon = Month > 9 ? $"{Month}" : "0" + Month;
62
			return $"{mon}-{Year}";
63
		}
64
65
		#endregion
66
67
		#region OVERLOADED OPERATORS
68
		public static bool operator >(Date d1, Date d2)
69
		{
70
			if (d1.Year > d2.Year)
71
				return true;
72
			else if (d1.Year < d2.Year)
73
				return false;
74
			else
75
			{
76
				// the years are equal
77
				if (d1.Month > d2.Month)
78
					return true;
79
				else return false;
80
			}
81
82
		}
83
84
		public static bool operator <(Date d1, Date d2)
85
		{
86
			return !(d1 >= d2);
87
		}
88
89
90
		public static bool operator >=(Date d1, Date d2)
91
		{
92
			if (d1.Year > d2.Year)
93
				return true;
94
			else if (d1.Year < d2.Year)
95
				return false;
96
			else
97
			{
98
				// the years are equal
99
				if (d1.Month > d2.Month)
100
					return true;
101
				else if (d1.Month == d2.Month)
102
					return true;
103
				else return false;
104
			}
105
		}
106
107
		public static bool operator <=(Date d1, Date d2)
108
		{
109
			return !(d1 > d2);
110
		}
111
112
		public static bool operator ==(Date d1, Date d2)
113
		{
114
			return d1.Equals(d2);
115
		}
116
117
		public static bool operator !=(Date d1, Date d2)
118
		{
119
			return !d1.Equals(d2);
120
		}
121
122
		#endregion
123
	}
124
}