Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C++)(Stock Market) Write a program to help a local stock trading company automa

ID: 3806729 • Letter: #

Question

(C++)(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:

********* First Investor's Heaven **********

********* Financial Report **********

Stock Today Previous Percent

Symbol Open Close High Low Close Gain Volume

------ ----- ----- ----- ----- -------- ------- ------

ABC 123.45 130.95 132.00 125.00 120.50 8.67% 10000

AOLK 80.00 75.00 82.00 74.00 83.00 -9.64% 5000

CSCO 100.00 102.00 105.00 98.00 101.00 0.99% 25000

IBD 68.00 71.00 72.00 67.00 75.00 -5.33% 15000

MSET 120.00 140.00 145.00 140.00 115.00 21.74% 30920

Closing Assets: $9628300.00

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

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.

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:

i. Set the stock information.

ii. Print the stock information.

iii. Show the different prices.

iv. Calculate and print the percent gain/loss.

v. Show the number of shares.

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.)

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 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, which you designed 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.

994 | Chapter 13: Overloading and Templates

The following statement derives the class stockListType from the

class listType.

class stockListType: public listType<stockType>

{

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

sortIndicesGainLoss. When printing the list ordered by the

component percent gain/loss, use the array sortIndicesGainLoss

to print the list. The elements of the array sortIndicesGainLoss

will tell which component of the stock list to print next.

c. Write a program that uses these two classes to automate the company’s

analysis of stock data.

Explanation / Answer

Solution:

Executable Code:

// Include header files

#include <cstdlib>

#include <iostream>

#include <iomanip>

#include <fstream>

#include <cassert>

#include<string>

using namespace std;

// Class listType

template <class stockType>

class listType

{

// Public memebers

public:

// Method to check empty

bool checkEmpty() const;

// Method to check full

bool checkFull() const;

// Method to get the length

int get_length() const;

// Method to get the maximum size

int get_max_size() const;

// Method to sort the list

void sortList();

// Method to display

void display() const;

// Destructor

~listType();

// Method to insert at position

void insert_at_position(const stockType& item, int position);

// Constructor

listType(int listSize = 50);

// Protected members

protected:

// Maximum size

int max_size;

// Length

int len;

// Create list of stockType

stockType *lis;

};

// Check array is empty

template <class stockType>

bool listType<stockType>::checkEmpty() const

{

return (len == 0);

}

// Check array is full

template <class stockType>

bool listType<stockType>::checkFull() const

{

return (len == max_size);

}

// Return the length of the array

template <class stockType>

int listType<stockType>::get_length() const

{

return len;

}

// Return maximum size of the array

template <class stockType>

int listType<stockType>::get_max_size() const

{

return max_size;

}

// Default constructor

template <class stockType>

listType<stockType>::listType(int listSize)

{

max_size = listSize;

len = 0;

lis = new stockType[max_size];

}

// Destructor

template <class stockType>

listType<stockType>::~listType()

{

delete [] lis;

}

// Sort the array

template <class stockType>

void listType<stockType>::sortList()

{

int di, dj;

int min;

stockType te;

for(di = 0; di < len; di++)

{

min = di;

for(dj = di; dj < len; ++dj)

if (lis[dj] < lis[min])

min = dj;

te = lis[di];

lis[di] = lis[min];

lis[min] = te;

}

}

// Display the list

template <class stockType>

void listType<stockType>::display() const

{

int i;

for(i = 0; i < len; ++i)

cout << lis[i];

cout << endl;

}

// Insert element at the position

template <class stockType>

void listType<stockType>::insert_at_position(const stockType& item, int position)

{

assert(position >= 0 && position < max_size);

lis[position] = item;

len++;

}

class stockListType

{

// Private members

private:

string stockSym;

double openPrice;

double closePrice;

double high;

double low;

int no_of_shar;

double prev_closePrice;

static double totalasset;

double perGainLoss;

// Public memebers

public:

void set_stock(string stockSym,double openPrice,double closePrice,

double high, double low, int no_of_shar, double prev_closePrice)

{

this->stockSym = stockSym;

this->openPrice = openPrice;

this->closePrice = closePrice;

this->high = high;

this->low = low;

this->no_of_shar = no_of_shar;

this->prev_closePrice = prev_closePrice;

}

void Displa()

{

cout <<"stockListType name is " << stockSym << endl;

}

void showPricesDiff()

{

cout <<"Oening price of today was "<<openPrice <<", closing price was "<<closePrice <<", high price was " <<

high << ", low price was " <<low << " Previous closing price is " << prev_closePrice <<endl;

}

void calcGainLos()

{

perGainLoss = (closePrice-prev_closePrice)/prev_closePrice*100;

}

void showNumOfShare()

{

cout <<"Number of share is " << no_of_shar << endl;

}

//**The natual order of the stock lis is by the stock symbol. Overload the relational operators to compare two stock objects by their symbols.

bool operator> (stockListType &ss)

{

return (this->stockSym.compare(ss.stockSym)>0);

}

bool operator>= (stockListType &ss)

{

return (this->stockSym.compare(ss.stockSym)>=0);

}

bool operator< (stockListType &ss)

{

return (this->stockSym.compare(ss.stockSym)<0);

}

bool operator<= (stockListType &ss)

{

return (this->stockSym.compare(ss.stockSym)<=0);

}

bool operator==(stockListType &ss)

{

return (this->stockSym.compare(ss.stockSym)==0);

}

friend ostream& operator<< (ostream &out, stockListType &ss);

friend istream& operator>> (istream &in, stockListType &ss);

static double getTotalAssets()

{

return totalasset;

}

};

istream& operator>>(istream &in, stockListType &ss)

{

// Since operator<< is a friend of the stockListType class, we can access stockListType's members directly.

in >> ss.stockSym;

in >> ss.openPrice;

in >> ss.closePrice;

in >> ss.high;

in >> ss.low;

in >> ss.prev_closePrice;

in >> ss.no_of_shar;

// cout << ss.totalasset << endl;

ss.totalasset+=ss.no_of_shar*ss.closePrice;

return in;

}

ostream& operator<< (ostream &out, stockListType &ss)

{

ss.calcGainLos();

// Since operator<< is a friend of the stockListType class, we can access stockListType's members directly.

out << setw(8) << left << ss.stockSym <<right << fixed << setprecision(2) << setw(8) <<

ss.openPrice<< fixed << setprecision(2) << setw(8) <<

ss.closePrice<< fixed <<setprecision(2) << setw(8) <<

ss.high<< fixed << setprecision(2) << setw(8) <<

ss.low<< fixed <<setprecision(2) << setw(8) <<

ss.prev_closePrice<< fixed << setprecision(2) << setw(8) <<

ss.perGainLoss<< fixed << setprecision(2) << setw(12) <<

ss.no_of_shar<< endl;

return out;

}

double stockListType::totalasset = 0.0;

int main()

{

ifstream infile("StockData.txt");

listType<stockListType> stock_array(50);

stockListType local_stock;

int count = 0;

if(!infile)

{

cout <<"Unable to open file StockData.txt. so Exiting from program ";

return 0;

}

while(!infile.eof())

{

infile >> local_stock;

stock_array.insert_at_position(local_stock,count++);

}

stock_array.sortList();

cout <<"*************First Investors Heaven*****************"<<endl;

cout <<"*************Financial Report*****************"<<endl;

cout <<"stockListType Today Previous Precent " << endl;

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

cout <<"------    -----   -----    ----- ----- -------- -------   ------"<< endl;

stock_array.display();

cout <<"Closing Assets: $"<<fixed << setprecision(2) << stockListType::getTotalAssets()<< endl;

cout <<"_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*"<<endl;

system("pause");

return 0;

}

Output:

*************First Investors Heaven*****************
*************Financial Report*****************
stockListType Today Previous Precent
Symbol Open Close High Low Close Gain Volume
------ ----- ----- ----- ----- -------- ------- ------
CBA 67.50 75.50 78.75 67.50 65.75 14.83 378233
MSMT 112.50 115.75 116.50 111.75 113.50 1.98 6723823

Closing Assets: $806839103.75
_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*
Press any key to continue . . .