Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 listings 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 MSMT 112.50 115.75 116.50 111.75 113.50 6723823 The first line indicates that the stock symbol is MSMT, today's opening price was 112.50, the closing was 115.75, today's high was 116.50 and so on. Text file needed for this program(data on stocks): http://dl.dropbox.com/u/78573135/StockData.txt design and implement a stock object a (Stock Object) Design and implement the stock object. Class the class that captures the various characteristics of the object stockType. the main componenets of a stock are the stock symbol, price and number of shares. Moreover, we need to output the opening price closing price, high/low price, previous price and percent loss/gain for the day. These are also characteristics of a stock. The stock should store all of this information. Perform the following operations on each stock object: []Set stock information [] Print stock information []Show different prices []Calculate and price the percent gain/loss []Show number of shares **The natual order of the stock list is by the stock symbol. Overload the relational operators to compare two stock objects by their symbols. **Overload '<<' for easy output **because the data is stored in a file, overload the stream operator '>>' for easy input For example, suppose the infile is an ifstream object and the input file was opened using the object infile. Further suppose that myStock is a stock object. Then, the statement: infile >> myStock; reads the data from the input file and stores it in the object myStock.

I have the project just about done but it will not compile. Below is my code, if someone could tell me where I am going wrong or what I am missing I would greatly appreciate it.

*************************************

stockApp.h

class stockApp

{

public:

stockApp();

~stockApp();

};

#ifndef STOCK_APP_H

#define STOCK_APP_H

#include "stockListType.h"

#include

#include

#include

#include

using namespace std;

class stockApp {

public:

stockApp();

void execute();

private:

stockListType lst;

void menu();

int getSelection(string);

string changeFileName();

bool fileExists(const string&);

};

#endif //!STOCK_APP_H

***************************

stockApp.cpp

#include "stockApp.h"

stockApp::stockApp() {}

void stockApp::execute()

{

menu();

}

void stockApp::menu()

{

char goAgain = ' ';

int selection;

//if the default input file exists, populate

//the stock object list with the data

if (fileExists(lst.getFileName()))

//populate the lsit with the items in the default input file

lst.populateList();

//if the default input file does not exist, display warning

else

cout << "Warning: the input file " << lst.getFileName() << " does not exist." << endl <<

"Please ensure the file is present or enter the name of a new file by selecting option 4 from the main menu." << endl;

do {

cout << "********* First Investor's Heaven *********" << endl;

cout << "******* Financial Reporting Software *******" << endl << endl;

cout << "1: Print a stock reports to file" << endl;

cout << "2: Print a stock report to screen" << endl;

cout << "3: Print stock report to screen sorted by percentage gain" << endl;

cout << "4: Chage the input file name" << endl;

cout << "5: Exit" << endl << endl;

selection = getSelection("Please make a selection."); //get a valid user menu selection

system("CLS");

switch (selection)//run the desired function based on input from user

{

case 1: //create file reports

lst.createFileReport();

lst.createFileReportByGainLoss();

break;

case 2: //print report to screen

lst.printScreenReport();

break;

case 3: //print report to screen sorted by gain

lst.printScreenReportByGain();

break;

case 4: //change input file name

lst.clearList();

lst.setFileName(changeFileName());

lst.populateList();

break;

case 5: //exit

exit(0);

break;

default:

cout << "An error has occured, the program will now close." << endl;

}

cout << endl;

cout << "Would you like to return to the menu? y/n" << endl;

cin >> goAgain;

system("CLS");

} while (tolower(goAgain) == 'y'); //end do/while

system("CLS");

cout << "The program will now exit." << endl;

}

int stockApp::getSelection(string prompt)

{

bool valid = false;

string input;

regex pattern("^[+]?[1-5]$");

do

{

cout << prompt << endl;

cin >> input;

if (!regex_match(input, pattern))

{

cout << "This is not a valid selection, please try again." << endl;

}

else

valid = true;

} while (!valid);

return stoi(input);

}

string stockApp::changeFileName()

