-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask.cpp
More file actions
183 lines (153 loc) · 4.51 KB
/
Task.cpp
File metadata and controls
183 lines (153 loc) · 4.51 KB
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
/*
* Task.cpp
*
* Created on: 27 Nov 2011
* Author: Max Foster
*/
#include <fstream>
#include <stdexcept>
#include <ctime>
#include <cstring>
using namespace std;
#include "Task.h"
#include "Utils.h"
const std::string Task::databaseFilename = "tasks.dat";
const int Task::minDescriptionLength = 0, Task::maxDescriptionLength = 256;
int Task::size()
{
return Record::size() + sizeof(int) + sizeof(time_t) + maxDescriptionLength + 1;
}
Task::Task(const int jobId, const time_t date, const char *newDescription)
: pending(false), jobId(jobId), date(date == 0 ? time(NULL) : date)
{
description = new char[maxDescriptionLength + 1];
strcpy(description, newDescription);
}
Task::Task(const Task &task)
{
description = new char[maxDescriptionLength + 1];
(*this) = task;
}
Task::~Task()
{
delete[] description;
}
void Task::operator =(const Task &task)
{
pending = task.pending;
*((Record*)this) = (Record)task;
jobId = task.jobId;
date = task.date;
strcpy(description, task.description);
}
void Task::writeToFile(fstream &file) const
{
Record::writeToFile(file);
file.write(reinterpret_cast<const char *>(&jobId), sizeof(jobId));
file.write(reinterpret_cast<const char *>(&date), sizeof(date));
file.write(description, maxDescriptionLength + 1);
}
void Task::readFromFile(fstream &file)
{
Record::readFromFile(file);
file.read(reinterpret_cast<char *>(&jobId), sizeof(jobId));
file.read(reinterpret_cast<char *>(&date), sizeof(date));
file.read(description, maxDescriptionLength + 1);
}
bool Task::hasMatchingField(const string &fieldName, const int searchTerm) const
{
if (fieldName == "jobid") return (jobId == searchTerm);
return Record::hasMatchingField(fieldName, searchTerm);
}
bool Task::hasMatchingField(const string &fieldName, const time_t searchTerm) const
{
if (fieldName == "date") return (date == searchTerm);
return false;
}
bool Task::hasMatchingField(const string &fieldName, const char *searchTerm) const
{
if (fieldName == "description") return (strcmp(description, searchTerm) == 0);
return false;
}
bool Task::hasMatchingField(const string &, const double) const
{
return false;
}
bool Task::fieldCompare(const Task &rhs) const
{
if (jobId != rhs.jobId) return false;
if (date != rhs.date) return false;
if (strcmp(description, rhs.description) != 0) return false;
return true;
}
bool Task::completeCompare(const Task &rhs) const
{
if (getId() != rhs.getId()) return false;
return fieldCompare(rhs);
}
int Task::getJobId() const
{
return jobId;
}
void Task::setJobId(const int newJobId)
{
string errorMessage;
if (isValidJobId(newJobId, errorMessage)) jobId = newJobId;
else throw std::runtime_error(errorMessage);
}
bool Task::isValidJobId(const int value, std::string &errorMessage)
{
if (value >= 0) return true;
errorMessage = "Job ID must be at least 0";
return false;
}
time_t Task::getDate() const
{
return date;
}
void Task::setDate(const time_t newDate)
{
string errorMessage;
if (isValidDate(newDate, errorMessage)) date = newDate;
else throw std::runtime_error(errorMessage);
}
bool Task::isValidDate(const time_t, std::string &)
{
return true;
}
const char * Task::getDescription() const
{
return description;
}
void Task::setDescription(const char *newDescription)
{
string errorMessage;
if (isValidDescription(newDescription, errorMessage)) strcpy(description, newDescription);
else throw std::runtime_error(errorMessage);
}
bool Task::isValidDescription(const char *value, std::string &errorMessage)
{
if ((value == NULL) || (value[0] == '\0'))
{
errorMessage = "Description must be present";
return false;
}
return validateLengthOf(value, maxDescriptionLength, "Description", errorMessage);
}
void Task::validate() const
{
string errorMessage;
if (!isValidJobId(jobId, errorMessage)) throw std::runtime_error(errorMessage);
if (!isValidDate(date, errorMessage)) throw std::runtime_error(errorMessage);
if (!isValidDescription(description, errorMessage)) throw std::runtime_error(errorMessage);
}
ostream & operator <<(ostream &stream, const Task &task)
{
stream << "***************" << endl
<< " ID : " << task.getId() << endl
<< " Job ID : " << task.getJobId() << endl
<< " Date : " << Date(task.getDate()) << endl
<< " Description : " << task.getDescription() << endl
<< "***************" << endl;
return stream;
}