-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmywork.cpp
More file actions
439 lines (396 loc) · 14.6 KB
/
Copy pathmywork.cpp
File metadata and controls
439 lines (396 loc) · 14.6 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include <cassert>
#include <iomanip>
#include <iostream>
#include "project4.hpp"
#include "History.hpp"
#include "Transaction.hpp"
////////////////////////////////////////////////////////////////////////////////
// Definitions for Transaction class
////////////////////////////////////////////////////////////////////////////////
//
//
// Constructor
// TASK
//
Transaction::Transaction( std::string ticker_symbol, unsigned int day_date,
unsigned int month_date, unsigned int year_date,
bool buy_sell_trans, unsigned int number_shares,
double trans_amount ) {
symbol = ticker_symbol;
day = day_date;
month = month_date;
year = year_date;
if (buy_sell_trans == true) {
trans_type = "Buy";
} else if (buy_sell_trans == false) {
trans_type = "Sell"; //cgl = trans_amount - (number_shares * acb_per_share);
}
shares = number_shares;
amount = trans_amount;
trans_id = assigned_trans_id;
++assigned_trans_id;
acb = 0; //used to be += amount
acb_per_share = 0;
share_balance = 0;
cgl = 0;
p_next = nullptr;
}
// Destructor
// TASK
//
Transaction::~Transaction() {
}
// TASK
// Overloaded < operator.
//
bool Transaction::operator<( Transaction const &other ) {
if (this->get_year() < other.get_year()) {
return true;
} else if (this->get_year() == other.get_year()) {
if (this->get_month() < other.get_month()) {
return true;
} else if (this->get_month() == other.get_month()) {
if (this->get_day() < other.get_day()) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
// GIVEN
// Member functions to get values.
//
std::string Transaction::get_symbol() const { return symbol; }
unsigned int Transaction::get_day() const { return day; }
unsigned int Transaction::get_month() const { return month; }
unsigned int Transaction::get_year() const { return year; }
unsigned int Transaction::get_shares() const { return shares; }
double Transaction::get_amount() const { return amount; }
double Transaction::get_acb() const { return acb; }
double Transaction::get_acb_per_share() const { return acb_per_share; }
unsigned int Transaction::get_share_balance() const { return share_balance; }
double Transaction::get_cgl() const { return cgl; }
bool Transaction::get_trans_type() const { return (trans_type == "Buy") ? true: false ; }
unsigned int Transaction::get_trans_id() const { return trans_id; }
Transaction *Transaction::get_next() { return p_next; }
// GIVEN
// Member functions to set values.
//
void Transaction::set_acb( double acb_value ) { acb = acb_value; }
void Transaction::set_acb_per_share( double acb_share_value ) { acb_per_share = acb_share_value; }
void Transaction::set_share_balance( unsigned int bal ) { share_balance = bal ; }
void Transaction::set_cgl( double value ) { cgl = value; }
void Transaction::set_next( Transaction *p_new_next ) { p_next = p_new_next; }
// GIVEN
// Print the transaction.
//
void Transaction::print() {
std::cout << std::fixed << std::setprecision(2);
std::cout << std::setw(4) << get_trans_id() << " "
<< std::setw(4) << get_symbol() << " "
<< std::setw(4) << get_day() << " "
<< std::setw(4) << get_month() << " "
<< std::setw(4) << get_year() << " ";
if ( get_trans_type() ) {
std::cout << " Buy ";
} else { std::cout << " Sell "; }
std::cout << std::setw(4) << get_shares() << " "
<< std::setw(10) << get_amount() << " "
<< std::setw(10) << get_acb() << " " << std::setw(4) << get_share_balance() << " "
<< std::setw(10) << std::setprecision(3) << get_acb_per_share() << " "
<< std::setw(10) << std::setprecision(3) << get_cgl()
<< std::endl;
}
////////////////////////////////////////////////////////////////////////////////
// Definitions for the History class
////////////////////////////////////////////////////////////////////////////////
//
//
// Constructor
// TASK
//
History::History() {
p_head = nullptr;
}
// Destructor
// TASK
//
History::~History() {
Transaction *p_old_head{nullptr};
while (get_p_head() != nullptr) {
p_old_head = get_p_head();
p_head = p_head->get_next();
delete p_old_head;
p_old_head = nullptr;
}
p_old_head = nullptr;
}
// TASK
// read_history(...): Read the transaction history from file.
//
void History::read_history() {
ece150::open_file();
History file{};
if (ece150::next_trans_entry()) {
Transaction *entry{new Transaction{ece150::get_trans_symbol(),
ece150::get_trans_day(), ece150::get_trans_month(),
ece150::get_trans_year(), ece150::get_trans_type(),
ece150::get_trans_shares(), ece150::get_trans_amount()}};
this->insert(entry);
// entry->print();
}
while (ece150::next_trans_entry()) {
Transaction *entry{new Transaction{ece150::get_trans_symbol(),
ece150::get_trans_day(), ece150::get_trans_month(),
ece150::get_trans_year(), ece150::get_trans_type(),
ece150::get_trans_shares(), ece150::get_trans_amount()}};
this->insert(entry);
// entry->print();
}
ece150::close_file();
}
// insert(...): Insert transaction into linked list.
//
void History::insert(Transaction *p_new_trans) {
Transaction *p_tmp{get_p_head()};
if (get_p_head() == nullptr) {
p_head = p_new_trans;
} else {
while (p_tmp->get_next() != nullptr) {
p_tmp = p_tmp->get_next();
}
p_tmp->set_next(p_new_trans);
}
p_tmp = nullptr;
}
// TASK
// sort_by_date(): Sort the linked list by trade date.
//
void History::sort_by_date() {
if (this->get_p_head() == nullptr || this->get_p_head()->get_next() == nullptr) { //no need to sort
return;
} else { //there are at least two nodes
Transaction *p_tmp_1{this->get_p_head()}; //pointer to walk through the initial list
Transaction *p_tmp_sorted{nullptr}; //pointer to walk through the sorted list
Transaction *p_head_sorted{nullptr}; //head pointer for the sorted list
//break off the first node
this->p_head = this->get_p_head()->get_next();
p_tmp_1->set_next(nullptr);
//put the first node into the sorted list
p_head_sorted = p_tmp_1;
//now the sorted list has exactly one node
while (this->get_p_head() != nullptr) {
//detaching the node
p_tmp_1 = this->get_p_head();
this->p_head = this->get_p_head()->get_next();
p_tmp_1->set_next(nullptr);
if ( ( *p_tmp_1 < *p_head_sorted ) ||
( (p_tmp_1->get_year() == p_head_sorted->get_year()) &&
(p_tmp_1->get_month() == p_head_sorted->get_month()) &&
(p_tmp_1->get_day() == p_head_sorted->get_day()) &&
(p_tmp_1->get_trans_id() < p_head_sorted->get_trans_id()) )) { //node goes front
p_tmp_1->set_next(p_head_sorted);
p_head_sorted = p_tmp_1;
} else { //the node goes elsewhere in the list
p_tmp_sorted = p_head_sorted;
while ( (p_tmp_sorted->get_next() != nullptr) &&
!( (*p_tmp_1 < *(p_tmp_sorted->get_next())) ||
((p_tmp_1->get_year() == (p_tmp_sorted->get_next())->get_year()) &&
(p_tmp_1->get_month() == (p_tmp_sorted->get_next())->get_month()) &&
(p_tmp_1->get_day() == (p_tmp_sorted->get_next())->get_day()) &&
(p_tmp_1->get_trans_id() < (p_tmp_sorted->get_next())->get_trans_id()) ) )) {
p_tmp_sorted = p_tmp_sorted->get_next();
}
p_tmp_1->set_next(p_tmp_sorted->get_next());
p_tmp_sorted->set_next(p_tmp_1);
}
}
this->p_head = p_head_sorted;
p_tmp_1 = nullptr;
p_tmp_sorted = nullptr;
p_head_sorted = nullptr;
}
}
// TASK
// update_acb_cgl(): Updates the ACB and CGL values.
//
void History::update_acb_cgl(){ //works fine?!
unsigned int shares{0};
double acb_per_share{0};
double acb{0};
double cgl{0};
Transaction *p_tmp{this->get_p_head()};
while (p_tmp != nullptr) { //might need to change the condition????
if (p_tmp->get_trans_type()) {
acb += p_tmp->get_amount();
shares += p_tmp->get_shares();
acb_per_share = acb/shares;
cgl = 0;
} else {
shares -= p_tmp->get_shares();
acb = acb - (p_tmp->get_shares()*acb_per_share);
cgl = p_tmp->get_amount() - (p_tmp->get_shares()*acb_per_share);
}
p_tmp->set_acb(acb);
p_tmp->set_acb_per_share(acb_per_share);
p_tmp->set_share_balance(shares);
p_tmp->set_cgl(cgl);
p_tmp = p_tmp->get_next();
}
p_tmp = nullptr;
}
// TASK
// compute_cgl(): )Compute the ACB, and CGL.
//
double History::compute_cgl(unsigned int year) {
History::update_acb_cgl();
Transaction *p_tmp{get_p_head()};
double cgl{0};
this->sort_by_date();
while (p_tmp != nullptr) {
if (p_tmp->get_year() == year) {
cgl += p_tmp->get_cgl();
}
p_tmp = p_tmp->get_next();
}
p_tmp = nullptr;
return cgl;
}
// TASK
// print() Print the transaction history.
//
void History::print() {
std::cout << "========== BEGIN TRANSACTION HISTORY ============" << std::endl;
Transaction *p_tmp{get_p_head()};
while (p_tmp != nullptr) {
p_tmp->print();
p_tmp = p_tmp->get_next();
}
std::cout << "========== END TRANSACTION HISTORY ============" << std::endl;
p_tmp = nullptr;
}
// GIVEN
// get_p_head(): Full access to the linked list.
//
Transaction *History::get_p_head() { return p_head; }
//WORKING SORTING ALGORITHM???!!!
/*
void History::sort_by_date() {
if (this->get_p_head() == nullptr || this->get_p_head()->get_next() == nullptr) { //no need to sort
return;
} else { //there are at least two nodes
Transaction *p_tmp_1{this->get_p_head()}; //pointer to walk through the initial list
Transaction *p_tmp_sorted{nullptr}; //pointer to walk through the sorted list
Transaction *p_head_sorted{nullptr}; //head pointer for the sorted list
//break off the first node
this->p_head = this->get_p_head()->get_next();
p_tmp_1->set_next(nullptr);
//put the first node into the sorted list
p_head_sorted = p_tmp_1;
//now the sorted list has exactly one node
while (this->get_p_head() != nullptr) {
//detaching the node
p_tmp_1 = this->get_p_head();
this->p_head = this->get_p_head()->get_next();
p_tmp_1->set_next(nullptr);
if ( ( *p_tmp_1 < *p_head_sorted ) ||
( (p_tmp_1->get_year() == p_head_sorted->get_year()) &&
(p_tmp_1->get_month() == p_head_sorted->get_month()) &&
(p_tmp_1->get_day() == p_head_sorted->get_day()) &&
(p_tmp_1->get_trans_id() < p_head_sorted->get_trans_id()) )) { //node goes front
p_tmp_1->set_next(p_head_sorted);
p_head_sorted = p_tmp_1;
} else { //the node goes elsewhere in the list
p_tmp_sorted = p_head_sorted;
while ( (p_tmp_sorted->get_next() != nullptr) &&
!( (*p_tmp_1 < *(p_tmp_sorted->get_next())) ||
((p_tmp_1->get_year() == (p_tmp_sorted->get_next())->get_year()) &&
(p_tmp_1->get_month() == (p_tmp_sorted->get_next())->get_month()) &&
(p_tmp_1->get_day() == (p_tmp_sorted->get_next())->get_day()) &&
(p_tmp_1->get_trans_id() < (p_tmp_sorted->get_next())->get_trans_id()) ) )) {
p_tmp_sorted = p_tmp_sorted->get_next();
}
p_tmp_1->set_next(p_tmp_sorted->get_next());
p_tmp_sorted->set_next(p_tmp_1);
}
}
this->p_head = p_head_sorted;
}
}
*/
//OLD SORTING ALGORITHM:
/*
void History::sort_by_date() {
if (this->get_p_head() == nullptr || this->get_p_head()->get_next() == nullptr) { //the list has at least 2 nodes
return;
} else {
Transaction *p_tmp_initial{get_p_head()}; //temporary pointer for the initial linked list
Transaction *p_head_sorted{nullptr};//head pointer for the sorted linked list
Transaction *p_tmp_sorted{nullptr}; //temporary pointer for the sorted linked list
this->p_head = this->p_head->get_next(); //p_head now points onto second node
p_tmp_initial->set_next(nullptr); //the first node's p_next is now nullptr (it's cut off) --> the first node has now been cut off from the original linked list
p_head_sorted = p_tmp_initial; //p_head_sorted now points onto the first node of the original linked list
while (this->get_p_head() != nullptr) {
p_tmp_initial = this->get_p_head(); //p_tmp_initial now points onto the new first (old second) node of the original linked list
this->p_head = this->get_p_head()->get_next(); //p_head now points onto the third node of the original linked list
p_tmp_initial->set_next(nullptr); //the new first (old second) node is now cut off from the original linked list
//at this point:
//p_head points onto the third node of the original linked list
//p_tmp_initial points onto the second node of the initial linked list
//p_head_sorted points onto the first node of the initial linked list
//p_tmp_sorted is still a nullptr
//We now consider the following two cases:
//case 1: transferred node (stored at p_tmp_initial) goes to the front of the list
//case 2: transferred node (stored at p_tmp_initial) goes elsewhere in the list
if ( (*p_tmp_initial < *p_head_sorted) ||
(( (p_tmp_initial->get_year() == p_head_sorted->get_year() &&
p_tmp_initial->get_month() == p_head_sorted->get_month()) &&
p_tmp_initial->get_day() == p_head_sorted->get_day()) &&
(p_tmp_initial->get_trans_id() < p_head_sorted->get_trans_id()) )) { //case 1
p_tmp_initial->set_next(p_head_sorted);
p_head_sorted = p_tmp_initial;
} else if ( ((p_tmp_initial->get_year() == p_head_sorted->get_year()) && //checking for
(p_tmp_initial->get_month() == p_head_sorted->get_month())) && //equal dates
(p_tmp_initial->get_day() == p_head_sorted->get_day())) {
if (p_tmp_initial->get_trans_id() < p_head_sorted->get_trans_id()) { //case 1
p_tmp_initial->set_next(p_head_sorted);
p_head_sorted = p_tmp_initial;
} else { //case 2
p_tmp_sorted = p_head_sorted;
while ( ( !(*p_tmp_initial < *(p_tmp_sorted->get_next())) ||
!( (p_tmp_initial->get_year() == p_tmp_sorted->get_year() ) &&
(p_tmp_initial->get_month() == p_tmp_sorted->get_month()) &&
(p_tmp_initial->get_day() == p_tmp_sorted->get_day()) &&
(p_tmp_initial->get_trans_id() < p_tmp_sorted->get_trans_id())) ) &&
(p_tmp_sorted->get_next() != nullptr) ){
p_tmp_sorted = p_tmp_sorted->get_next();
}
p_tmp_initial->set_next(p_tmp_sorted->get_next());
p_tmp_sorted->set_next(p_tmp_initial);
}
} else { //case 2
p_tmp_sorted = p_head_sorted;
while ( !( (*p_tmp_initial < *(p_tmp_sorted->get_next())) ||
(( (p_tmp_initial->get_year() == p_tmp_sorted->get_next()->get_year() &&
p_tmp_initial->get_month() == p_tmp_sorted->get_next()->get_month()) &&
p_tmp_initial->get_day() == p_tmp_sorted->get_next()->get_day()) &&
(p_tmp_initial->get_trans_id() < p_tmp_sorted->get_next()->get_trans_id()) ) ) &&
(p_tmp_sorted->get_next() != nullptr) ) {
p_tmp_sorted = p_tmp_sorted->get_next();
}
p_tmp_initial->set_next(p_tmp_sorted->get_next());
p_tmp_sorted->set_next(p_tmp_initial);
}
}
this->p_head = p_head_sorted;
p_tmp_initial = nullptr;
p_tmp_sorted = nullptr;
p_head_sorted = nullptr;
}
}
*/