-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart.cpp
More file actions
268 lines (226 loc) · 7.24 KB
/
Part.cpp
File metadata and controls
268 lines (226 loc) · 7.24 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
259
260
261
262
263
264
265
266
267
268
/*
* Part.cpp
*
* Created on: 27 Nov 2011
* Author: Max Foster
*/
#include <fstream>
#include <stdexcept>
#include <cmath>
#include <cstring>
using namespace std;
#include "Globals.h"
#include "Part.h"
#include "Utils.h"
const string Part::databaseFilename = "parts.dat";
const int
Part::minNameLength = 3, Part::maxNameLength = 24,
Part::minNumberLength = 0, Part::maxNumberLength = 32;
int Part::size()
{
return Record::size() + (sizeof(int) * 2) + maxNameLength + maxNumberLength + 2 + (sizeof(double) * 2);
}
Part::Part(const int jobId, const char *newName, const char *newNumber, const double price, const double vatRate,
const int quantity)
: pending(false), jobId(jobId), quantity(quantity), price(price),
vatRate(vatRate < 0.0 ? Globals::vatRate(time(NULL)) : vatRate)
{
name = new char[maxNameLength + 1];
number = new char[maxNumberLength + 1];
strcpy(name, newName);
strcpy(number, newNumber);
}
Part::Part(const Part &part)
{
name = new char[maxNameLength + 1];
number = new char[maxNumberLength + 1];
(*this) = part;
}
Part::~Part()
{
delete[] name;
delete[] number;
}
void Part::operator =(const Part &part)
{
pending = part.pending;
*((Record*)this) = (Record)part;
jobId = part.jobId;
strcpy(name, part.name);
strcpy(number, part.number);
price = part.price;
vatRate = part.vatRate;
quantity = part.quantity;
}
void Part::writeToFile(fstream &file) const
{
Record::writeToFile(file);
file.write(reinterpret_cast<const char *>(&jobId), sizeof(jobId));
file.write(name, maxNameLength + 1);
file.write(number, maxNumberLength + 1);
file.write(reinterpret_cast<const char *>(&price), sizeof(price));
file.write(reinterpret_cast<const char *>(&vatRate), sizeof(vatRate));
file.write(reinterpret_cast<const char *>(&quantity), sizeof(quantity));
}
void Part::readFromFile(fstream &file)
{
Record::readFromFile(file);
file.read(reinterpret_cast<char *>(&jobId), sizeof(jobId));
file.read(name, maxNameLength + 1);
file.read(number, maxNumberLength + 1);
file.read(reinterpret_cast<char *>(&price), sizeof(price));
file.read(reinterpret_cast<char *>(&vatRate), sizeof(vatRate));
file.read(reinterpret_cast<char *>(&quantity), sizeof(quantity));
}
bool Part::hasMatchingField(const string &fieldName, const int searchTerm) const
{
if (fieldName == "jobid") return (jobId == searchTerm);
else if (fieldName == "quantity") return (quantity == searchTerm);
return Record::hasMatchingField(fieldName, searchTerm);
}
bool Part::hasMatchingField(const string &fieldName, const char *searchTerm) const
{
if (fieldName == "name") return (strcmp(name, searchTerm) == 0);
else if (fieldName == "number") return (strcmp(number, searchTerm) == 0);
return false;
}
bool Part::hasMatchingField(const string &fieldName, const double searchTerm) const
{
if (fieldName == "price") return (price == searchTerm);
else if (fieldName == "vatRate") return (vatRate == searchTerm);
return false;
}
bool Part::fieldCompare(const Part &rhs) const
{
if (jobId != rhs.jobId) return false;
if (strcmp(name, rhs.name) != 0) return false;
if (strcmp(number, rhs.number) != 0) return false;
if (fabs(price - rhs.price) > 0.00001) return false;
if (fabs(vatRate - rhs.vatRate) > 0.00001) return false;
if (quantity != rhs.quantity) return false;
return true;
}
bool Part::completeCompare(const Part &rhs) const
{
if (getId() != rhs.getId()) return false;
return fieldCompare(rhs);
}
int Part::getJobId() const
{
return jobId;
}
void Part::setJobId(const int newJobId)
{
string errorMessage;
if (isValidJobId(newJobId, errorMessage)) jobId = newJobId;
else throw std::runtime_error(errorMessage);
}
bool Part::isValidJobId(const int value, std::string &errorMessage)
{
if (value >= 0) return true;
errorMessage = "Job ID must be at least 0";
return false;
}
const char * Part::getName() const
{
return name;
}
void Part::setName(const char *newName)
{
string errorMessage;
if (isValidName(newName, errorMessage)) strcpy(name, newName);
else throw std::runtime_error(errorMessage);
}
bool Part::isValidName(const char *value, std::string &errorMessage)
{
return validateLengthOf(value, minNameLength, maxNameLength, "Part name", errorMessage);
}
const char * Part::getNumber() const
{
return number;
}
void Part::setNumber(const char *newNumber)
{
string errorMessage;
if (isValidNumber(newNumber, errorMessage)) strcpy(number, newNumber);
else throw std::runtime_error(errorMessage);
}
bool Part::isValidNumber(const char *value, std::string &errorMessage)
{
return validateLengthOf(value, maxNumberLength, "Part number", errorMessage);
}
double Part::getPrice() const
{
return price;
}
void Part::setPrice(const double newPrice)
{
string errorMessage;
if (isValidPrice(newPrice, errorMessage)) price = newPrice;
else throw std::runtime_error(errorMessage);
}
bool Part::isValidPrice(const double value, std::string &errorMessage)
{
if (value >= 0.01) return true;
errorMessage = "Price must be greater than 0.00";
return false;
}
double Part::getVatRate() const
{
return vatRate;
}
void Part::setVatRate(const double newVatRate)
{
string errorMessage;
if (isValidVatRate(newVatRate, errorMessage)) vatRate = newVatRate;
else throw std::runtime_error(errorMessage);
}
bool Part::isValidVatRate(const double value, std::string &errorMessage)
{
if (value >= 0.0) return true;
errorMessage = "VAT rate must be at least 0.0";
return false;
}
int Part::getQuantity() const
{
return quantity;
}
void Part::setQuantity(const int newQuantity)
{
string errorMessage;
if (isValidQuantity(newQuantity, errorMessage)) quantity = newQuantity;
else throw std::runtime_error(errorMessage);
}
bool Part::isValidQuantity(const int value, std::string &errorMessage)
{
if (value > 0) return true;
errorMessage = "Quantity must be greater than 0";
return false;
}
void Part::validate() const
{
string errorMessage;
if (!isValidJobId(jobId, errorMessage)) throw std::runtime_error(errorMessage);
if (!isValidName(name, errorMessage)) throw std::runtime_error(errorMessage);
if (!isValidNumber(number, errorMessage)) throw std::runtime_error(errorMessage);
if (!isValidPrice(price, errorMessage)) throw std::runtime_error(errorMessage);
if (!isValidVatRate(vatRate, errorMessage)) throw std::runtime_error(errorMessage);
if (!isValidQuantity(quantity, errorMessage)) throw std::runtime_error(errorMessage);
}
double Part::getTotalPrice() const
{
return (getPrice() * getQuantity()) + doubleTo2Dp((getPrice() * getQuantity()) * (getVatRate() / 100.0));
}
ostream & operator <<(ostream &stream, const Part &part)
{
stream << "***************" << endl
<< " ID : " << part.getId() << endl
<< " Job ID : " << part.getJobId() << endl
<< " Name : " << part.getName() << endl
<< " Number : " << part.getNumber() << endl
<< " Price : " << part.getPrice() << endl
<< "VAT Rate : " << part.getVatRate() << endl
<< "Quantity : " << part.getQuantity() << endl
<< "***************" << endl;
return stream;
}