Projekt

Obecné

Profil

Stáhnout (2.39 KB) Statistiky
| Větev: | Tag: | Revize:
1
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
		/// <summary>
23
		/// Increases the month by one. Increases year and sets month to 1 if need be.
24
		/// </summary>
25
		/// <returns>An increased date</returns>
26
		public Date IncreaseMonthByOne()
27
		{
28
			uint newMonth = Month;
29
			newMonth++;
30
			uint newYear = Year;
31
			if (newMonth > 12)
32
			{
33
				// newMonth must be 13
34
				newMonth = 1;
35
				newYear++;
36
			}
37

    
38
			return new Date(newMonth, newYear);
39
		}
40

    
41

    
42
		#region OVERRIDEN METHODS FOR OF THE OBJECT CLASS
43
		public override bool Equals(object obj)
44
		{
45
			if (obj.GetType() != typeof(Date))
46
				return false;
47

    
48
			Date other = obj as Date;
49
			if (other.Month == this.Month && other.Year == this.Year)
50
				return true;
51
			return false;
52
		}
53

    
54
		public override int GetHashCode()
55
		{
56
			int hashCode = -994906903;
57
			hashCode = hashCode * -1521134295 + Month.GetHashCode();
58
			hashCode = hashCode * -1521134295 + Year.GetHashCode();
59
			return hashCode;
60
		}
61

    
62
		public override string ToString()
63
		{
64
			string mon = Month > 9 ? $"{Month}" : "0" + Month;
65
			return $"{mon}-{Year}";
66
		}
67

    
68
		#endregion
69

    
70
		#region OVERLOADED OPERATORS
71
		public static bool operator >(Date d1, Date d2)
72
		{
73
			if (d1.Year > d2.Year)
74
				return true;
75
			else if (d1.Year < d2.Year)
76
				return false;
77
			else
78
			{
79
				// the years are equal
80
				if (d1.Month > d2.Month)
81
					return true;
82
				else return false;
83
			}
84

    
85
		}
86

    
87
		public static bool operator <(Date d1, Date d2)
88
		{
89
			return !(d1 >= d2);
90
		}
91

    
92

    
93
		public static bool operator >=(Date d1, Date d2)
94
		{
95
			if (d1.Year > d2.Year)
96
				return true;
97
			else if (d1.Year < d2.Year)
98
				return false;
99
			else
100
			{
101
				// the years are equal
102
				if (d1.Month > d2.Month)
103
					return true;
104
				else if (d1.Month == d2.Month)
105
					return true;
106
				else return false;
107
			}
108
		}
109

    
110
		public static bool operator <=(Date d1, Date d2)
111
		{
112
			return !(d1 > d2);
113
		}
114

    
115
		public static bool operator ==(Date d1, Date d2)
116
		{
117
			return d1.Equals(d2);
118
		}
119

    
120
		public static bool operator !=(Date d1, Date d2)
121
		{
122
			return !d1.Equals(d2);
123
		}
124

    
125
		#endregion
126
	}
127
}
(2-2/2)