(Stock Market) Write a program to help a local stock trading company automate it
ID: 3593933 • Letter: #
Question
(Stock Market)
Write a program to help a local stock trading company automate its systems. The company invests only in the stock market. At the end of each trading day, the company would like to generate and post the listing of its stocks so that investors can see how their holdings performed that day. We assume that the company invests in, say, 10 different stocks. The desired output is to produce two listings, one sorted by stock symbol and another sorted by percent gain from highest to lowest.
The input data is provided in a file in the following format:
symbol openingPrice closingPrice todayHigh todayLow prevClose volume
For example, the sample data is:
MSMT 112.50 115.75 116.50 111.75 113.50 6723823
CBA 67.50 75.50 78.75 67.50 65.75 378233
.
.
.
.
The first line indicates that the stock symbol is MSMT, today's opening price was 112.50, the closing price was 115.75, today's high price was 116.50, today's low price was 111.75, yesterday's closing price was 113.50, and the number of shares currently being held is 6723823.
The listing sorted by stock symbols must be of the following form:
please refer to the book on page 941! It's a lot to type!
Develop this programming exercise in two steps. In the first step (part a), design and implement a stock object. In the second step (part b), design and implement an object to maintain a list of stocks.
Part a:
(Stock Object) Design and implement the stock object. Call the class that captures the various characteristics of a stock object stockType.
The main components of a stock are the stock symbol, stock price, and number of shares. Moreover, we need to output the opening price, closing price, high price, low price, previous price, and the percent gain/loss for the day. These are also all the characteristics of a stock. Therefore, the stock object should store all this information.
Perform the following operations on each stock object:
1. Set the stock information.
2. Print the stock information.
3. Show the different prices.
4. Calculate and print the percent gain/loss.
5. Show the number of shares.
~the natural ordering of the stock list is by stock symbol. Overload the relational operators to compare two stock objects by their symbols.
~ Overload the insertion operator, <<, for="" easy="" output="" br=""> ~Because the data is stored in a file, overload the stream extraction operator, >>, for easy input.
For example, suppose infile is ifstream object and the input file was opened using the object infile. Further supppose that myStock is a stock object. Then the statement:
infile >>myStock;
read the data from the input file and stores it in the object myStock. (Note that this statement reads and stores the data in the relevant components of myStock.)
Part B:
Now that you have designed and implemented the class stockType to implement a stock object in a program, it is time to create a lit of stock objects.
Let us call the class to implement a list of stock objects stockListType.
The class stockListType must be derived from teh class listType, which you desinged and implemented in the previous exercise. However, the class stockListType is a very specific class, designed to create a list of stock objects. Therefore, the class stockListType is no longer a template.
Add and/or overwrite the operations of the class listType to implement the necessary operations on a stock list.
The following statement derives the class stockListType from the class listType.
class stockListType:public listType
{
member list
};
The member variables to hold the list elements, the length of the list and the max listSize were declared as protected in the class listType. Therefore, these members can be directly accessed in the class stockListType.
Because the company also requres you to produce the lsit ordered by the percent gain/loss, you need to sort the stock list by this compnenet. However, you are not to physically sort the list by the component percent gain/loss. Instead, you will provide a logical ordering with respect to thsi component.
To do so, add a member variable, an array, to hold the indicies of the stock list ordered by the compnent percent gain/loss. Call this array sortIndicesGainLoss. When printing the lis tordered by the component percent gain/loss, use the array sortIndicesGainLoss to print the list. The elements of the array sortIndicesGainLoss will tell whcih component of the stock list to print next.
Part C:
Write a program that uses these two classes to automate the company's analysis of stock data!
This problem is throwing me for a loop and I do not know where to even start now..information overload. Can any one help me out with this using two .h files and two .cpp files. None of the other examples I am finding are helping me out.
Explanation / Answer
Below is your program: -
stockType.h
#ifndef stockType_H
#define stockType_H
#include <string>
using namespace std;
class stockType
{
friend ostream& operator<<(ostream&, const stockType&);
friend istream& operator>>(istream&, stockType&);
private:
string symbol;
double close, previousClose;
double open, high, low;
double gainLoss;
long shares;
void calculateGainLoss();
public:
void setSymbol(string);
void setOpen(double);
void setClose(double);
void setHigh(double);
void setLow(double);
void setPreviousClose(double);
void setShares(long);
string getSymbol();
double getOpen();
double getClose();
double getHigh();
double getLow();
double getPreviousClose();
double getGainLoss();
long getShares();
void print() const;
bool operator==(const stockType&) const;
bool operator!=(const stockType&) const;
bool operator<(const stockType&) const;
bool operator<=(const stockType&) const;
bool operator>(const stockType&) const;
bool operator>=(const stockType&) const;
//The symbol, closing price and previous closing price is the
//minimum amount of information required to properly construct a
//stockType object. calculateGainLoss() should be invoked in the
//constructor.
stockType(string symbol, double close, double previousClose,
double open = 0, double high = 0, double low = 0, long shares = 0);
//Needed for temporary object creation in listType::sort()
stockType();
};
#endif
stockType.cpp
#include "stockType.h"
#include <iostream>
#include <cassert>
#include <iomanip>
stockType::stockType(string symbol, double close, double previousClose,
double open, double high, double low, long shares)
{
this->symbol = symbol;
this->close = close;
this->previousClose = previousClose;
this->open = open;
this->high = high;
this->low = low;
this->shares = shares;
calculateGainLoss();
}
string stockType::getSymbol()
{
return symbol;
}
void stockType::calculateGainLoss()
{
gainLoss = (close - previousClose) / previousClose * 100.0;
}
stockType::stockType()
{}
bool stockType::operator<(const stockType& other) const
{
assert(this->symbol.length() > 0 && other.symbol.length() > 0);
return this->symbol[0] < other.symbol[0];
}
double stockType::getOpen()
{
return open;
}
double stockType::getClose()
{
return close;
}
long stockType::getShares()
{
return shares;
}
void stockType::setShares(long shares)
{
this->shares = shares;
}
double stockType::getGainLoss()
{
return gainLoss;
}
istream& operator>>(istream& in, stockType& stock)
{
in >> stock.symbol >> stock.open >> stock.close
>> stock.high >> stock.low >> stock.previousClose >> stock.shares;
stock.calculateGainLoss();
return in;
}
ostream& operator<<(ostream& out, const stockType& stock)
{
out << fixed << showpoint << setprecision(2)
<< setw(6) << stock.symbol << " " << setw(9) << stock.open << " "
<< setw(7) << stock.close << " " << setw(7) << stock.high << " "
<< setw(7) << stock.low << " " << setw(7) << stock.previousClose << " "
<< setw(8) << stock.gainLoss << "% " << " "
<< setw(12) << stock.shares << endl;
return out;
}
listType.h
listType.cpp
stockListType.h
#ifndef stockListType_H
#define stockListType_H
#include "listType.h"
#include "stockType.h"
#include <vector>
using namespace std;
class stockListType : public listType<stockType>
{
private:
vector<int> sortIndicesByGainLoss;
int numberOfIndices; //keep track of how many elements are actually in
//sortIndicesByGainLoss, this is important
//because vector initializes every int to zero
public:
double totalValue();
void sortByStockSymbol();
void resizeVector(int n);
bool not_previous_index(int n);
void printByGainLoss();
stockListType(int maxSize);
};
#endif
stockListType.cpp
#include "stockListType.h"
#include "stockType.h"
#include "listType.h"
using namespace std;
stockListType::stockListType(int maxSize) : listType<stockType>{ maxSize }
{}
void stockListType::sortByStockSymbol()
{
listType<stockType>::sort();
}
void stockListType::printByGainLoss()
{
sortIndicesByGainLoss.resize(getLength());
int sub = 0;
//sort by GainLoss
numberOfIndices = 0;
double highest = -999;
int indice = 0;
for (int i = 0; i < getLength() + 1; i++)
{
highest = -999;
for (int j = 0; j < getLength(); j++)
{
if ((list[j].getGainLoss() >= highest) && not_previous_index(j))
{
highest = list[j].getGainLoss();
indice = j;
}
}
if (not_previous_index(indice))
{
numberOfIndices++;
sortIndicesByGainLoss[sub] = indice;
sub++;
}
}
//printByGainLoss
for (int i = 0; i < getLength(); i++)
cout << list[sortIndicesByGainLoss[i]];
}
bool stockListType::not_previous_index(int n)
{
for (int i = 0; i < numberOfIndices; i++)
if (n == sortIndicesByGainLoss[i])
return false;
return true;
}
double stockListType::totalValue()
{
double total = 0.0;
for (int i = 0; i < getLength(); ++i)
{
total += list[i].getClose() * list[i].getShares();
}
return total;
}
c)
StockMarket.cpp
#include <iostream>
#include <memory>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include "stockType.h"
#include "stockListType.h"
//#define TEST
using namespace std;
//Seeks the input stream to the beginning of the file.
void rewind(ifstream&);
//Determines the number of rows in the file. Needed
//because of the required use of listType.
int determineRows(ifstream&);
//Inserts the stock listings from the file into the stockListType
//n - the number of rows to read.
void readFileIntoList(ifstream&, stockListType&, int n);
//Prints the Header, called prior to printing the contents
//of the stockListType.
void printHeader();
void printClosingAssets(stockListType&);
void printFooter();
int main()
{
int numberOfRows;
ifstream inFile;
inFile.open("stocks.txt");
if (!inFile)
{
cout << "File Not Found, Exiting." << endl;
return EXIT_FAILURE;
}
numberOfRows = determineRows(inFile);
rewind(inFile);
stockListType stockList{ numberOfRows };
readFileIntoList(inFile, stockList, numberOfRows);
//Output sorted by stock symbol.
printHeader();
stockList.sortByStockSymbol();
stockList.print();
printClosingAssets(stockList);
printFooter();
cout << endl << endl;
//Output sorted by gain/loss.
printHeader();
stockList.printByGainLoss();
printClosingAssets(stockList);
printFooter();
cout << endl;
system("pause");
return EXIT_SUCCESS;
}
void rewind(ifstream& file)
{
file.clear();
file.seekg(0, ios::beg);
}
void printClosingAssets(stockListType& list)
{
cout << "Closing Assets: " << "$" << fixed << setprecision(2) << list.totalValue() << endl;
}
void printFooter()
{
string s = "_*_*_*_*_*_*_*_*";
cout << s << s << s;
}
int determineRows(ifstream& stream)
{
int numberOfRows = 0;
string line;
while (getline(stream, line))
numberOfRows++;
return numberOfRows;
}
void readFileIntoList(ifstream& in, stockListType& list, int rows)
{
for (int i = 0; i < rows; ++i)
{
stockType aStock{};
in >> aStock;
list.insertAt(aStock, i);
}
}
void printHeader()
{
cout << setw(40) << setfill('*') << " First Investor's Heaven " << setw(10) << "" << endl;
cout << setw(40) << setfill('*') << " Financial Report " << setw(10) << "" << endl;
cout << setfill(' ') << "Stock" << setw(20) << "Today" << setw(25) << "Previous" << setw(10) << "Percent" << endl;
cout << "Symbol" << setw(9) << "Open" << setw(8) << "Close" << setw(8) << "High"
<< setw(8) << "Low" << setw(8) << "Close" << setw(10) << "Gain" << setw(15) << "Volume" << endl;
cout << setw(7) << setfill('-') << " " << setw(5) << setfill(' ') << "-" << setw(4) << setfill('-') << " "
<< setw(3) << setfill(' ') << "-" << setw(5) << setfill('-') << " " << setw(4) << setfill(' ') << "-"
<< setw(4) << setfill('-') << " " << setw(5) << setfill(' ') << "-" << setw(3) << setfill('-') << " "
<< setw(3) << setfill(' ') << "-" << setw(6) << setfill('-') << " " << setw(5) << setfill(' ') << "-"
<< setw(7) << setfill('-') << " " << setw(6) << setfill(' ') << "-" << setw(6) << setfill('-') << " ";
cout << setfill(' ') << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.