-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject.cs
197 lines (159 loc) · 4.97 KB
/
Project.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections.Generic;
using System.Text;
namespace Hours_Tracker
{
class Session
{
public bool isActive { get; private set; }
DateTime startTime;
DateTime stopTime;
TimeSpan time;
public Session(DateTime start, DateTime stop)
{
startTime = start;
stopTime = stop;
time = stop-start;
isActive = false;
}
public Session(DateTime start)
{
startTime = start;
isActive = true;
}
public void SetStop(DateTime stop)
{
stopTime = stop;
time = stop - startTime;
isActive = false;
}
public TimeSpan GetTime()
{
if (stopTime == DateTime.MinValue)
{
return DateTime.Now - startTime;
}
return time;
}
public string GetStartTime()
{
return startTime.ToString();
}
public string GetStopTime()
{
return (isActive) ? "Session is active, Clock Out to Set Stop Time" : stopTime.ToString();
}
public string GetTotalSessionTime()
{
return (isActive) ? "Entry is still active" : time.ToString();
}
public string GetSaveData(int index)
{
string output = "Session " + index.ToString();
output += "\nStart Time = \"" + startTime + "\"\n";
if (!isActive)
output += "Stop Time = \"" + stopTime + "\"\n";
return output;
}
}
class Project
{
public string projectName { get; private set; }
string[] projectData;
List<Session> sessions = new List<Session>();
Session latestSession;
/* Constructors */
// Project with Entries
public Project(string name, string[] data)
{
this.projectName = name;
this.projectData = data;
ParseData();
}
// Empty Project
public Project(string name)
{
this.projectName = name;
}
private void ParseData()
{
for(int i = 0; i < projectData.Length; i++)
{
if (projectData[i].Contains("Session"))
{
Session entry;
DateTime start;
DateTime stop;
if (i + 1 < projectData.Length)
{
i++;
int first = projectData[i].IndexOf("\"") + 1;
int last = projectData[i].LastIndexOf("\"");
DateTime.TryParse(projectData[i].Substring(first, last - first), out start);
}
else
continue;
if (i + 1 < projectData.Length)
{
i++;
int first = projectData[i].IndexOf("\"") + 1;
int last = projectData[i].LastIndexOf("\"");
DateTime.TryParse(projectData[i].Substring(first, last - first), out stop);
entry = new Session(start, stop);
}
else
{
entry = new Session(start);
}
sessions.Add(entry);
latestSession = entry;
}
}
}
public void OpenNewSession(DateTime start)
{
latestSession = new Session(start);
sessions.Add(latestSession);
}
public void CloseSession(DateTime stop)
{
latestSession.SetStop(stop);
}
public bool IsActive()
{
if (latestSession != null)
return latestSession.isActive;
return false;
}
public string GetTotalTime()
{
TimeSpan totalTime = new TimeSpan();
foreach (Session entry in sessions)
{
totalTime += entry.GetTime();
}
return (totalTime.Days * 24 + totalTime.Hours) + " Hours and " + totalTime.Minutes + " Minutes.";
}
public string[] GetSaveData()
{
string[] output = new string[sessions.Count + 2];
output[0] = "Project = *" + projectName + "*\n";
for (int i = 1; i < output.Length - 1; i++)
{
output[i] = sessions[i - 1].GetSaveData(i-1);
}
output[output.Length - 1] = "END\n";
return output;
}
public string RenameProject(string name)
{
string tempString = projectName;
projectName = name;
return tempString;
}
public List<Session> GetSessions()
{
return sessions;
}
}
}