#include <iostream>
#include <iomanip>
#include
#include <fstream>
using namespace std;
// global constants
const int MAXENTS=10;
// function prototypes
int menu();
int dispatch(int, string[], double[],double[], int&);
void resetPortfolio(string[], double[],double[],int&);
int readPortfolio(string[], double[],double[]);
int writePortfolio(string[], double[],double[], int);
void buyStock(string[], double[],double[], int&);
void sellStock(string[], double[],double[], int&);
void displayPortfolio(string[], double[],double[], int);
int findStock(string,string[], int);
//-----------------------------------------------------------------------------
// Function main()
// Purpose: Function controls main process of program by calling other functions
// Input: none
// Output: int - 0 - return code
//-----------------------------------------------------------------------------
int main()
{
int actents=0;
int opt=0;
string stockSymbol[MAXENTS];
double stockShares[MAXENTS];
double stockPrice[MAXENTS];
cout << "Welcome to Pat Programmer's Stock Portfolio Manager " << endl;
resetPortfolio(stockSymbol,stockShares,stockPrice,actents); // initialize arrays
actents=readPortfolio(stockSymbol,stockShares,stockPrice); // reads portfolio from disk
if (actents<=0)
{
cout << "Creating new portfolio - press any key to continue" << endl;
std::cin.get();
}
do
{
opt=menu(); // invoke menu function
if (opt==6) // exit code
break;
dispatch(opt,stockSymbol,stockShares,stockPrice,actents);
}while(true);
cout << "Thank you for using the Portfolio Manager." << endl;
system("pause");
return 0;
}
//-----------------------------------------------------------------------------
// Function menu()
// Purpose: Function display menu and reads menu option selected
// Input: none
// Output: int - option selected
//-----------------------------------------------------------------------------
int menu()
{
int opt;
string opts;
// code menu display and prompt for user selection here
return opt;
}
//-----------------------------------------------------------------------------
// Function dispatch()
// Purpose: Function calls processing functions based upon menu option selected
// Input: portfolio arrays - stockSymbol, stockShares, stockPrice, and actents
// Output: int - 0 return code
//-----------------------------------------------------------------------------
int dispatch(int opt, string stockSymbol[],double stockShares[],double stockPrice[],int & actents )
{
// code switch statement to call processing functions
// based upon menu option selected
// 1 - call resetPortfolio(...)
// 2 - call buyStock(...)
// 3 - call sellStock(...)
// 4 - call displayPortfolio(...)
// 5 - call savePortfolio(...)
cout << "Press any key to continue" << endl;
cin.ignore();
cin.get();
return 0;
}
//-----------------------------------------------------------------------------
// Function resetPortfolio
// Purpose: Function resets the portfolio arrays
// Input: portfolio arrays - stockSymbol, stockShares, stockPrice, and actents
// Output: none
//-----------------------------------------------------------------------------
void resetPortfolio(string stockSymbol[], double stockShares[],double stockPrice[],int & actents)
{
// code logic to set all entries of the stock symbol array to ""
// code logic to set all entries of the other arrays to 0
// set the number of actual entries in the arrays (actents) to 0
return;
}
//-----------------------------------------------------------------------------
// Function readPortfolio()
// Purpose: reads the portfolio from disk
// Input: portfolio arrays - stockSymbol, stockShares, stockPrice, and actents
// Output: int - number of entries written to disk
//-----------------------------------------------------------------------------
int readPortfolio(string stockSymbol[], double stockShares[],double stockPrice[])
{
ifstream in;
string symbol;
double shares,price;
in.open("portfolio.txt");
if (!in.good())
return -1;
int i=0;
while (i>symbol >> shares >> price)
{
stockSymbol[i]=symbol;
stockShares[i]=shares;
stockPrice[i]=price;
i++;
}
in.close();
return i;
}
//-----------------------------------------------------------------------------
// Function writePortfolio()
// Purpose: writes the portfolio from the portfolio arrays to disk
// Input: portfolio arrays - stockSymbol, stockShares, stockPrice, and actents
// Output: int - number of entries written to disk
//-----------------------------------------------------------------------------
int writePortfolio(string stockSymbol[], double stockShares[],double stockPrice[],int actents)
{
ofstream out;
string symbol;
out.open("portfolio.txt");
int i=0;
for (i=0; i<actents;i++)
{
out << stockSymbol[i] << " " << stockShares[i] << " " << stockPrice[i] << endl;
}
out.close();
return i;
}
//-----------------------------------------------------------------------------
// Function findStock()
// Purpose: Locate stock in array
// input: stock symbol, stock symbol array, and number of actual entries
// output: position in array or -1 for not found
//-----------------------------------------------------------------------------
int findStock(string symbol, string stockSymbol[], int actents)
{
for (int i=0; i<actents; i++)
if (stockSymbol[i]==symbol)
return i;
return -1;
}
//-----------------------------------------------------------------------------
// Function : buyStock()
// Purpose: processes stock purchases updating the portfolio arrays
// Input: portfolio arrays - stockSymbol, stockShares, stockPrice, and actents
// Output: none
//-----------------------------------------------------------------------------
void buyStock(string stockSymbol[], double stockShares[],double stockPrice[],int & actents)
{
// Code logic to do the following
// 1. Prompt for the stock symbol, number of shares purchased, and the purchase price.
// 2. Call the findStock() function to locate the stock symbol in the stockSymbol[] array
// 3. if found (pos>=0)update the number of shares, and compute the average price
// display purchase message (see output example)
// 4. if not found (pos<0) add a new entry to the array actents
}
//-----------------------------------------------------------------------------
// Function: sellStock()
// Purpose: processes stock sales updating the portfolio arrays
// Input: portfolio arrays - stockSymbol, stockShares, stockPrice, and actents
// Output: none
//-----------------------------------------------------------------------------
void sellStock(string stockSymbol[], double stockShares[],double stockPrice[],int & actents)
{
// Code logic to do the following
// 1. Prompt for the stock symbol, number of shares to sell, and the selling price.
// 2. Call the findStock() function to locate the stock symbol in the stockSymbol[] array
// 3. if found (pos>=0)update the number of shares as long as the number of shares sold
// does not result in a negative balance.
// display sell message (see output example)
// 4. if not found (pos<0) display a message indicating the the user must own the stock.
}
//-----------------------------------------------------------------------------
// Function displayPortfolio()
// Purpose: displays stock portfolio arrays
// Input: portfolio arrays - stockSymbol, stockShares, stockPrice, and actents
// Output: portfolio report, no return value
//-----------------------------------------------------------------------------
void displayPortfolio(string stockSymbol[], double stockShares[],double stockPrice[],int actents)
{
system ("cls");
cout << setprecision(2) << fixed <<showpoint;
cout << "Stock Portfolio Report "<< endl << endl;
cout << "Stock Shares Price Value"<< endl;
// code logic to display portfolio report (see output example)
cout << "\n\nTotal Portfolio value "<< setw(10) << total << endl;
}