// Renee V. Lopez CS1A Chapter 3, P. 147, #21
/*******************************************************************************
* Calculate Net Profit from Stock Transaction
* _____________________________________________________________________________
* This program calculates the net profit from a stock transaction by considering
* the number of shares bought and sold, the price of the stock at both times,
* and the commission fees.
* _____________________________________________________________________________
* INPUT
* sharesBought - the number of shares bought
* stockBuyPrice - the price per share at purchase
* COM_RATE - the commission rate
* sharesSold - the number of shares sold
* stockSellPrice - the price per share at sale
*
* OUTPUT
* buyTotal - the total amount paid for buying shares
* commissionBuy - the commission paid when buying shares
* sellTotal - the total amount received from selling shares
* commissionSell - the commission paid when selling shares
* netProfit - the final net profit of the transaction (negative if a loss)
******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// INPUT - Variable declarations
float sharesBought; // the number of shares bought
float stockBuyPrice; // the price per share at purchase
const float COM_RATE = 0.02; // the commission rate
float buyTotal; // the total amount paid for buying shares
float commissionBuy; // the commission fee when buying shares
float sharesSold; // the number of shares sold
float stockSellPrice; // the price per share at sale
float sellTotal; // the total amount received from selling shares
float commissionSell; // the commission fee when selling shares
// OUTPUT - Variable declaration
float netProfit; // final net profit of the transaction
// Calculate the commission fee and total cost for buying shares
sharesBought = 1000;
stockBuyPrice = 32.87;
buyTotal = sharesBought * stockBuyPrice;
commissionBuy = buyTotal * COM_RATE;
// Calculate the commission fee and total amount received when selling shares
sharesSold = 1000;
stockSellPrice = 33.92;
sellTotal = sharesSold * stockSellPrice;
commissionSell = sellTotal * COM_RATE;
// Calculate the net profit of the transaction
netProfit = sellTotal - buyTotal - (commissionBuy + commissionSell);
// Output results to console
cout << "Joe bought " << sharesBought << " shares for $" << fixed << setprecision(2)
<< buyTotal << " with a commission fee of $" << commissionBuy << ".\n";
cout << "Joe sold " << sharesSold << " shares for $" << fixed << setprecision(2)
<< sellTotal << " with a commission fee of $" << commissionSell << ".\n";
cout << "Joe made a net profit of $" << fixed << setprecision(2) << netProfit << ".";
return 0;
}