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

Let us assume that you are an investor who holds a portfolio of stocks. You need

ID: 3712384 • Letter: L

Question

Let us assume that you are an investor who holds a portfolio of stocks. You need to design a C+ program to maintain and manage stock price information of 7 days. General Features and Functions Basic features: the program should be able to 1) Store the portfolio information into a 2-dimentional array (N × 8) of String type. The firs . column of the array for stock code, and the rest columns for 7-day prices. You may declar the total number of stocks as a constant valued 10 (i.e., N-10), and initialize the array b setting code to the string "NULL" and prices 0. Below is an exemplar portfolio with stocks 02800 31.25 32.5 33.7 33.2 03700 419.6 422.3421.7429.2411.9412.6410.1 NULL0 NULL0 35.8 35.130.7 0 0 0

Explanation / Answer

StockPortfolio.H

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

using namespace std;

class StockPortfolio
{
public:
   StockPortfolio(void);
   virtual ~StockPortfolio(void);
  
   bool addStock(string code, string price[7]);
   bool retrieveStock(string code);
   bool modifyStock(string code, int day, string price);
   bool deleteStock(string code);
   bool displayStockPortfolio(void);
  
   /*2 dimensional array of stocks */
   string arrStockPortfolio[10][8];

};

StockPortfolio.CPP

#include "StockPortfolio.h"


StockPortfolio::StockPortfolio(void)
{
  
   /* intialize the Stock Portfolio */
   for(int i=0;i<10;i++)
   {
       arrStockPortfolio[i][0] = "NULL";
       for(int j=1;j<8;j++)
       {
          
           arrStockPortfolio[i][j] = "0";
       }
   }
          
}


StockPortfolio::~StockPortfolio(void)
{
}


bool StockPortfolio::addStock(string code, string price[7])
{
   bool isAdded = false;

   if (!retrieveStock(code))
   {
       for(int i=0;i<10;i++)
       {
           if (arrStockPortfolio[i][0] == "NULL")
           {
          
               arrStockPortfolio[i][0] = code;

               for (int j=1;j<8;j++)
               {
          
                   arrStockPortfolio[i][j] = price[j-1];
               }

               isAdded = true;
          
               break;

           }
       }

       if (!isAdded)
           cout << " NOT ADDED. STOCK CODE LIMIT IS FULL" ;
       else
           cout << " ADDED SUCCESSFULLY" ;
   }
   else
       cout << " NOT ADDED. STOCK CODE ALREADY EXISTS" ;


   return isAdded;
}


bool StockPortfolio::retrieveStock(string code)
{
   bool isRetrieved = false;
  
   float prices[7];
   float lowest_price=0;
   float highest_price=0;
   float average_price=0;
   float sum_price_all_days=0;

   for(int i=0;i<10;i++)
   {
       if ((arrStockPortfolio[i][0]).compare(code) == 0)
       {
          
          
           cout << "                       CODE-DAY1-DAY2-DAY3-DAy4-DAY5-DAY6-DAY7 " ;
           cout << "                       -------------------------------------------" ;
           cout << "                          " ;
          
           for (int j=0;j<8;j++)
           {
          
               cout << arrStockPortfolio[i][j] << "-";
              
               if (j > 0)
                   prices[j-1] = atof((arrStockPortfolio[i][j]).c_str());

           }

           for (int i=0;i<7;i++)
           {
               if (i==0)
               {
                   lowest_price = prices[i];
                   highest_price = prices[i];
               }

               if (prices[i] < lowest_price)
                   lowest_price = prices[i];

               if (prices[i] > lowest_price)
                   highest_price = prices[i];

               sum_price_all_days = sum_price_all_days + prices[i];

           }

           average_price = sum_price_all_days/7;

           cout << "                          " ;
           cout << "                          LOWEST PRICE = " << lowest_price;
           cout << "                          HIGHEST PRICE = " << highest_price;
           cout << "                          AVERAGE PRICE = " << average_price;

           isRetrieved = true;
          
           break;

       }
   }

   if (!isRetrieved)
       cout << " STOCK CODE DOES NOT EXISTS BEFORE ADDING OR RETRIEVING" ;
  
   return isRetrieved;
}


bool StockPortfolio::modifyStock(string code, int day, string price)
{
   bool isModified = false;

   if (day >= 1 && day <= 7)
   {
       for(int i=0;i<10;i++)
       {
           if ((arrStockPortfolio[i][0]).compare(code) == 0)
           {
          
               arrStockPortfolio[i][day] = price;
          
               isModified = true;
          
               break;

           }
       }
   }
   else
   cout << " WRONG DAY" ;

   if (!isModified)
       cout << " NOT MODIFIED. STOCK CODE DOES NOT EXISTS OR WRONG DAY PROVIDED" ;
   else
       cout << " MODIFIED SUCCESSFULLY" ;

   return isModified;
}


bool StockPortfolio::deleteStock(string code)
{
   bool isDeleted = false;

   for(int i=0;i<10;i++)
   {
       if ((arrStockPortfolio[i][0]).compare(code) == 0)
       {
          
           for (int j=0;j<8;j++)
           {
          
               if (j==0)
                   arrStockPortfolio[i][j] = "NULL";
               else
                   arrStockPortfolio[i][j] = "0";
           }

           isDeleted = true;
          
           break;

       }
   }

  
   if (!isDeleted)
       cout << " NOT DELETED. STOCK CODE NOT FOUND" ;
   else
       cout << " DELETED SUCCESSFULLY" ;


   return isDeleted;
}


bool StockPortfolio::displayStockPortfolio(void)
{
   bool isDisplayed = false;
  
   cout << "                       CODE-DAY1-DAY2-DAY3-DAy4-DAY5-DAY6-DAY7 " ;
   cout << "                       -------------------------------------------" ;
   cout << "                          " ;

   for(int i=0;i<10;i++)
   {
       cout << "                          " ;
       if ((arrStockPortfolio[i][0]).compare("NULL") != 0)
       {
           for (int j=0;j<8;j++)
           {
               cout << arrStockPortfolio[i][j] << "-";
           }

           isDisplayed = true;
       }
   }

   if (!isDisplayed)
       cout << " NOT DISPLAYED. STOCK CODE DOES NOT EXISTS" ;

   return isDisplayed;
}

MAIN.CPP

#include "StockPortfolio.h"

void add(StockPortfolio *);
void modify(StockPortfolio *);
void del(StockPortfolio *);
void retrieve(StockPortfolio *);
void display(StockPortfolio *);
string getinput(string);
string getinputStockCode();
string getinputStockPrice(string ask);
bool validateStockCode(string);
bool validateStockPrice(string price);


int main(int argc, char **argv)
{
  
   StockPortfolio *portfolio = new StockPortfolio();
   int mnuoption = 0;
   bool bExit = false;

   std::cout << std::fixed;
    std::cout << std::setprecision(2);
  
   while(bExit == false)
   {
       cout << "            STOCK PORTFOLIO" ;
       cout << "            ---------------" ;
       cout << " " ;
      
       cout << "            1. ADD" ;
       cout << "            2. MODIFY" ;
       cout << "            3. DELETE" ;
       cout << "            4. RETRIEVE" ;
       cout << "            5. DISPLAY" ;
       cout << "            6. EXIT" ;

       cout << " Select menu option:" ;
       cin >> mnuoption;

       switch(mnuoption) {
           case 1 :
               add(portfolio);
               break;
      
           case 2 :
               modify(portfolio);
               break;

           case 3 :
               del(portfolio);
               break;

           case 4 :
               retrieve(portfolio);
               break;

           case 5 :
               display(portfolio);
               break;

           case 6 :
               bExit = true;
               break;

          
       }
      
   }
  
   getinput("type any key");

   return 0;
}

void add(StockPortfolio *portfolio){
  
   string code, prices[7];
  
   code = getinputStockCode();
  
   prices[0] = getinputStockPrice("day 1 price");
   prices[1] = getinputStockPrice("day 2 price");
   prices[2] = getinputStockPrice("day 3 price");
   prices[3] = getinputStockPrice("day 4 price");
   prices[4] = getinputStockPrice("day 5 price");
   prices[5] = getinputStockPrice("day 6 price");
   prices[6] = getinputStockPrice("day 7 price");

   portfolio->addStock(code, prices);


}

void modify(StockPortfolio *portfolio)
{
   string code; string day; string price;
  
   code= getinput("stock code");
   day= getinput("day");
   price= getinputStockPrice("price");

   portfolio->modifyStock(code, atoi(day.c_str()), price);

}
void del(StockPortfolio *portfolio){
  
   string code;
   code= getinput("stock code");
   portfolio->deleteStock(code);

}

void retrieve(StockPortfolio *portfolio){

   string code;
   code= getinput("stock code");
   portfolio->retrieveStock(code);

}

void display(StockPortfolio *portfolio){
   portfolio->displayStockPortfolio();   
}

string getinput(string ask){
  
   string answer;
  
   cout << " " << ask << ": ";
   cin >> answer;

   return answer;
}


string getinputStockCode()
{
   string code;
  
   while(true)
   {
       code= getinput("stock code");
       if (validateStockCode(code))
           break;
   }

   return code;
}

string getinputStockPrice(string ask)
{
   string price;
  
   while(true)
   {
       price= getinput(ask);
       if (validateStockPrice(price))
           break;
   }

   return price;
}

bool validateStockCode(string code)
{
   bool isValid = false;

   int icode = atoi(code.c_str());
   if (icode >=1000 & icode<=4999)
       isValid = true;
   else
       isValid = false;
  
   if (isValid == false)
       cout << " Invalid Stock code. Code can be between 1000 and 4999 only";
      
   return isValid;


}

bool validateStockPrice(string price)
{
   bool isValid = false;

   float icode ;
  
  
       icode = atof(price.c_str());
      
       if (icode > 0)
           isValid = true;
       else
           isValid = false;
  
  
   if (isValid == false)
       cout << " Invalid Stock price. It has to be numeric and non-zero value";
      
   return isValid;


}

OUTPUT ADD and RETRIEVE

D:UsersjdsDocumentsVisual Studio 2010ProjectsStockPortfolioDebug>StockPor
tfolio


           STOCK PORTFOLIO
           ---------------


           1. ADD
           2. MODIFY
           3. DELETE
           4. RETRIEVE
           5. DISPLAY
           6. EXIT


Select menu option:1


stock code: 9999

Invalid Stock code. Code can be between 1000 and 4999 only

stock code: 999

Invalid Stock code. Code can be between 1000 and 4999 only

stock code: 1000


day 1 price: 23


day 2 price: 23


day 3 price: 23


day 4 price: 23


day 5 price: 23


day 6 price: 23


day 7 price: 23

STOCK CODE DOES NOT EXISTS BEFORE ADDING OR RETRIEVING
ADDED SUCCESSFULLY

           STOCK PORTFOLIO
           ---------------


           1. ADD
           2. MODIFY
           3. DELETE
           4. RETRIEVE
           5. DISPLAY
           6. EXIT


Select menu option:4


stock code: 1000


                      CODE-DAY1-DAY2-DAY3-DAy4-DAY5-DAY6-DAY7
                      -------------------------------------------
                         1000-23-23-23-23-23-23-23-

                         LOWEST PRICE = 23.00
                         HIGHEST PRICE = 23.00
                         AVERAGE PRICE = 23.00

           STOCK PORTFOLIO
           ---------------


           1. ADD
           2. MODIFY
           3. DELETE
           4. RETRIEVE
           5. DISPLAY
           6. EXIT


Select menu option:6


type any key: y

D:UsersjdsDocumentsVisual Studio 2010ProjectsStockPortfolioDebug>

OUTPUT ADD MULTIPLE AND THEN DISPLAY


D:UsersjdsDocumentsVisual Studio 2010ProjectsStockPortfolioDebug>StockPor
tfolio


           STOCK PORTFOLIO
           ---------------


           1. ADD
           2. MODIFY
           3. DELETE
           4. RETRIEVE
           5. DISPLAY
           6. EXIT


Select menu option:1


stock code: 9999

Invalid Stock code. Code can be between 1000 and 4999 only

stock code: 999

Invalid Stock code. Code can be between 1000 and 4999 only

stock code: 1000


day 1 price: 23


day 2 price: 23


day 3 price: 23


day 4 price: 23


day 5 price: 23


day 6 price: 23


day 7 price: 23

STOCK CODE DOES NOT EXISTS BEFORE ADDING OR RETRIEVING
ADDED SUCCESSFULLY

           STOCK PORTFOLIO
           ---------------


           1. ADD
           2. MODIFY
           3. DELETE
           4. RETRIEVE
           5. DISPLAY
           6. EXIT


Select menu option:4


stock code: 1000


                      CODE-DAY1-DAY2-DAY3-DAy4-DAY5-DAY6-DAY7
                      -------------------------------------------
                         1000-23-23-23-23-23-23-23-

                         LOWEST PRICE = 23.00
                         HIGHEST PRICE = 23.00
                         AVERAGE PRICE = 23.00

           STOCK PORTFOLIO
           ---------------


           1. ADD
           2. MODIFY
           3. DELETE
           4. RETRIEVE
           5. DISPLAY
           6. EXIT


Select menu option:6


type any key: y

D:UsersjdsDocumentsVisual Studio 2010ProjectsStockPortfolioDebug>


OUTPUT ADD STOCKS BEYOND LIMIT RANGE OF 10 AND DISPLAY


...

stock code: 4323


day 1 price: 34


day 2 price: 34


day 3 price: 34


day 4 price: 34


day 5 price: 34


day 6 price: 34


day 7 price: 34

STOCK CODE DOES NOT EXISTS BEFORE ADDING OR RETRIEVING
NOT ADDED. STOCK CODE LIMIT IS FULL

OUTPUT DISPLAY OPTION
...


           STOCK PORTFOLIO
           ---------------


           1. ADD
           2. MODIFY
           3. DELETE
           4. RETRIEVE
           5. DISPLAY
           6. EXIT


Select menu option:5


                      CODE-DAY1-DAY2-DAY3-DAy4-DAY5-DAY6-DAY7
                      -------------------------------------------

                         1000-12-12-12-1-12-12-12-
                         2000-23-23-23-23-23-23-23-
                         3000-45-45-45-45-45-45-45-
                         4000-4-4-4-4-4-4-4-
                         4545-4-4-4-4-4-4-4-
                         3232-34-45-45-45-54-34-43-
                         3433-343-34-34-34-34-34-34-
                         2323-23-23-23-23-23-23-23-
                         3333-34-34-34-34-34-34-34-
                         4444-45-45-45-45-45-45-45-

OUTPUT: DELETE OPTION
...

                      CODE-DAY1-DAY2-DAY3-DAy4-DAY5-DAY6-DAY7
                      -------------------------------------------

                         1000-12-12-12-1-12-12-12-
                         2000-23-23-23-23-23-23-23-
                         3000-45-45-45-45-45-45-45-
                         4000-4-4-4-4-4-4-4-
                         4545-4-4-4-4-4-4-4-
                         3232-34-45-45-45-54-34-43-
                         3433-343-34-34-34-34-34-34-
                         2323-23-23-23-23-23-23-23-
                         3333-34-34-34-34-34-34-34-
                         4444-45-45-45-45-45-45-45-

           STOCK PORTFOLIO
           ---------------


           1. ADD
           2. MODIFY
           3. DELETE
           4. RETRIEVE
           5. DISPLAY
           6. EXIT


Select menu option:3


stock code: 2323

DELETED SUCCESSFULLY

           STOCK PORTFOLIO
           ---------------


           1. ADD
           2. MODIFY
           3. DELETE
           4. RETRIEVE
           5. DISPLAY
           6. EXIT


Select menu option:5


                      CODE-DAY1-DAY2-DAY3-DAy4-DAY5-DAY6-DAY7
                      -------------------------------------------

                         1000-12-12-12-1-12-12-12-
                         2000-23-23-23-23-23-23-23-
                         3000-45-45-45-45-45-45-45-
                         4000-4-4-4-4-4-4-4-
                         4545-4-4-4-4-4-4-4-
                         3232-34-45-45-45-54-34-43-
                         3433-343-34-34-34-34-34-34-

                         3333-34-34-34-34-34-34-34-
                         4444-45-45-45-45-45-45-45-