Write a program to automate a stock trading company. At the end of each trading
ID: 3861713 • Letter: W
Question
Write a program to automate a stock trading company. At the end of each trading day, the company would like to generate and post the listing of its stocks. The desired output is to produce a sorted list by stock symbol.
Create an input data file in the following format, seven data separated by a single space, don’t enter the heading:
Create a class, stockListType, to implement a vector of stock objects.
The class stockListType is a template, designed to create a list of stock objects.
Design and implement a class called stockType.
the data members are symbol, openPrice, closePrice, highPrice, lowPrice, prevPrice, percentGainOrLoss, and numOfShares.
the functions:
set the stock information.
print the stock information,
show the different prices.
calculate and print the percent of gain/loss.
The formula is (TodayClose – YesterdayClose) / YesterdayClose * 100.
overload the relational operators to compare two stock objects by their symbols
overload the insert '<<' and extraction '>>' operators, << , for easy input/output.
Add and/or overwrite the operations of the class to implement the necessary operations on a stock.
Write a program that uses these two classes to analyze the data and produce the following output:
Please upload the following:
The class .cpp file
The main program
The class .h file
Output File
symbol openingPrice closingPrice todayHigh todayLow prevClose volume MSET 120 140 145 140 115 30920 AOLK 80 75 82 74 83 5000 ABC 123.45 130.95 132 125 120.50 10000 IBD 68 71 72 67 75 15000 CSCO 100 102 105 98 101 25000Explanation / Answer
main.cpp
#include <iostream>//include for input-output stream library
#include <fstream>//include for file stream library
#include <iomanip>//include for io manipulation library
#include <vector>//include for vector library
#include<string> //include for string library
#include<algorithm>//include for algorithm library
#include "stockType.h"//header file which has all the declaration about stock Type objects
#include "stockList.h" //header file which has all the declaration about stock List objects
using namespace std;
const int a = 5;
int flag =0; // Flag for the file checking
void getData(stockList& list) //Function to get data from file and save it in the stock type object
{
ifstream infile;
string fileName;
string symbol;
double OpenPrice;
double ClosePrice;
double tHigh;
double tLow;
double yClose;
int shares;
stockType temp;
cout<<"HOME WORK4: CURRENT DAY STOCK ANALYSER"<<endl;
cout<<"WELCOME TO OHRYRO STOCK CONSULTANTS"<<endl;
cout<<"PLEASE ENTER YOUR STOCK DATA FILE NAME"<<endl;
getline(cin,fileName);// Gets file nam from user
infile.open(fileName);// Opens file
if (!infile) // To throw error for file unavailability
{
cout << "FILE NOT EXISTS. PLEASE ENTER A CORRECT FILE NAME. PROGRAM TERMINATED" << endl;
flag=5;
}
else
{
infile >> symbol;
while (infile) // Moving data from file to stock inturn stock list
{
infile >> OpenPrice >> ClosePrice >> tHigh >> tLow >> yClose >> shares;
temp.setStockInfo(symbol,OpenPrice,ClosePrice,tHigh,tLow,yClose,shares);
list.insert(temp);
infile >> symbol;
}
}
};
int main()
{
stockList stockList; // creating stock list object
cout << fixed << showpoint;
cout << setprecision(2);
getData(stockList); // copying dat a from file to the stcoklist object
if(flag==0)
{
stockList.sortStockList();// Sorting the input data
stockList.printBySymbol();//Printing by sort list by symbol
cout << endl;
stockList.printByGain();//Printing dat by sorted list by gain
}
return 0;
}
stockList.h
#ifndef H_stockList
#define H_stockList
#include <vector>//include for vector library
#include<string> //include for string library
#include <iostream>//include for input-output stream library
#include<algorithm>//include for algorithm library
#include "stockType.h"// Include to use stock type object
using namespace std;
class stockList // StockList class declaration
{
public:
void printBySymbol();
void printByGain();
void printReports();
void sortStockList();
void sortByGain();
void insert(const stockType& item);
private:
vector<int> indexByGain;
vector<stockType> list; // Stock list creation using stock type object
};
void stockList::printBySymbol() // Printing Stock List by Symbol
{
double closingBalance = 0;
cout << " XXXXXXXXXXXXXXXXXXX FIRST INVESTOR'S HAVEN XXXXXXXXXXXXXXXXXXXXXXXXX" << endl;
cout << "XXXXXXXXXXXXXXXXXXX FINANCIAL REPORT XXXXXXXXXXXXXXXXXXXXXXXXX" << endl;
cout << " STOCK TODAY PREVIOUS PERCENT" << endl;
cout << "SYMBOL OPEN CLOSE HIGH LOW CLOSE GAIN VOLUME" << endl;
cout << "------ ----- ----- ----- ----- -------- ------- ------" << endl;
int x=list.size();
int y=0;
while (y<x)
{
cout << list[y] << endl;
closingBalance += list[y].getClosePrice() * list[y].getnoOfShares();
y++;
}
cout << " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" << endl;
cout << "CLOSING ASSETS: $" << closingBalance << endl;
cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" << endl;
}
void stockList::printByGain() // Printing Stock List by Gain
{
cout << "XXXXXXXXXXXXXXXXXXX FIRST INVESTOR'S HAVEN XXXXXXXXXXXXXXXXXXXXXXXXX" << endl;
cout << "XXXXXXXXXXXXXXXXXXX FINANCIAL REPORT XXXXXXXXXXXXXXXXXXXXXXXXX" << endl;
cout << "XXXXXXXXXXXXXXXXXXX GAIN/LOSS XXXXXXXXXXXXXXXXXXXXXXXXX" << endl;
cout << " STOCK TODAY PREVIOUS PERCENT" << endl;
cout << "SYMBOL OPEN CLOSE HIGH LOW CLOSE GAIN VOLUME" << endl;
cout << "------ ----- ----- ----- ----- -------- ------- ------" << endl;
int x=list.size();
int y=0;
while (y<x)
{
cout << list[indexByGain[y]]<<" ";
y++;
}
cout << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" << endl;
}
void stockList::sortStockList() // Sorting stock list by Symbol
{
sort(list.begin(), list.end());
sortByGain();
}
void stockList::insert(const stockType& item)// ethod for inserting stock type object in stock list
{
list.push_back(item);
}
void stockList::sortByGain() // Sorting stock List By Gain
{
bool *temp;
temp = new bool[list.size()];
int maxIndex;
int l=list.size();
int y=0;
while(y<l)
{
temp[y] = false;
y++;
}
l=list.size();
int r=0;
while(r<l)
{
l=list.size();
int j=0;
while(j<l)
{
if (!temp[j])
{
maxIndex = j;
break;
}
j++;
}
l=list.size();
j=maxIndex+1;
while(j<l)
{
if (!temp[j])
{
if (list[maxIndex].getPercentChange() < list[j].getPercentChange())
maxIndex = j;
}
j++;
}
indexByGain.push_back(maxIndex);
temp[maxIndex] = true;
r++;
}
delete [] temp;
}
#endif
stockType.h
#ifndef H_stockType
#define H_stockType
#include <iostream>//include for input-output stream library
#include <fstream>//include for file stream library
#include <iomanip>//include for io manipulation library
#include<string> //include for string library
using namespace std;
class stockType // Declaring Stock Type Class
{
friend ostream& operator<< (ostream&, const stockType&);
friend ifstream& operator>> (ifstream&, stockType&);
public:
void setStockInfo(string symbol, double openPrice,
double closeProce, double high,
double Low, double prevClose,
int shares);
string getSymbol();
double getPercentChange();
double getOpenPrice();
double getClosePrice();
double getHighPrice();
double getLowPrice();
double getPrevPrice();
int getnoOfShares();
stockType();
stockType(string symbol, double openPrice,
double closeProce, double high,
double Low, double prevClose,
int shares);
bool operator ==(const stockType& other) const;
bool operator !=(const stockType& other) const;
bool operator <=(const stockType& other) const;
bool operator >=(const stockType& other) const;
bool operator >(const stockType& other) const;
bool operator <(const stockType& other) const;
private:
string stockSymbol;
double todayOpenPrice;
double todayClosePrice;
double todayHigh;
double todayLow;
double yesterdayClose;
double percentChange;
int noOfShares;
};
void stockType::setStockInfo(string symbol, double openPrice,//Setting Stock Info Method
double closeProce, double high,
double low, double prevClose,
int shares)
{
stockSymbol = symbol;
todayOpenPrice = openPrice;
todayClosePrice = closeProce;
todayHigh = high;
todayLow = low;
yesterdayClose = prevClose;
percentChange = (todayClosePrice - yesterdayClose) / yesterdayClose;
noOfShares = shares;
}
string stockType::getSymbol() // Method for reterieving symbol
{
return stockSymbol;
}
double stockType::getPercentChange()// Method for reterieving percenatge change
{
return percentChange;
}
double stockType::getOpenPrice()// Method for reterieving open price
{
return todayOpenPrice;
}
double stockType::getClosePrice()// Method for reterieving close price
{
return todayClosePrice;
}
double stockType::getHighPrice()// Method for reterieving high Price
{
return todayHigh;
}
double stockType::getLowPrice()// Method for reterieving low price
{
return todayLow;
}
double stockType::getPrevPrice()// Method for reterieving prev price
{
return yesterdayClose;
}
int stockType::getnoOfShares()// Method for reterieving no of shares
{
return noOfShares;
}
stockType::stockType()// Method for setting an empty object for soritng purposes
{
stockSymbol = "";
todayOpenPrice = 0;
todayClosePrice = 0;
todayHigh = 0;
todayLow = 0;
yesterdayClose = 0;
percentChange = 0;
noOfShares = 0;
}
stockType::stockType(string symbol, double openPrice,// Method for writng data in to the object
double closeProce, double high,
double low, double prevClose,
int shares)
{
stockSymbol = symbol;
todayOpenPrice = openPrice;
todayClosePrice = closeProce;
todayHigh = high;
todayLow = low;
yesterdayClose = prevClose;
percentChange = (todayClosePrice - yesterdayClose) / yesterdayClose; // Percentage change calculation
noOfShares = shares;
}
bool stockType::operator ==(const stockType& other) const // Natural Logical Sorting Method for data sorting by Symbol
{
return (stockSymbol == other.stockSymbol);
}
bool stockType::operator !=(const stockType& other) const
{
return (stockSymbol != other.stockSymbol);
}
bool stockType::operator <=(const stockType& other) const
{
return (stockSymbol <= other.stockSymbol);
}
bool stockType::operator >=(const stockType& other) const
{
return (stockSymbol >= other.stockSymbol);
}
bool stockType::operator >(const stockType& other) const
{
return (stockSymbol > other.stockSymbol);
}
bool stockType::operator <(const stockType& other) const
{
return (stockSymbol < other.stockSymbol);
}
ostream& operator<<(ostream& os, const stockType& stock) // Out stream simplification
{
os << setw(6) << stock.stockSymbol << " "
<< setw(6) << stock.todayOpenPrice << " "
<< setw(6) << stock.todayClosePrice << " "
<< setw(6) << stock.todayHigh << " "
<< setw(6) << stock.todayLow << " "
<< setw(6) << stock.yesterdayClose << " "
<< setw(8) << stock.percentChange * 100 << "% "
<< setw(10) << stock.noOfShares;
return os;
}
ifstream& operator>> (ifstream& inf, stockType& stock)// in stream simplification
{
inf >> stock.stockSymbol >> stock.todayOpenPrice
>> stock.todayClosePrice >> stock.todayHigh
>> stock.todayLow >> stock.yesterdayClose
>> stock.noOfShares;
stock.percentChange = (stock.todayClosePrice - stock.yesterdayClose) / stock.yesterdayClose;
return inf;
}
#endif
data.txt
APPLE 123.45 130.95 132.00 125.00 120.50 10000
GOOGLE 120.00 140.00 145.00 140.00 115.00 30920
YAHOO 100 102 105 98 101 25000
TESLA 68 71 72 67 75 15000
CISCO 68 71 72 67 75 15000
sample output
WELCOME TO OHRYRO STOCK CONSULTANTS
PLEASE ENTER YOUR STOCK DATA FILE NAME
dama.txt
XXXXXXXXXXXXXXXXXXX FIRST INVESTOR'S HAVEN XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXX FINANCIAL REPORT XXXXXXXXXXXXXXXXXXXXXXXXX
STOCK TODAY PREVIOUS PERCENT
SYMBOL OPEN CLOSE HIGH LOW CLOSE GAIN VOLUME
------ ----- ----- ----- ----- -------- ------- ------
APPLE 56.00 130.95 132.00 90.00 120.50 8.67% 10000
CISCO 68.00 71.00 72.00 67.00 75.00 -5.33% 15000
GOOGLE 72.00 140.00 50.00 9.00 115.00 21.74% 30920
INFY 68.00 60.00 72.00 67.00 75.00 -20.00% 23000
MICRO 68.00 89.00 72.00 67.00 75.00 18.67% 40000
NEXUS 68.00 93.00 72.00 67.00 75.00 24.00% 60000
TCS 68.00 71.00 72.00 67.00 75.00 -5.33% 15000
TESLA 68.00 71.00 72.00 67.00 75.00 -5.33% 15000
WIPRO 68.00 57.00 72.00 67.00 75.00 -24.00% 15000
YAHOO 100.00 102.00 105.00 98.00 101.00 0.99% 25000
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CLOSING ASSETS: $22758300.00
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.