Saturday, January 14, 2017

Programming Challenge 6.20 - Stock Profit

/* Stock Profit - The profit from the sale of a stock can be calculated
   as follows:
 
      * Profit = ((NS X SP) - SC) - ((NS X PP) + PC)

   where:
    
      * NS is the number of shares
      * SP is the sale price per share
      * SC is the sale commission paid
      * PP is the purchase price per share
      * PC is the purchase commission paid
 
   If the calculation yields a positive value, then the sale of the
   stock resulted in a profit.
 
   If the calculation yields a negative number, then the sale resulted
   in a loss.

   This program uses the following main function:

      * calcStockProfit()

   Additional functions:

      * displayMenu()
      * displayIntroduction()
      * getStockData()

   The function is demonstrated by asking the user to enter the necessary
   data and displays the amount of profit or loss. */

#include "Utility.h"

/* Prototypes: Display Menu, Introduction, Get stock data,
               Calculate stock profit */
void displayMenu();
void displayIntroduction();
void getStockData();
double calcStockProfit(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\tStock 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\tStock Sales - Profit Or Loss?\n\n"
        << "This program is designed to help in finding out,\n"
        << "whether the sale of a stock with n-Number of shares\n"
        << "would result in a profit or loss.\n\n"
        << "To determine this, the program uses a custom function\n"
        << "to calculate the profit or loss made.\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 shares
   * The sale price per sahre
   * 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()
{
   /* Variable: Number of shares */
   int numShares = 0;

   /* Variables: Sale price per share, Sale commission paid, Purchase
   price per share, purchase commission paid */
   double purchaseCommissionPaid = 0.0,
               salePricePerShare = 0.0,
               saleCommissionPaid = 0.0,
               purchasePricePerShare = 0.0,
               profitOrLoss = 0.0;
   /* Get: Values from the user
      Validate: Input */
   cout << "\nEnter the number of shares: ";
   cin >> 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(numShares, salePricePerShare, saleCommissionPaid,
                                  purchasePricePerShare, purchaseCommissionPaid);

   /* Set up: Output numeric formatting */
   cout << fixed << showpoint << setprecision(2);

   /* Determine and display the profit or loss, for bigger numbers,
      profitOrLoss is promoted from double to long double */
   if (profitOrLoss > 0)
   {
      cout << "\nYou made a profit from the sale of your stock of: $ "
         << (long double) profitOrLoss << "\n\n";
   }
   else
   {
      cout << "\nYou made a loss from the sale of your stock of: $ "
         << (long double) profitOrLoss << "\n\n";
   }
}

/* **********************************************************
   Definition: calcStockProfit

   This function calculates the profit or loss for the sale
   of a stock. It accepts the following arguments:

      * The number of shares
      * The purchase commission paid
      * The sale price per sahre
      * The sale commission paid
    
   The function returns the profit (or loss) from the sale of
   stock.
   ********************************************************** */

double calcStockProfit(int numberShares, double salesPrice,
                       double commissionPaid, double purchasePrice,
                       double purchaseCommission)
{
   /* Variable: Profit or Loss */
   double profitOrLoss = 0.0;

   /* Calculate: Profit or Loss */
   profitOrLoss = ((numberShares * salesPrice) - commissionPaid) -
                  ((numberShares * purchasePrice) + purchaseCommission);

   /* Return: Profit or loss */
   return profitOrLoss;
}

Example Output





No comments:

Post a Comment