Write a program to help a local stock trading company automate its systems. The
ID: 653775 • Letter: W
Question
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:
ABC 123.45 130.95 132.00 125.00 120.50 10000
MSET 120.00 140.00 145.00 140.00 115.00 30920
The first line indicates that the stock symbol is MSMT, today's opening price was 123.45, the closing price was 130.95, today's high price was 132.00, today's low price was 125.00, yesterday's closing price was 120.50, and the number of shares currently being held is 10000
.
The listing sorted by stock symbols must be of the following form:
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.
(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, opening price, closing price, high price, low price, previous price, percent gain/loss for the day (percentChange = (todayClosePrice - yesterdayClose) / yesterdayClose), and number of shares.
Therefore, the stock object should store all this information.
Perform the following operations on each stock object:
i. Set the stock information.
ii. Get the stock information.
iii Calculate and print the percent gain/ loss.
iv Calculate and print total closingAssets
(closingAssets for each stock = ClosePrice * noOfShares)
v. Print the stock information.
a.1. The natural ordering of the stock list is by stock symbol.
Overload the relational operators to compare two stock
objects by their symbols.
a.2. Overload the insertion operator, ?, for easy output.
a.3. Because the data is stored in a file, overload the stream
extraction operator, ?, for easy input.
For example, suppose 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.
(Note that this statement reads and stores the data in the relevant components of myStock.)
Now that you have designed and implemented the class stockType to implement a stock object in a program, it is time to create a list of stock objects.
Let us call the class to implement a list of stock objects stockListType.
The class stockListType must be derived from the class listType, included with this project. 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 requires you to produce the list ordered by the percent gain/loss, you need to sort the stock list by this component.
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 this component.
To do so, add a member variable, an array, to hold the indices of the stock list ordered by the component percent gain/ loss. Call this array sortlndicesGainLoss. When printing the list ordered by the component percent gain/loss, use the array sortlndicesGainLoss to print the list. The elements of the array sortlndicesGainLoss will tell which component of the stock list to print next.
Write a program that uses these two classes to automate the company's analysis of stock data.
The below is an include headerfile for listType
//Header file listType.h
#ifndef H_listType
#define H_listType
#include <iostream>
#include <fstream>
using namespace std;
template <class T>
class listType
{
public:
bool isEmptyList() const;
// Function returns a nonzero value (TRUE)if list is empty,
// otherwise it returns the value 0 (False)
bool isFullList() const;
// Function returns a nonzero value (TRUE)if list is full,
// otherwise it returns the value 0 (False)
void setLength(int len);
int showLength() const;
void search(T searchItem) const;
// Search the list for searchItem
// Postcondition: found is set to a nonzero value (TRUE)if
// searchItem is found in the list,
// otherwise found is set to 0(False)
void insert(T newElement);
// Inserte newElement in the list
// Prior to insertion list must not be full
// Postcondition: list is old list plus the newElement
void deleteItem(T deleteElement);
// if deleteElement is found in the list it is deleted
// If list is empty output the message "Cannot delete from the
// empty list"
// Postcondition: list is old list minus the deleteItem if
// deleteItem is found in the list
void sort();
// sort the list
// Precondition: list must exist
// Postcondition: list elements are in ascending order
void print() const;
// Output the elements of the list
void getList(ifstream&);
// read and store elements in the list
// Postcondition: length = number of elements in the list
// elements = array holding the input data
void destroyList();
// Postcondition: length = 0
void printList() const;
// Output the elements of the list
listType(int listSize);
// constructor with parameters
// Create an array of size specified by the parameter listSize
// Postcondition: elements contains the base address
// of the array, length = 0 and maxsize = listSize
listType();
// default constructor
// Create an array of 50 components
// Postcondition: elements contains the base address
// of the array, length = 0 and maxsize = 50
~listType();
// destructor
// delete all elements of the list
// Postcondition: array elements is deleted
protected:
void binarySeacrh(T searchItem,
int& found, int& index);
int maxSize; // maximum number that can be stored in the list
int length; // number of elements in the list
T *elements; //pointer to the array that holds list elements
};
// constructor to set the array size specified by the user
template <class T>
listType<T>::listType(int listSize)
{
maxSize = listSize;
length = 0;
elements = new T[maxSize];
}
template <class T>
listType<T>::listType() // default constructor
{
maxSize = 50;
length = 0;
elements = new T[50];
}
template <class T>
listType<T>::~listType() //destructor
{
delete [] elements;
}
template <class T>
bool listType<T>::isEmptyList() const
{
return (length == 0);
}
template <class T>
bool listType<T>::isFullList() const
{
return (length == maxSize );
}
template <class T>
void listType<T>::sort() //selection sort
{
int i, j;
int min;
T temp;
for (i = 0; i <length; i++)
{
min = i;
for (j = i+1; j < length; ++j)
if (elements[j] < elements[min])
min = j;
temp = elements[i];
elements[i] = elements[min];
elements[min] = temp;
}//end for
}//end sort
template <class T>
void listType<T>::print() const
{
int i;
for (i = 0; i < length; i++)
cout << elements[i] << endl;
cout << endl;
}//end print
template <class T>
void listType<T>::getList(ifstream& infile)
{
int i;
for (i = 0; i < length; i++)
infile >> elements[i];
}
template <class T>
void listType<T>::search(T searchItem) const
{
int found;
int index;
binarySeacrh(searchItem,found,index);
if (found)
cout << "Item is in the list" << endl;
else
cout << "Item is not in the list" << endl;
}
template <class T>
void listType<T>::binarySeacrh(T searchItem,
int& found, int& index)
{
int first = 0;
int last = length -1;
int mid;
found = 0;
while( !found && (first <= last))
{
mid = (first + last) / 2;
if (elements[mid] == searchItem)
found = 0;
else if (elements[mid] > searchItem)
last = mid - 1;
else
first = mid + 1;
}
loc = mid;
}
template <class T>
void listType<T>::setLength(int len)
{
length = len;
}
template <class T>
int listType<T>::showLength() const
{
return length;
}
#endif
Financial Report Stock Symbol OpenClose High Low Today Previous Percent close Gain Volume 8.67% 9.64% 99% 5.33% MSET 120.00 140.00 145.00 140.00 115.00 21.74% ABC 123.45 130.95 132.00 125.00 12.50 5000 25000 15000 30920 AOLK 80.00 75.00 82.00 74.0 83.00 CSCO 100.00 102.0 105.00 98. 101.00 IBD 68.00 71.00 72.00 67.00 75.00 Closing Assets: $962830e.00 FirstInverstor's Heaven Financial Report Gain/Loss Stock Symbol OpenClose High Low Today Previous Percent close Gain Volume MSET 120.00 140.00 145.00 140.00 115.00 21.74% 8.67% 99% 5.33% 9.64% 30920 ABC 123.45 130.95 132.00 125.00 12.50 CSCO 100.00 102.0 105.00 98. 101.00 IBD 68.00 71.00 72.00 67.00 75.00 AOLK 80.00 75.00 82.00 74.0 83.00 25000 15000 5000 Press any key to continue...Explanation / Answer
program :
#define H_myString
#define H_myString
#include <iostream>
using namespace std;
class newString
{
friend ostream& operator<< (ostream&, const newString&);
friend istream& operator>> (istream&, newString&);
public:
const newString& operator=(const newString& rightStr);
newString(const char *);
newString();
newString(const newString& rightStr);
~newString();
char &operator[] (int);
const char &operator[](int) const;
bool operator==(const newString& rightStr) const;
bool operator!=(const newString& rightStr) const;
bool operator<=(const newString& rightStr) const;
bool operator<(const newString& rightStr) const;
bool operator>=(const newString& rightStr) const;
bool operator>(const newString& rightStr) const;
private:
char *strPtr; //pointer to the char array
//that holds the string
int strLength; //data member to store the length
//of the string
};
#include <iostream>
#include <cassert>
#include <cstring>
#include <iomanip>
#include "myString.h"
using namespace std;
const newString& newString::operator=(const newString &rightStr)
{ if(this != &rightStr) //avoid self-copy
{ delete [] strPtr;
strLength = rightStr.strLength;
strPtr = new char[strLength + 1];
assert(strPtr != NULL);
strcpy(strPtr, rightStr.strPtr);
}
return *this;
}
newString::newString(const char *str)
{
strLength = strlen(str);
strPtr = new char[strLength + 1];
assert(strPtr != NULL);
strcpy(strPtr, str);
}
newString::newString()
{
strLength = 0;
strPtr = new char[1];
assert(strPtr != NULL);
strcpy(strPtr, "");
}
newString::newString(const newString& rightStr)
{
strLength = rightStr.strLength;
strPtr = new char[strLength + 1];
assert(strPtr != NULL);
strcpy(strPtr, rightStr.strPtr);
}
newString::~newString()
{ delete [] strPtr;
}
char& newString::operator[] (int index)
{
assert(0 <= index && index < strLength);
return strPtr[index];
}
const char& newString::operator[](int index) const
{
assert(0 <- index && index < strLength);
return strPtr[index];
}
bool newString::operator==(const newString& rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) == 0);
}
bool newString::operator!=(const newString& rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) != 0);
}
bool newString::operator<=(const newString& rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) <= 0);
}
bool newString::operator<(const newString& rightStr) const
{ return (strcmp(strPtr, rightStr.strPtr) < 0);
}
bool newString::operator>=(const newString& rightStr) const
{ return (strcmp(strPtr, rightStr.strPtr) >= 0);
}
bool newString::operator>(const newString& rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) > 0);
}
ostream& operator<< (ostream &out, const newString &rightStr)
{ out << rightStr.strPtr;
return out;
}
istream& operator>> (istream &in, newString &rightStr)
{
char temp[81]; in >> setw(81) >> temp;
rightStr = temp;
return in;
}
StockType class
class stockType
{
friend ostream& operator<< (ostream&, stockType&);
friend ifstream& operator>> (ifstream&, stockType&);
public:
void setStockInfo(newString symbol, double openPrice,
double closeProce, double high,
double Low, double prevClose,
int shares);
newString getSymbol();
double getPercentChange();
double getOpenPrice();
double getClosePrice();
double getHighPrice();
double getLowPrice();
double getPrevPrice();
int getNoOfShares();
stockType();
stockType(newString symbol, double openPrice,
double closeProce, double high,
double Low, double prevClose,
int shares);
int operator ==(stockType &other);
int operator !=(stockType &other);
int operator <=(stockType &other);
int operator >=(stockType &other);
int operator >(stockType &other);
int operator <(stockType &other);
private:
newString stockSymbol;
double todayOpenPrice;
double todayClosePrice,todayHigh,todayLow;
double yesterdayClose;
double percentChange;
int noOfShares;
};
ostream& operator<< (ostream &out, stockType &obj)
{
out << setprecision(2) << fixed << right;
out << setw(6) << obj.stockSymbol;
out << setw(8) << obj.todayOpenPrice;
out << setw(8) << obj.todayClosePrice;
out << setw(8) << obj.todayHigh;
out << setw(8) << obj.todayLow;
out << setw(10) << obj.yesterdayClose;
out << setw(8) << obj.getPercentChange() << "%";
out << setw(8) << obj.noOfShares;
out << endl;
return out;
}
ifstream& operator>> (ifstream &in, stockType &obj)
{
in >> obj.stockSymbol;
in >> obj.todayOpenPrice;
in >> obj.todayClosePrice;
in >> obj.todayHigh;
in >> obj.todayLow;
in >> obj.yesterdayClose;
in >> obj.noOfShares;
return in;
}
void stockType::setStockInfo(newString symbol, double openPrice, double closePrice, double high, double low, double prevClose,int shares)
{
stockSymbol = symbol;
todayOpenPrice = openPrice;
todayClosePrice = closePrice;
todayHigh = high;
todayLow = low;
yesterdayClose = prevClose;
noOfShares = shares;
}
newString stockType::getSymbol()
{
return stockSymbol;
}
double stockType::getPercentChange()
{
double rslt = 0.0;
rslt = (todayClosePrice - yesterdayClose) / yesterdayClose * 100;
return rslt;
}
double stockType::getOpenPrice()
{ return todayOpenPrice;}
double stockType::getClosePrice()
{ return todayClosePrice;
}
double stockType::getHighPrice()
{
return todayHigh;
}
double stockType::getLowPrice()
{ return todayLow;}
double stockType::getPrevPrice()
{ return yesterdayClose;}
int stockType::getNoOfShares()
{ return noOfShares;}
stockType::stockType()
{ stockSymbol = "";
todayOpenPrice = 0.0;
todayClosePrice = 0.0;
todayHigh = 0.0;
todayLow = 0.0;
yesterdayClose = 0.0;
noOfShares = 0;
}
stockType::stockType(newString symbol, double openPrice, double closePrice, double high,double low, double prevClose, int shares)
{
stockSymbol = symbol;
todayOpenPrice = openPrice; todayClosePrice = closePrice;
todayHigh = high;
todayLow = low;
yesterdayClose = prevClose;
noOfShares = shares;}
int stockType::operator ==(stockType &other)
{ return (stockSymbol == other.stockSymbol);
}
int stockType::operator !=(stockType &other)
{
return (stockSymbol != other.stockSymbol);
}
int stockType::operator <=(stockType &other)
{
return (stockSymbol <= other.stockSymbol);
}
int stockType::operator >=(stockType &other)
{
return (stockSymbol >= other.stockSymbol);
}
int stockType::operator >(stockType &other)
{
return (stockSymbol > other.stockSymbol);
}
int stockType::operator <(stockType &other)
{
return (stockSymbol < other.stockSymbol);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.