Stock Market Simulator main e8c3612
A game that provides a realistic stock buying experience with unpredictable trends to test investment strategies.
Loading...
Searching...
No Matches
stock.cpp File Reference

Implementation of the Stock class. More...

#include "stock.h"
#include "file_io.h"
#include "format.h"
#include "names.h"
#include "random_price.h"
#include <algorithm>
#include <cassert>
#include <fstream>
#include <iostream>
Include dependency graph for stock.cpp:

Go to the source code of this file.

Functions

std::ostream & operator<< (std::ostream &fout, const Stock &stock)
 
std::istream & operator>> (std::istream &fin, Stock &stock)
 
void sortStocksList (std::vector< Stock > &stocks_list, SortingMethods sortMethod, SortingDirections sortDirection)
 Sorts the stocks.
 

Variables

const int INVALID_OPERATION = -1
 

Detailed Description

Implementation of the Stock class.

Definition in file stock.cpp.

Function Documentation

◆ operator<<()

std::ostream & operator<< ( std::ostream & fout,
const Stock & stock )

Definition at line 71 of file stock.cpp.

71 {
72 fout << stock.category
73 << std::endl; // literally load everything into class into file
74 fout << stock.name << std::endl;
75 for (unsigned int index = 0; index < stock.history.size(); index++) {
76 fout << stock.history[index] << " ";
77 }
78 fout << -1 << std::endl; // -1 is the stop code for vector<float> history in
79 // filesave
80 fout << stock.quantity << std::endl;
81 fout << stock.attributes.at(standard_deviation) << " ";
82 fout << stock.attributes.at(mean) << " ";
83 fout << stock.attributes.at(lower_limit) << " ";
84 fout << stock.attributes.at(upper_limit) << std::endl;
85 fout << stock.split_count << std::endl << std::endl;
86
87 // Save the ongoing events, separated by std::endl
88 for (Stock_event event : stock.events) {
89 fout << event << std::endl;
90 }
91 return fout;
92}
unsigned int split_count
Contains the spliting count of a stock.
Definition stock.h:296
std::vector< float > history
Contains the stock price history.
Definition stock.h:293
std::map< stock_modifiers, float > attributes
Stores the initial value of the stock_modifiers (e.g.
Definition stock.h:289
unsigned int category
Use numbers to represent the category of the stock.
Definition stock.h:282
std::string name
Name of the stock that we assigned to it.
Definition stock.h:270
std::list< Stock_event > events
Stores all the events that will apply to this stock specifically.
Definition stock.h:285
unsigned int quantity
Number of stocks the player has purchased.
Definition stock.h:276
@ upper_limit
The upper limit of the stock price percentage change.
Definition events.h:48
@ standard_deviation
Amount of variation of the stock price percentage change.
Definition events.h:32
@ mean
The expectation of the stock price percentage change.
Definition events.h:36
@ lower_limit
The lower limit of the stock price percentage change.
Definition events.h:42
The data structure of an event that will be applied to the stocks.
Definition events.h:104

◆ operator>>()

std::istream & operator>> ( std::istream & fin,
Stock & stock )

Definition at line 94 of file stock.cpp.

94 {
95 fin >> stock.category; // line 1
96 assert(stock.category < category_list_size && "Invalid category");
97 // line 2 is entirely the stock name
98 std::getline(fin >> std::ws, stock.name);
99 float loadedPrice;
100 fin >> loadedPrice;
101 // Erase the history vector and load the new history
102 stock.history.clear();
103 while (loadedPrice != -1) {
104 stock.history.emplace_back(loadedPrice);
105 fin >> loadedPrice; // line 3
106 }
107 // Set the price
108 stock.price = stock.history.back();
109 fin >> stock.quantity; // line 4
110 fin >> stock.attributes[standard_deviation]; // line 5
111 fin >> stock.attributes[mean];
112 fin >> stock.attributes[lower_limit];
113 fin >> stock.attributes[upper_limit];
114 fin >> stock.split_count; // line 6
115 // Clear the events list
116 stock.events.clear();
117 // Skip 2 empty lines
118 std::string emptyLine;
119 std::getline(fin >> std::ws, emptyLine);
120 std::getline(fin >> std::ws, emptyLine);
121 std::string loadedEventString;
122 while (std::getline(fin, loadedEventString)) {
123 Stock_event loadedEvent;
124 std::istringstream(loadedEventString) >> loadedEvent;
125 // Check the loaded event is valid
126 if (loadedEvent.event_id == getStockSplitEvent().event_id) {
127 stock.add_event(loadedEvent);
128 continue;
129 }
130 assert(
131 loadedEvent.event_id < all_stock_events.size() && "Invalid event loaded");
132 Stock_event comparedEvent = all_stock_events[loadedEvent.event_id];
133 assert(loadedEvent == comparedEvent && "Invalid event loaded");
134 stock.add_event(loadedEvent);
135 }
136 return fin;
137}
void add_event(const Stock_event &event)
Add an event to the stock.
Definition stock.cpp:210
float price
Current price of the stock.
Definition stock.h:273
Stock_event getStockSplitEvent(void)
Definition events.cpp:1453
const std::vector< Stock_event > all_stock_events
The list of all events that can be applied to the stocks.
Definition events.cpp:49
const int category_list_size
The size of the category list.
Definition names.h:27
unsigned int event_id
The id of the event.
Definition events.h:110

◆ sortStocksList()

void sortStocksList ( std::vector< Stock > & stocks_list,
SortingMethods sortMethod = by_category,
SortingDirections sortDirection = ascending )

Sorts the stocks.

Parameters
stocks_listA vector of stocks. Pass by reference to modify the stocks.
sortMethodSorting method. Default is by_category.
sortDirectionSorting direction. True for ascending, false for descending. Default is ascending.

Definition at line 321 of file stock.cpp.

322 {
323 switch (sortMethod) {
324 case by_name:
325 std::sort(stocks_list.begin(), stocks_list.end(),
326 [](Stock a, Stock b) { return a.get_name() < b.get_name(); });
327 break;
328 case by_category:
329 std::sort(stocks_list.begin(), stocks_list.end(),
330 [](Stock a, Stock b) { return a.get_category() < b.get_category(); });
331 break;
332 case by_price:
333 std::sort(stocks_list.begin(), stocks_list.end(),
334 [](Stock a, Stock b) { return a.get_price() < b.get_price(); });
335 break;
336 case by_quantity:
337 std::sort(stocks_list.begin(), stocks_list.end(),
338 [](Stock a, Stock b) { return a.get_quantity() < b.get_quantity(); });
339 break;
340 case by_sd:
341 std::sort(stocks_list.begin(), stocks_list.end(), [](Stock a, Stock b) {
342 return a.get_total_attribute(standard_deviation) <
343 b.get_total_attribute(standard_deviation);
344 });
345 break;
346 case by_mean:
347 std::sort(stocks_list.begin(), stocks_list.end(), [](Stock a, Stock b) {
348 return a.get_total_attribute(mean) < b.get_total_attribute(mean);
349 });
350 break;
351 case by_lower_limit:
352 std::sort(stocks_list.begin(), stocks_list.end(), [](Stock a, Stock b) {
353 return a.get_total_attribute(lower_limit) <
354 b.get_total_attribute(lower_limit);
355 });
356 break;
357 case by_upper_limit:
358 std::sort(stocks_list.begin(), stocks_list.end(), [](Stock a, Stock b) {
359 return a.get_total_attribute(upper_limit) <
360 b.get_total_attribute(upper_limit);
361 });
362 break;
363 default:
364 break;
365 }
366 if (sortDirection == descending) {
367 std::reverse(stocks_list.begin(), stocks_list.end());
368 }
369}
A class that represents a stock object in the game.
Definition stock.h:55
@ by_upper_limit
Definition stock.h:313
@ by_mean
Definition stock.h:315
@ by_price
Definition stock.h:310
@ by_quantity
Definition stock.h:312
@ by_category
Definition stock.h:311
@ by_lower_limit
Definition stock.h:314
@ by_sd
Definition stock.h:316
@ by_name
Definition stock.h:309
@ descending
Definition stock.h:319

References by_category, by_lower_limit, by_mean, by_name, by_price, by_quantity, by_sd, by_upper_limit, and descending.

Referenced by main().

Here is the caller graph for this function:

Variable Documentation

◆ INVALID_OPERATION

const int INVALID_OPERATION = -1

Definition at line 27 of file stock.cpp.

Referenced by Stock::purchase(), and Stock::sell().