/* Multiple Stock Sales - This program uses the function written for
Programming Challenge 6.20 (Stock Profit) to calculate the total
profit or loss from the sale of multiple stocks. The program asks
the user for the number of stock sales and the necessary data for
each stock sale. It accumulates the profit or loss for each stock
sale and displays the total profit or loss gained from selling the
stock.
This program uses the following functions:
* calcStockProfit()
And the following additional functions:
* displayMenu()
* displayIntroduction()
* getStockData() */
#include "Utility.h"
/* Prototypes: Display Menu, Display Introduction, Get stock data,
Calculate stock profit */
void displayMenu();
void displayIntroduction();
void getStockData();
double calcStockProfit(int, int, double, double, double, double);
int main()
{
/* Call: displayMenu */
displayMenu();
pauseSystem();
return 0;
}
/* **********************************************************
Define: displayMenu
This function displays a menu that allows the user to
choose from the following:
* Display Introduction
* Begin / Again
* Quit
It validates menu selection and calls the appropriate
function, depending on the choice the user made.
********************************************************** */
void displayMenu()
{
/* Constants: Introduction, Begin or Again, Quit */
const int INTRODUCTION = 1,
BEGIN_AGAIN = 2,
QUIT = 3;
/* Variable: Menu choice */
int menuChoice = 0;
/* Display: The menu
Get: Menu selection */
do
{
cout << "\t\tMultiple Stock Sales - Profit or Loss\n\n"
<< "1. Introduction\n"
<< "2. Begin/Again\n"
<< "3. Quit\n\n"
<< "Your menu choice: ";
cin >> menuChoice;
/* Validate: Menu choice
Call: displayIntroduction, getStockData, catchInifiniteLoop */
while (menuChoice < 1 || menuChoice > 3)
{
cout << "\nYour menu selection was invalid. Please select:\n\n"
<< "1. To display an Introduction\n"
<< "2. To Begin or try again\n"
<< "3. If you wish to quit.\n\n"
<< "Your menu choice: ";
cin >> menuChoice;
/* Call: catchInfiniteLoop */
catchInfiniteLoop();
}
if (menuChoice != QUIT)
{
switch (menuChoice)
{
case 1:
displayIntroduction();
break;
case 2:
getStockData();
break;
}
}
if (menuChoice == 3)
{
cout << "\nThank you for trying this demo-program.\n"
<< "(Please press Enter to Exit)";
pauseSystem();
}
} while (menuChoice != QUIT);
}
/* **********************************************************
Definition: displayIntroduction
This function displays an introduction and explains the
purpose and function of this program to the user.
********************************************************** */
void displayIntroduction()
{
cout << "\n\t\tMultiple Stock Sales - Profit Or Loss?\n\n"
<< "This program is designed to help in finding out,\n"
<< "whether the sale of multiple stocks with n-Number\n"
<< "of shares would result in a profit or loss.\n\n"
<< "To determine this, the program uses a custom function\n"
<< "that calculates the profit or loss, accumulates the result\n"
<< "for each stock, and displays the result once finished.\n\n"
<< "The following formula is used:\n\n"
<< " * Profit = ((NS X SP) - SC) - ((NS X PP) + PC)\n\n"
<< "where: \n\n"
<< " * NS is the number of shares\n"
<< " * SP is the sale price per share\n"
<< " * SC is the sale commission paid\n"
<< " * PP is the purchase price per share\n"
<< " * PC is the purchase commission paid\n\n";
}
/* **********************************************************
Definiton: getStockData
This function asks the following information:
* The number of stocks to sell
* The number of shares
* The sale price per share
* The sale commission paid
* The purchase price per share
* The purchase commission paid
It validates the input, passes the values as arguments
to calcStockProfit, gets and displays the result of the
calculation.
********************************************************** */
void getStockData()
{
/* Variables: Number of stocks, Number of shares, Number of
shares total, stockCounter (counter variable) */
int numStocks = 0,
numShares = 0,
numSharesTotal = 0,
stockCounter = 0;
/* Variables: Sale price per share, Sale commission paid, Purchase
price per share, purchase commission paid, Profit or
loss */
long double salePricePerShare = 0.0,
saleCommissionPaid = 0.0,
purchaseCommissionPaid = 0.0,
purchasePricePerShare = 0.0,
profitOrLoss = 0.0;
/* Get: Values from the user
Validate: Input */
cout << "\nHow many stocks do you wish to sell? ";
cin >> numStocks;
while (numStocks <= 0)
{
cout << "\nInvalid Input: The number of Stocks to sell must be "
<< "greater than 0.\n"
<< "Enter the number of stocks: ";
cin >> numStocks;
catchInfiniteLoop();
}
/* While numStocks is greater than 0 the user is asked to enter
data per stock he or she wishes to sell */
for (stockCounter = 1; stockCounter <= numStocks; ++stockCounter)
{
cout << "\nPlease enter data for stock N°" << stockCounter
<< "\n................................\n";
cout << "\nEnter the number of shares: ";
cin >> numShares;
/* To display the total of shares sold, the number of shares is
accumulated by numSharesTotal */
numSharesTotal += numShares;
while (numShares <= 0)
{
cout << "\nInvalid Input: The number of Shares must be greater "
<< "than 0.\n"
<< "Enter the number of shares: ";
cin >> numShares;
catchInfiniteLoop();
}
cout << "\nSale price per share: $ ";
cin >> salePricePerShare;
while (salePricePerShare <= 0)
{
cout << "\nInvalid Input: You must enter a positive value for "
<< "the sales price.\n"
<< "Sale price per share: $ ";
cin >> salePricePerShare;
catchInfiniteLoop();
}
cout << "\nSale commission paid: $ ";
cin >> saleCommissionPaid;
while (saleCommissionPaid <= 0)
{
cout << "\nInvalid Input: The sale commission paid must have a "
<< "value greater 0.\n"
<< "Sale commission paid: $ ";
cin >> saleCommissionPaid;
catchInfiniteLoop();
}
cout << "\nPurchase price per share: $ ";
cin >> purchasePricePerShare;
while (purchasePricePerShare <= 0)
{
cout << "\nInvalid Input: The sale commission paid must have a "
<< "value greater 0.\n"
<< "Purchase price per share: $ ";
cin >> purchasePricePerShare;
catchInfiniteLoop();
}
cout << "\nPurchase commission paid: $ ";
cin >> purchaseCommissionPaid;
while (purchaseCommissionPaid <= 0)
{
cout << "\nInvalid Input: The purchase commission paid must have "
<< "a value greater 0.\n"
<< "Purchase commission paid: $ ";
cin >> purchaseCommissionPaid;
catchInfiniteLoop();
}
/* Call: calcStockProfit */
profitOrLoss = calcStockProfit(numStocks, numShares, salePricePerShare,
saleCommissionPaid, purchasePricePerShare,
purchaseCommissionPaid);
}
/* Set up: Output numeric formatting */
cout << fixed << showpoint << setprecision(2);
/* Determine whether the sales of stocks led to a profit or loss, and
display the result */
if (profitOrLoss > 0)
{
cout << "\nBy selling " << numStocks
<< " stocks with " << numSharesTotal
<< " shares you made a total profit of: $ "
<< profitOrLoss << "\n\n";
}
else
{
cout << "\nBy selling " << numStocks
<< " stocks with " << numSharesTotal
<< " shares you made a total loss of: $ "
<< profitOrLoss << "\n\n";
}
}
/* **********************************************************
Definition: calcStockProfit
This function calculates the profit or loss for the sale
of multiple stocks. It accepts the following arguments:
* The number of shares
* The purchase commission paid
* The sale price per sahre
* The sale commission paid
The function calculates the profit (or loss) of each stock
accumulates the results, and returns the profit (or loss)
from the sale of all stocks total.
********************************************************** */
double calcStockProfit(int numStocks, int numberShares, double salesPrice,
double commissionPaid, double purchasePrice,
double purchaseCommission)
{
/* Variables: Profit or loss, Total profit or loss */
static double profitOrLoss = 0.0,
totalProfitOrLoss = 0.0;
/* As long as stockCount is lower than numStocks, the sales profits or
losses per individual stock are calculated and the result passed to
totalProfitOrLoss */
for (int stockCount = 1; stockCount <= numStocks; ++stockCount)
{
/* Calculate: Profit or Loss */
profitOrLoss = ((numberShares * salesPrice) - commissionPaid) -
((numberShares * purchasePrice) + purchaseCommission);
}
/* Accumulate the total profit or loss from the sales of multiple stocks */
totalProfitOrLoss += profitOrLoss;
/* Return: totalProfitOrLoss */
return totalProfitOrLoss;
}
Saturday, January 14, 2017
Programming Challenge 6.21 - Multiple Stock Sales
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment