-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVatRate.cpp
More file actions
137 lines (112 loc) · 3.33 KB
/
VatRate.cpp
File metadata and controls
137 lines (112 loc) · 3.33 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
/*
* VatRate.cpp
*
* Created on: 25 March 2012
* Author: Max Foster
*/
#include <stdexcept>
#include <ctime>
using namespace std;
#include "VatRate.h"
#include "Globals.h"
#include "Utils.h"
const string VatRate::databaseFilename = "vatrates.dat";
int VatRate::size()
{
return Record::size() + sizeof(double) + sizeof(time_t);
}
VatRate::VatRate(double value, time_t startDate)
: value(value < 0.0 ? Globals::defaultVatRate : value), startDate(startDate == 0 ? time(NULL) : startDate) {}
VatRate::VatRate(const VatRate &vatRate)
{
*this = vatRate;
}
VatRate::~VatRate() {}
void VatRate::operator =(const VatRate &vatRate)
{
*((Record*)this) = (Record)vatRate;
value = vatRate.value;
startDate = vatRate.startDate;
}
void VatRate::writeToFile(std::fstream &file) const
{
Record::writeToFile(file);
file.write(reinterpret_cast<const char *>(&value), sizeof(value));
file.write(reinterpret_cast<const char *>(&startDate), sizeof(startDate));
}
void VatRate::readFromFile(std::fstream &file)
{
Record::readFromFile(file);
file.read(reinterpret_cast<char *>(&value), sizeof(value));
file.read(reinterpret_cast<char *>(&startDate), sizeof(startDate));
}
bool VatRate::hasMatchingField(const std::string &fieldName, const int searchTerm) const
{
return Record::hasMatchingField(fieldName, searchTerm);
}
bool VatRate::hasMatchingField(const std::string &fieldName, const double searchTerm) const
{
if (fieldName == "value") return (value == searchTerm);
return false;
}
bool VatRate::hasMatchingField(const std::string &fieldName, const time_t searchTerm) const
{
if (fieldName == "startdate") return (startDate == searchTerm);
return false;
}
bool VatRate::fieldCompare(const VatRate &rhs) const
{
if (value != rhs.value) return false;
if (startDate != rhs.startDate) return false;
return true;
}
bool VatRate::completeCompare(const VatRate &rhs) const
{
if (getId() != rhs.getId()) return false;
return fieldCompare(rhs);
}
double VatRate::getValue() const
{
return value;
}
void VatRate::setValue(const double newValue)
{
string errorMessage;
if (isValidValue(newValue, errorMessage)) value = newValue;
else throw std::runtime_error(errorMessage);
}
bool VatRate::isValidValue(const double value, std::string &errorMessage)
{
if (value >= 0.0) return true;
errorMessage = "Value must be at least 0.00";
return false;
}
time_t VatRate::getStartDate() const
{
return startDate;
}
void VatRate::setStartDate(const time_t newStartDate)
{
string errorMessage;
if (isValidStartDate(newStartDate, errorMessage)) startDate = newStartDate;
else throw std::runtime_error(errorMessage);
}
bool VatRate::isValidStartDate(const time_t, std::string &)
{
return true;
}
void VatRate::validate() const
{
string errorMessage;
if (!isValidValue(value, errorMessage)) throw std::runtime_error(errorMessage);
if (!isValidStartDate(startDate, errorMessage)) throw std::runtime_error(errorMessage);
}
ostream & operator <<(ostream &stream, const VatRate &vatRate)
{
stream << "***************" << endl
<< " ID : " << vatRate.getId() << endl
<< " Value : " << vatRate.getValue() << endl
<< "Start Date : " << Date(vatRate.getStartDate()) << endl
<< "***************" << endl;
return stream;
}