-
Notifications
You must be signed in to change notification settings - Fork 0
/
Term.cs
83 lines (64 loc) · 1.79 KB
/
Term.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
76
77
78
79
80
81
82
83
namespace CourseView;
public enum TermType
{
Spring,
Summer,
Fall
}
public class InvalidTermStringException : Exception
{
public InvalidTermStringException() : base("Provided term string is invalid.")
{
}
}
public class Term
{
public int Year;
public TermType Type;
public override string ToString()
{
return $"{this.Year}{((int)this.Type + 1) * 10}";
}
public string ToLabelString()
{
return $"{this.Type.ToString()} {this.Year}";
}
protected static TermType GetTermType(int term)
{
if (term > 30)
return TermType.Fall;
if (term < 10)
return TermType.Summer;
return (TermType)((int)(term / 10) - 1);
}
protected static TermType GetCurrentTermType()
{
if (Tools.InRange(DateTime.Now.Month, 1, 5))
return TermType.Spring;
if (Tools.InRange(DateTime.Now.Month, 5, 9))
return TermType.Summer;
return TermType.Fall;
}
public static Term FromTermString(string termString)
{
string[] divided = Tools.DivideString(termString, new int[]{ 4, 2 });
return new Term(int.Parse(divided[0]), Term.GetTermType(int.Parse(divided[1])));
}
public static Term GetCurrent()
{
return new Term(DateTime.Now.Year, Term.GetCurrentTermType());
}
public Term(int year, TermType type)
{
this.Year = year;
this.Type = type;
}
public Term(string str)
{
if (!Tools.InRange(str.Length, 0, 7))
throw new InvalidTermStringException();
string[] dividedString = Tools.DivideString(str, new int[] {4, 2});
this.Type = Term.GetTermType(int.Parse(dividedString[1]));
this.Year = int.Parse(dividedString[0]);
}
}