-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpense.cpp
More file actions
258 lines (218 loc) · 6.86 KB
/
Expense.cpp
File metadata and controls
258 lines (218 loc) · 6.86 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Expense.cpp
*
* Created on: 27 Nov 2011
* Author: Max Foster
*/
#include <fstream>
#include <stdexcept>
#include <cmath>
#include <ctime>
#include <cstring>
using namespace std;
#include "Expense.h"
#include "Utils.h"
const string Expense::databaseFilename = "expenses.dat";
const int Expense::minDescriptionLength = 0, Expense::maxDescriptionLength = 256;
int Expense::size()
{
return Record::size() + sizeof(unsigned) + maxDescriptionLength + 1 + (sizeof(double) * 2) + sizeof(int);
}
Expense::Expense(const time_t date, const char *newDescription, const double price, const double vat, const int type)
: date(date == 0 ? time(NULL) : date), price(price), vat(vat), type(type)
{
description = new char[maxDescriptionLength + 1];
strcpy(description, newDescription);
}
Expense::Expense(const Expense &expense)
{
description = new char[maxDescriptionLength + 1];
(*this) = expense;
}
Expense::~Expense()
{
delete[] description;
}
void Expense::operator =(const Expense &expense)
{
*((Record*)this) = (Record)expense;
date = expense.date;
strcpy(description, expense.description);
price = expense.price;
vat = expense.vat;
type = expense.type;
}
void Expense::writeToFile(fstream &file) const
{
Record::writeToFile(file);
file.write(reinterpret_cast<const char *>(&date), sizeof(date));
file.write(description, maxDescriptionLength + 1);
file.write(reinterpret_cast<const char *>(&price), sizeof(price));
file.write(reinterpret_cast<const char *>(&vat), sizeof(vat));
file.write(reinterpret_cast<const char *>(&type), sizeof(type));
}
void Expense::readFromFile(fstream &file)
{
Record::readFromFile(file);
file.read(reinterpret_cast<char *>(&date), sizeof(date));
file.read(description, maxDescriptionLength + 1);
file.read(reinterpret_cast<char *>(&price), sizeof(price));
file.read(reinterpret_cast<char *>(&vat), sizeof(vat));
file.read(reinterpret_cast<char *>(&type), sizeof(type));
}
bool Expense::hasMatchingField(const string &fieldName, const time_t searchTerm) const
{
if (fieldName == "date") return (date == searchTerm);
return false;
}
bool Expense::hasMatchingField(const string &fieldName, const char *searchTerm) const
{
if (fieldName == "description") return (strcmp(description, searchTerm) == 0);
return false;
}
bool Expense::hasMatchingField(const string &fieldName, const double searchTerm) const
{
if (fieldName == "price") return (price == searchTerm);
else if (fieldName == "vat") return (vat == searchTerm);
return false;
}
bool Expense::hasMatchingField(const string &fieldName, const int searchTerm) const
{
if (fieldName == "type") return (type == searchTerm);
return Record::hasMatchingField(fieldName, searchTerm);
}
bool Expense::fieldCompare(const Expense &rhs) const
{
if (date != rhs.date) return false;
if (strcmp(description, rhs.description) != 0) return false;
if (fabs(price - rhs.price) > 0.00001) return false; // Accouting for floating point error
if (fabs(vat - rhs.vat) > 0.00001) return false;
if (type != rhs.type) return false;
return true;
}
bool Expense::completeCompare(const Expense &rhs) const
{
if (getId() != rhs.getId()) return false;
return fieldCompare(rhs);
}
time_t Expense::getDate() const
{
return date;
}
void Expense::setDate(const time_t newDate)
{
string errorMessage;
if (isValidDate(newDate, errorMessage)) date = newDate;
else throw std::runtime_error(errorMessage);
}
bool Expense::isValidDate(const time_t, std::string &)
{
return true;
}
const char * Expense::getDescription() const
{
return description;
}
void Expense::setDescription(const char *newDescription)
{
string errorMessage;
if (isValidDescription(newDescription, errorMessage)) strcpy(description, newDescription);
else throw std::runtime_error(errorMessage);
}
bool Expense::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);
}
double Expense::getPrice() const
{
return price;
}
void Expense::setPrice(const double newPrice)
{
string errorMessage;
if (isValidPrice(newPrice, errorMessage)) price = newPrice;
else throw std::runtime_error(errorMessage);
}
bool Expense::isValidPrice(const double value, std::string &errorMessage)
{
if (value >= 0.01) return true;
errorMessage = "Price must be greater than 0.00";
return false;
}
double Expense::getVat() const
{
return vat;
}
void Expense::setVat(const double newVat)
{
string errorMessage;
if (isValidVat(newVat, errorMessage)) vat = newVat;
else throw std::runtime_error(errorMessage);
}
bool Expense::isValidVat(const double value, std::string &errorMessage)
{
if (value >= 0.0) return true;
errorMessage = "VAT cost must be greater than 0.00";
return false;
}
int Expense::getType() const
{
return type;
}
string Expense::getTypeString() const
{
switch (type)
{
case STOCK_MATERIALS: return "Stock/Materials";
case SUBCONTRACTOR: return "Subcontractor";
case PREMISES: return "Premises";
case REPAIRS: return "Repairs";
case ADMIN: return "Admin";
case MOTOR: return "Motor";
case ADVERTISING: return "Avertising";
case DEBT: return "Debt";
default: return "Other";
}
}
void Expense::setType(const int newType)
{
string errorMessage;
if (isValidType(newType, errorMessage)) type = newType;
else throw std::runtime_error(errorMessage);
}
bool Expense::isValidType(const double value, std::string &errorMessage)
{
if (value >= 0) return true;
errorMessage = "Type heading must be at least 0";
return false;
}
double Expense::getTotalPrice() const
{
return price + vat;
}
void Expense::validate() const
{
string errorMessage;
if (!isValidDate(date, errorMessage)) throw std::runtime_error(errorMessage);
if (!isValidDescription(description, errorMessage)) throw std::runtime_error(errorMessage);
if (!isValidPrice(price, errorMessage)) throw std::runtime_error(errorMessage);
if (!isValidVat(vat, errorMessage)) throw std::runtime_error(errorMessage);
if (!isValidType(type, errorMessage)) throw std::runtime_error(errorMessage);
}
ostream & operator <<(ostream &stream, const Expense &expense)
{
stream << "***************" << endl
<< " ID : " << expense.getId() << endl
<< " Date : " << Date(expense.getDate()) << endl
<< "Description : " << expense.getDescription() << endl
<< " Price : " << expense.getPrice() << endl
<< " VAT : " << expense.getVat() << endl
<< " Type : " << expense.getTypeString() << endl
<< "***************" << endl;
return stream;
}