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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

main.cpp

#include "stockApp.h"
#include "stockListType.h"
#include "listType.h"

#include <iostream>

using namespace std;

int main()
{
   stockApp s;

   s.execute();  


   system("PAUSE");
   return 0;
}

stockApp.h

#ifndef STOCK_APP_H
#define STOCK_APP_H

#include "stockListType.h"
#include <regex>
#include <string>
#include <iostream>
#include <algorithm>

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 (fileExists(lst.getFileName())) //if the default input file exists, populate the stock object list with the data  
       lst.populateList(); //populate the list with the items in the default input file.

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

       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 << 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. Change 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 user input
       {
       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 exit" << endl;
           exit(0);
       }

       cout << endl;
       cout << "Would you like to retun to the menu? y/n" << endl; //ask the user if they want to return to the menu. If not, exit.
       cin >> goAgain;

       system("CLS");

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

   system("CLS");
   cout << "The program will now exit" << endl << 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 (must be .txt), 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 << 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 << 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 << "File Name unchanged" << endl << endl;
           valid = false;
       }
   } while (!valid);

   return tempName;
}

bool stockApp::fileExists(const string& fileName)
{
   struct stat buffer;
   return (stat(fileName.c_str(), &buffer) == 0);
}

stockListType.cpp

#include "stockListType.h"

stockListType::stockListType() : listType<stockType>(), 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.insertAt(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;
   }//end for

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

   createFooter(file);


   file.flush();
   file.close();

   cout << "Files created sucessfully" << endl;
}

void stockListType::printScreenReport()
{
   stockType temp;

   createHeader(cout);

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

       cout << temp;
   }//end for

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

   createFooter(cout);
}

void stockListType::createFileReportByGainLoss() //create a report file from the stock data sorted by gain/loss. By Bill Fulton
{
   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); //create the file header

   for (int i = 0; i < t.getLength(); i++) //iterate over the stock data objects sorted by their gain
   {
       temp = t.getAt(sortIndicesGainLoss[i]);

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

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

   createFooter(file); //create the file footer


   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;
   }//end for  

   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++)
       sortIndicesGainLoss[i] = i;


   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;
}

stockListType.h

#ifndef STOCK_LIST_TYPE_H
#define STOCK_LIST_TYPE_H

#include "stockType.h"
#include "listType.h"
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

class stockListType : public listType<stockType>
{
public:

   stockListType();
  
   void populateList();
   void clearList();  
  
   string getFileName();
   double getTotalAssetsValue();

   void setFileName(string);

   void createFileReport();
   void createFileReportByGainLoss();
   void printScreenReport();
   void printScreenReportByGain();

private:
   listType<stockType> t;
   double totalAssetsValue;
   string fileName = "stocks.txt";

   void addToList(stockType);
   void createHeader(ostream&);
   void createFooter(ostream&);
   int* sortGainLoss();

   int countLines();  
};
#endif // !STOCK_LIST_TYPE_H

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.setPerviousPrice(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::setPerviousPrice(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();
}

stockType.h

#ifndef STOCK_TYPE_H
#define STOCK_TYPE_H

#include <string>
#include <iostream>
#include <iomanip>

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 setPerviousPrice(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 // !STOCK_TYPE_H

listType.h

#ifndef STOCK_TYPE_H
#define STOCK_TYPE_H

#include <string>
#include <iostream>
#include <iomanip>

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 setPerviousPrice(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 // !STOCK_TYPE_H

stocks.txt

ABC 123.45 130.95 132.00 125.00 120.50 10000
MSET 120.00 140.00 145.00 140.00 115.00 30920
AOLK 80 75 82 74 83 5000
CSCO 100 102 105 98 101 25000
IBD 68 71 72 67 75 15000

Add a comment
Know the answer?
Add Answer to:
(Stock Market) Write a program to help a local stock trading company automate its systems. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Stock Market) Write a program to help a local stock trading company automate its systems. The...

    (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 19.

  • I need one file please (C++)(Stock Market) Write a program to help a local stock trading...

    I need one file please (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...

  • In C++ Programming Write a program in to help a local restaurant automate its breakfast

    Write a program to help a local restaurant automate its breakfast billing system. The program should do the following:Show the customer the different breakfast items offered by the restaurant.Allow the customer to select more than one item from the menu.Calculate and print the bill.Assume that the restaurant offers the following breakfast items (the price of each item is shown to the right of the item):Use an array menuList of type menuItemType, as defined in Programming Exercise 3. Your program must contain at least the...

  • IN C# Write a program to help a local restaurant automate its lunch billing system. The...

    IN C# Write a program to help a local restaurant automate its lunch billing system. The program should do the following: Show the customer the different lunch items offered by the restaurant. Allow the customer to select more than one item from the menu. Calculate and print the bill. Assume that the restaurant offers the following lunch items (the price of each item is shown to the right of the item): Ham and Cheese Sandwich $5.00 Tuna Sandwich $6.00 Soup...

  • c++ Write a C++ program using the struct keyword to help a local restaurant automate its...

    c++ Write a C++ program using the struct keyword to help a local restaurant automate its breakfast billing system The program should do the following: a. Show the customer the different breakfast items offered by the restaurant. b. Allow the customer to select more than one item from the menu. c. Calculate and print the bill. Assume that the restaurant offers the following breakfast items (the price of each item is shown to the right of the item) Menu Plain...

  • In C++ Programming Write a program in Restaurant.cpp to help a local restaurant automate its breakfast...

    In C++ Programming Write a program in Restaurant.cpp to help a local restaurant automate its breakfast billing system. The program should do the following: Show the customer the different breakfast items offered by the restaurant. Allow the customer to select more than one item from the menu. Calculate and print the bill. Assume that the restaurant offers the following breakfast items (the price of each item is shown to the right of the item): Name Price Egg (cooked to order)...

  • Nash's Trading Post, LLC started the year with $44400 in its Common Stock account and a...

    Nash's Trading Post, LLC started the year with $44400 in its Common Stock account and a credit balance in Retained Earnings of $32600. During the year, the company earned net income of $35500 and declared and paid $14800 of dividends. In addition, the company sold additional common stock amounting to $20700. As a result, the amount of its retained earnings at the end of the year would be - $97700. - $53300. - $118400. - $74000.

  • Firm C has a market value of $500 million and the stock is trading at $25...

    Firm C has a market value of $500 million and the stock is trading at $25 per share. Firm D has a market value of $100 million and the stock is trading at $50 each. Firm C is contemplating acquiring firm D. Firm C’s CFO estimates that the combined firm will be worth $750 million and firm D can be acquired at an acquisition premium of $95 million. If for the outstanding shares of firm D, firm C is willing...

  • when a company participates in a stock buyback parogram. it means that the company is buying...

    when a company participates in a stock buyback parogram. it means that the company is buying shares of its own stock and taking them off the matket. with this simple definition in mind, how would a company's stick butback program affect its earning per share? 24 Each week, you will be asked to respond to the prompt or prompts in the discussion forum. Your initial post should be a minimum of 300 words in length, and is due on Sunday....

  • Question A When a company participates in a stock buyback program, it means that the company...

    Question A When a company participates in a stock buyback program, it means that the company is buying shares of its own stock and taking them off the market. With this simple definition in mind, how would a company's stock buyback program affect its Earnings per Share?

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT