-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarket.cpp
206 lines (160 loc) · 4.76 KB
/
Market.cpp
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
#include "Market.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
Market::Market():name("MARKET"),currentDay(0),
stockMap(new HashMap(HASHMAP_SIZE)), keyList(new StringList()) { }
Market::Market(string mname):name(mname), currentDay(0),
stockMap(new HashMap(HASHMAP_SIZE)), keyList(new StringList()) { }
Market::Market(string mname, int dayNum):name(mname), currentDay(dayNum),
stockMap(new HashMap(HASHMAP_SIZE)), keyList(new StringList()) { }
void Market::readStockFile(){
this->readStockFile(currentDay);
}
void Market::writeStockFile(){
this->writeStockFile(currentDay);
}
void Market::writeStockFile(int dayNum){
// build the file name
string fname = this->name;
fname.append(".");
string dayWord = to_string(dayNum);
fname.append(dayWord);
fname.append(".txt");
// create an input file stream
ofstream outfile(fname, ios::out);
// check that we can open it
if (!outfile){
cerr << "Could not open file "<< fname << endl;
exit(EXIT_FAILURE);
}
// get a pointer to the start of the list of keys
Node<string> * cursor = keyList->getHead();
Stock * found;
string key;
while (cursor != nullptr){
// get the key from the list of keys
key = cursor->getValue();
// get the stock associated with that key
found = stockMap->find(key);
outfile << found->getTicker() << ",";
outfile << found->getName() << ",";
outfile << found->getPrice() << ",";
outfile << found->getQuantity() << endl;
// go on to the next node in the list of keys
cursor = cursor->getNext();
}
outfile.close();
}
void Market::readStockFile(int dayNum){
// build the file name
string fname = this->name;
fname.append(".");
string dayWord = to_string(dayNum);
fname.append(dayWord);
fname.append(".txt");
// create an input file stream
ifstream infile(fname, ios::in);
// check that we can open it
if (!infile){
cerr << "Could not open file "<< fname << endl;
exit(EXIT_FAILURE);
}
string ticker;
string name;
double price;
int quantity;
// temp variable to hold one line of text from the file
string line;
// temp variable to hold one character version of price & quantity
string temp_word;
// read one line at time from the file of stock info
while (getline(infile, line)){
if (line.size() > 1){
//treat the string containing line as a stream named words
istringstream words(line);
//read from words into ticker until we see a comma
getline(words, ticker, ',');
//continuing reading, now into name, until we see a comma
getline(words, name, ',');
//continuing reading, now into temp_word, until we see a comma
// and assign to price
getline(words, temp_word, ',');
price = stod(temp_word);
//continuing reading, now into temp_word, until we see a comma
// and assign to quantity
getline(words, temp_word, '\n');
quantity = stoi(temp_word);
Stock *s = new Stock(name, ticker, quantity, price);
// ... add the stock the Market object's HashMap of stocks
stockMap->insert(s);
// ... add the key to the object's list of keys
keyList->insert(ticker);
}
}
infile.close();
return;
}
void Market::addStock(Stock *s){
// add the stock to the stockMap
stockMap->insert(s);
// add the eky to the list of keys
keyList->insert(s->getTicker());
return;
};
void Market::removeStock(string sym){
// remove the stock from the stockMap
stockMap->remove(sym);
// remove the key from the list of keys
keyList->remove(sym);
return;
};
Stock * Market::getStockBySymbol(string sym){
return stockMap->find(sym);
};
void Market::showStocks() const {
cout << "Stocks for Market: " << name << endl;
Node<string> * cursor = keyList->getHead();
Stock *found;
string key;
while (cursor != nullptr){
key = cursor->getValue();
found = stockMap->find(key);
cout << found << endl;
cursor = cursor->getNext();
}
return;
};
int Market::getDay() const{
return currentDay;
}
int Market::nextDay() {
return ++currentDay;
}
void Market::step() {
random_device rd;
default_random_engine eng(rd());
uniform_real_distribution<float> distr(0,1);
float max_range = 0.10;
float shift = 0.05;
float factor;
Node<string> * cursor = keyList->getHead();
Stock *found;
string key;
while (cursor != nullptr){
// get the ticker symbol
key = cursor->getValue();
// get the stock associated with the ticker symbol
found = stockMap->find(key);
//produce a factor
factor = (distr(eng) * max_range) - shift;
// get the stock's price
double price = found->getPrice();
// update the stock's price
price = price * (1 + factor);
found->setPrice(price);;
// move on to the next ticker symbol in the list
cursor = cursor->getNext();
}
};