-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWallet.cpp
More file actions
305 lines (241 loc) · 6.53 KB
/
Wallet.cpp
File metadata and controls
305 lines (241 loc) · 6.53 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// ===========================================================================
// Wallet.cpp
// ===========================================================================
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <stdexcept>
#include "Wallet.h"
// c'tors
Wallet::Wallet() : m_euros(0), m_cents(0) {}
Wallet::Wallet(unsigned long long euros, unsigned int cents)
{
if (cents >= 100) {
throw std::out_of_range("cent larger than 100 are not allowed!");
}
m_euros = euros;
m_cents = cents;
}
Wallet::Wallet(unsigned long long euros)
: m_euros(euros), m_cents(0)
{}
Wallet::Wallet(std::string euros, std::string cents)
: m_euros(0), m_cents(0)
{
std::size_t pos = 0;
long long result = 0;
std::size_t length = 0;
// verifying euros
try
{
length = euros.length();
m_euros = std::stoll(euros, &pos);
}
catch (const std::invalid_argument& ex)
{
std::cout << "Wrong format for euros values expected: " << euros << " - " << ex.what() << std::endl;
throw std::invalid_argument("Wrong format for euro value");
}
if (m_euros < 0) {
throw std::invalid_argument("Only positive value for euro valid!");
}
if (length != pos) {
throw std::invalid_argument("Wrong format for euro value!");
}
if (length == 0) {
throw std::invalid_argument("Wrong format for euro value!");
}
if (euros[0] == '-') {
throw std::invalid_argument("Negative value for euro not allowed!");
}
// verifying cents
try
{
length = cents.length();
m_cents = std::stoi(cents, &pos);
}
catch (const std::invalid_argument& ex)
{
std::cout << "Wrong format for cents values expected: " << cents << " - " << ex.what() << std::endl;
throw std::invalid_argument("Wrong format for cent values");
}
if (m_cents < 0) {
throw std::invalid_argument("Only positive value for cent valid!");
}
if (m_cents >= 100) {
throw std::out_of_range("Cents larger than 100 are not allowed!");
}
if (length != pos) {
throw std::invalid_argument("Wrong format for cent value!");
}
if (length == 0) {
throw std::invalid_argument("Wrong format for cent value!");
}
if (cents[0] == '-') {
throw std::invalid_argument("Negative value for cent not allowed!");
}
}
// getter
unsigned long long Wallet::getEuros() const { return m_euros; }
unsigned int Wallet::getCent() const { return m_cents; }
// public interface
void Wallet::add(unsigned long long euros, unsigned int cents) {
if (cents >= 100) {
throw std::invalid_argument("Cent amount between 0 and 99 expected!");
}
m_cents = m_cents + cents;
unsigned int carry = m_cents / 100;
m_cents = m_cents % 100;
m_euros = m_euros + euros + carry;
}
void Wallet::add(unsigned long long euros) {
m_euros += euros;
}
void Wallet::add(const Wallet& other) {
add(other.m_euros, other.m_cents);
}
void Wallet::sub(unsigned long long euros, unsigned int cents) {
if (cents >= 100) {
throw std::invalid_argument("Cent amount between 0 and 99 expected!");
}
Wallet tmp(euros, cents);
if (*this < tmp) {
throw std::invalid_argument("Not enough money available in wallet!");
}
if (m_cents < cents)
{
m_euros = m_euros - 1;
m_cents = m_cents + 100;
}
m_euros = m_euros - euros;
m_cents = m_cents - cents;
}
void Wallet::sub(unsigned long long euros)
{
if (m_euros < euros) {
throw std::invalid_argument("Not enough money available in wallet!");
}
m_euros = m_euros - euros;
}
void Wallet::sub(const Wallet& other) {
sub(other.m_euros, other.m_cents);
}
void Wallet::print() const
{
std::cout << m_euros << "." << std::setw(2) << std::setfill('0') << m_cents << std::endl;
}
int Wallet::compareTo(const Wallet& other) const {
int value = 0;
if (m_euros < other.m_euros)
{
value = -1;
}
else if (m_euros > other.m_euros)
{
value = 1;
}
else if (m_cents < other.m_cents)
{
value = -1;
}
else if (m_cents > other.m_cents)
{
value = 1;
}
else
{
value = 0;
}
return value;
}
unsigned long long Wallet::to_cents() const {
return 100 * m_euros + m_cents;
}
std::string Wallet::to_string() const {
std::ostringstream oss;
oss << m_euros << "." << std::setw(2) << std::setfill('0') << m_cents;
return oss.str();
}
// operators
void Wallet::operator += (const Wallet& other) {
add(other);
}
void Wallet::operator += (unsigned long long euros) {
add(euros);
}
void Wallet::operator -= (const Wallet& other) {
sub(other);
}
void Wallet::operator -= (unsigned long long euros) {
sub(euros);
}
// increment operator: prefix version
Wallet& Wallet::operator++()
{
add(1);
return *this;
}
// decrement operator: prefix version
Wallet& Wallet::operator--()
{
sub(1);
return *this;
}
// increment operator: postfix version
Wallet Wallet::operator++(int)
{
Wallet tmp(*this); // construct a copy
add(1); // increment wallet
return tmp; // return the copy
}
// decrement operator: postfix version
Wallet Wallet::operator--(int)
{
Wallet tmp(*this); // construct a copy
sub(1); // decrement wallet
return tmp; // return the copy
}
// comparison operators
bool operator== (const Wallet& lhs, const Wallet& rhs) {
if (lhs.m_euros == rhs.m_euros && lhs.m_cents == rhs.m_cents) {
return true;
}
else {
return false;
}
}
bool operator!= (const Wallet& lhs, const Wallet& rhs) {
return !(lhs == rhs);
}
bool operator<(const Wallet& lhs, const Wallet& rhs)
{
if (lhs.m_euros < rhs.m_euros) {
return true;
}
else if (lhs.m_euros == rhs.m_euros && lhs.m_cents < rhs.m_cents) {
return true;
}
else {
return false;
}
}
bool operator<= (const Wallet& lhs, const Wallet& rhs)
{
return lhs < rhs || lhs == rhs;
}
bool operator> (const Wallet& lhs, const Wallet& rhs)
{
return !(lhs <= rhs);
}
bool operator>= (const Wallet& lhs, const Wallet& rhs)
{
return !(lhs < rhs);
}
// output
std::ostream& operator<<(std::ostream& os, const Wallet& wallet) {
return os << wallet.to_string();
}
// ===========================================================================
// End-of-File
// ===========================================================================