{

bool valid = false;

string tempName;

string defaultFileName = "stocks.txt";

string currentFileName = lst.getFileName();

regex pattern("^[A-za-z0-9]+\.txt$");

cout << "The current file being read is " << currentFileName << ". If you would like to keep using this file, type exit to continue." << endl;

cout << "If you would like to change this file, enter the name of the new file, including the file extension and press enter." << endl;

cout << "If you would like to reset the file name to the default file name of " << defaultFileName << ", type default and press enter." << endl;

do {

cout << "Make a selection: ";

cin >> tempName;

cout << endl;

transform(tempName.begin(), tempName.end(), tempName.begin(), ::tolower);

if (tempName == "exit")

{

tempName = currentFileName;

cout << "File name was not changed." << endl;

valid = true;

}

else if (tempName == "default")

{

tempName = defaultFileName;

cout << "File name was set to default." << endl;

valid = true;

}

else if (!fileExists(tempName))

{

cout << "The file " << tempName << " does not exist. Please try again." << endl;

valid = false;

}

else if (regex_match(tempName, pattern))

{

cout << "The file name was changed to " << tempName << endl;

valid = true;

}

else

{

cout << "This is not a valid file path. Please only enter files ending in .txt" << endl;

cout << "File name unchanged." << endl;

valid = false;

}

} while (!valid);

return tempName;

}

bool stockApp::fileExists(const string& fileName)

{

struct stat buffer;

return(stat(fileName.c_str(), &buffer) == 0);

}

***********************************

stockType.h

#ifndef STOCK_TYPE_H

#define STOCK_TYPE_H

#include

#include

#include

using namespace std;

class stockType

{

friend ostream& operator<<(ostream&, stockType&);

friend istream& operator>>(istream&, stockType&);

public:

stockType();

string getSymbol();

double getTotalValue();

int getNumOfShares();

double getOpeningPrice();

double getClosingPrice();

double getHighPrice();

double getLowPrice();

double getPreviousPrice();

double getPercentGainLoss();

void setSymbol(string);

void setNumOfShares(int);

void setOpeningPrice(double);

void setClosingPrice(double);

void setHighPrice(double);

void setLowPrice(double);

void setPreviousPrice(double);

bool operator == (stockType&);

bool operator !=(stockType&);

bool operator <= (stockType&);

bool operator <(stockType&);

bool operator >=(stockType&);

bool operator>(stockType&);

private:

string symbol;

double totalValue;

int numOfShares;

double openingPrice;

double closingPrice;

double highPrice;

double lowPrice;

double previousPrice;

double percentGainLoss;

};

#endif

************************

stockType.cpp

#include "stockType.h"

/********************

STREAM OPERATOR OVERLOADS

*********************/

ostream& operator<<(ostream& out, stockType& temp)

{

char tab = '\t';

out.imbue(locale(""));

out << setprecision(2) << fixed;

out << temp.getSymbol() << tab << setw(6) << temp.getOpeningPrice() << tab << setw(6) <<

temp.getClosingPrice() << tab << setw(7) << temp.getHighPrice() << tab << setw(8) << temp.getLowPrice() <<

setw(8) << temp.getPreviousPrice() << setw(10) << temp.getPercentGainLoss() << "%" << tab <<

setw(11) << temp.getNumOfShares() << endl;

return out;

}

istream& operator>>(istream& in, stockType& obj)

{

string temp;

double val;

in >> temp;

obj.setSymbol(temp);

in >> val;

obj.setOpeningPrice(val);

in >> val;

obj.setClosingPrice(val);

in >> val;

obj.setHighPrice(val);

in >> val;

obj.setLowPrice(val);

in >> val;

obj.setPreviousPrice(val);

in >> val;

obj.setNumOfShares(val);

return in;

}

/***************

CONSTRUCTORS

****************/

stockType::stockType() {}

/***************

ACCESSORS

****************/

string stockType::getSymbol()

{

return this->symbol;

}

double stockType::getTotalValue()

{

this->totalValue = closingPrice * numOfShares;

return totalValue;

}

int stockType::getNumOfShares()

{

return this->numOfShares;

}

double stockType::getOpeningPrice()

{

return this->openingPrice;

}

double stockType::getClosingPrice()

{

return this->closingPrice;

}

double stockType::getHighPrice()

{

return this->highPrice;

}

double stockType::getLowPrice()

{

return this->lowPrice;

}

double stockType::getPreviousPrice()

{

return this->previousPrice;

}

double stockType::getPercentGainLoss()

{

percentGainLoss = ((closingPrice - previousPrice) / previousPrice) * 100;

return percentGainLoss;

}

/************

MUTATORS

*************/

void stockType::setSymbol(string symbol)

{

this->symbol = symbol;

}

void stockType::setNumOfShares(int numOfShares)

{

this->numOfShares = numOfShares;

}

void stockType::setOpeningPrice(double openingPrice)

{

this->openingPrice = openingPrice;

}

void stockType::setClosingPrice(double closingPrice)

{

this->closingPrice = closingPrice;

}

void stockType::setHighPrice(double highPrice)

{

this->highPrice = highPrice;

}

void stockType::setLowPrice(double lowPrice)

{

this->lowPrice = lowPrice;

}

void stockType::setPreviousPrice(double previousPrice)

{

this->previousPrice = previousPrice;

}

/****************************

RELATIONAL OPERATOR OVERLOADS

*****************************/

bool stockType::operator==(stockType& obj)

{

return symbol == obj.getSymbol();

}

bool stockType::operator!=(stockType&obj)

{

return symbol != obj.getSymbol();

}

bool stockType::operator<=(stockType& obj)

{

return symbol <= obj.getSymbol();

}

bool stockType::operator<(stockType& obj)

{

return symbol < obj.getSymbol();

}

bool stockType::operator>=(stockType& obj)

{

return symbol >= obj.getSymbol();

}

bool stockType::operator>(stockType& obj)

{

return symbol > obj.getSymbol();

}

******************

stockListType.h

#ifndef STOCK_LIST_TYPE_H

#define STOCK_LIST_TYPE_H

#include"stockType.h"

#include "listType.h"

#include

#include

#include

using namespace std;

class stockListType:public listType

{

public:

stockListType();

void populateList();

void clearList();

string getFileName();

double getTotalAssetsValue();

void setFileName(string);

void createFileReport();

void createFileReportByGainLoss();

void printScreenReport();

void printScreenReportByGain();

private:

listType t;

double totalAssetsValue;

string fileName = "stocks.txt";

void addToList(stockType);

void createHeader(ostream&);

void createFooter(ostream&);

int* sortGainLoss();

int countLines();

};

#endif

***************************

stockListType.cpp

#include "stockListType.h"

#include "stockType.h"

#include "listType.h"

#include

#include

stockListType::stockListType():listType(), totalAssetsValue(0) {}

void stockListType::populateList()

{

ifstream file;

file.open(fileName);

int lines = countLines();

int i = 0;

stockType temp;

if (file.good() && file.is_open())

{

while (!file.eof() && i < lines)

{

file >> temp;

addToList(temp);

i++;

}

}

file.close();

t.sort();

}

int stockListType::countLines()

{

int lines = 0;

string s = "";

ifstream file;

file.open(fileName);

if (file.good() && file.is_open())

{

while (!file.eof())

{

getline(file, s);

s.erase(remove_if(s.begin(), s.end(), isspace), s.end()); //eliminate all spaces from the read lines

if (!s.empty())

{

lines++;

s = "";

}

}

}

return lines;

}

void stockListType::addToList(stockType obj)

{

t.insertAtt(obj, t.getLength());

}

void stockListType::clearList()

{

t.clear();

}

string stockListType::getFileName()

{

return this->fileName;

}

double stockListType::getTotalAssetsValue()

{

stockType temp;

double totalAssetsValue = 0;

for (int i = 0; i < t.getLength(); i++)

{

temp = t.getAt(i);

totalAssetsValue += (temp.getNumOfShares() * temp.getClosingPrice());

}

return totalAssetsValue;

}

void stockListType::setFileName(string fileName)

{

this->fileName = fileName;

}

void stockListType::createFileReport()

{

ofstream file;

file.open("Stock Report - By Symbol.txt");

stockType temp;

createHeader(file);

for(int i = 0; i < t.getLength(); i++)

{

temp = t.getAt(i);

file << temp;

}

file << "Closing Assets: $" << getTotalAssetsValue() << endl;

createFooter(file);

file.flush();

file.close();

cout << "Files created successfully." << endl;

}

void stockListType::printScreenReport()

{

stockType temp;

createHeader(cout);

for (int i = 0; i < t.getLength(); i++)

{

temp = t.getAt(i);

cout << temp;

}

cout << "Closing Assets: $" << getTotalAssetsValue() << endl;

createFooter(cout);

}

//create a report file from the stock data sorted by gain/loss

void stockListType::createFileReportByGainLoss()

{

ofstream file;

file.open("Stock Report - By Gain.txt");

stockType temp;

int* sortIndicesGainLoss = new int[t.getLength()];

//sortIndicesGainLoss = sortGainLoss(); //create a list of indices for the stock data sorted by percentage gain

createHeader(file);

for (int i = 0; i < t.getLength(); i++) //iterate over the stock data objects sorted by gain

{

temp = t.getAt(sortIndicesGainLoss[i]);

file << temp;//output to file using the overloaded stream insertion operator

}

file << "Closing Assets: $" << getTotalAssetsValue() << endl; //add the value of all stock assets to the report

createFooter(file);

file.flush(); //flush the file stream

file.close(); //close the file

} //end createFileReportByGainLoss

void stockListType::printScreenReportByGain()

{

stockType temp;

int* sortIndicesGainLoss = new int[t.getLength()];

sortIndicesGainLoss = sortGainLoss();

createHeader(cout);

for (int i = 0; i < t.getLength(); i++)

{

temp = t.getAt(sortIndicesGainLoss[i]);

cout << temp;

}

cout << "Closing Assets: $" << getTotalAssetsValue() << endl;

createFooter(cout);

}

void stockListType::createHeader(ostream & file)

{

file << "********* First Investor's Heaven *********" << endl;

file << "********* Financial Report *********" << endl;

file << "Stock Today Previous Percent" << endl;

file << "Symbol Open Close High Low Close Gain Volume" << endl;

file << "------ ----- ----- ----- ----- ----- ----- -----" << endl;

}

void stockListType::createFooter(ostream & file)

{

file << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*";

}

int* stockListType::sortGainLoss()

{

double min = 0;

double max;

int* sortIndicesGainLoss = new int[t.getLength()];

for (int i = 0; i < t.getLength(); i++)

{

for (int j = 0; j < t.getLength() - 1; j++)

{

if (t.getAt(sortIndicesGainLoss[j]).getPercentGainLoss() < t.getAt(sortIndicesGainLoss[j + 1]).getPercentGainLoss())

{

int temp = sortIndicesGainLoss[j];

sortIndicesGainLoss[j] = sortIndicesGainLoss[j + 1];

sortIndicesGainLoss[j + 1] = temp;

}

}

}

return sortIndicesGainLoss;

}

*********************************\

listType.h

#pragma once

#ifndef STOCK_TYPE_H

#define STOCK_TYPE_H

#include

#include

#include

using namespace std;

class stockType

{

friend ostream& operator<<(ostream&, stockType&);

friend istream& operator>>(istream&, stockType&);

public:

stockType();

string getSymbol();

double getTotalValue();

int getNumOfShares();

double getOpeningPrice();

double getClosingPrice();

double getHighPrice();

double getLowPrice();

double getPreviousPrice();

double getPercentGainLoss();

void setSymbol(string);

void setNumOfShares(int);

void setOpeningPrice(double);

void setClosingPrice(double);

void setHighPrice(double);

void setLowPrice(double);

void setPreviousPrice(double);

bool operator == (stockType&);

bool operator != (stockType&);

bool operator <=(stockType&);

bool operator<(stockType&);

bool operator>=(stockType&);

bool operator >(stockType&);

private:

string symbol;

double totalValue;

int numOfShares;

double openingPrice;

double closingPrice;

double highPrice;

double lowPrice;

double previousPrice;

double percentGainLoss;

};

#endif

****************************

main.cpp

#include "main.h"

main::main()

{

}

main::~main()

{

}

#include "stockApp.h"

#include "stockListType.h"

#include "listType.h"

#include

using namespace std;

int main() {

stockApp s;

s.execute();

system("PAUSE");

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2017 Skopje Macedonia September 18 22 2017 Proceedings Part 3 Lnai 10536

Authors: Yasemin Altun ,Kamalika Das ,Taneli Mielikainen ,Donato Malerba ,Jerzy Stefanowski ,Jesse Read ,Marinka Zitnik ,Michelangelo Ceci ,Saso Dzeroski

1st Edition

3319712721, 978-3319712727

More Books

Students also viewed these Databases questions

Question

Does it avoid use of underlining?

Answered: 1 week ago