Tuesday, May 23, 2017

Programming Challenge 11.2 - Movie Profit

/* Movie Profit - This is a modification of the Movie Data program written
    for Programming Challenge 11.01. This program uses a structure named
    MovieData to store the following information about a movie:
   
        * Title
        * Director
        * Year Released
        * Running Time (in minutes)
        * Production cost
        * First year's profit or loss

    The function that displays the movie data is modified so that it displays
    the title, director, release year, running time, and first year's profit
    or loss. */

#include "Utility.h"

struct MovieData
{
    string title;              /* The movie's title                            */
    string director;          /* The movie director's name                 */
    int     releaseYear;      /* The year the movie was released         */
    int     runningTime;      /* The movie length in minutes                 */
    double productionCost; /* The movie's production cost                */
    double revenue;          /* The movie's first year profit or loss */   
};

void   getMovieData(MovieData &);
double getProfit(const MovieData &);
void   repeatInput(MovieData &, double);
string formatOutput(const double);
void   showMovieData(const MovieData &, const double);

int main()
{
    /* Structure variable definitions */
    MovieData movieOne;
    MovieData movieTwo;

    double profitM1 = 0.0;        /* Holds the profit or loss of movie one */
    double profitM2 = 0.0;        /* Holds the profit or loss of movie two */

    cout << "\n\tFavorite Movie Database\n\n";
    getMovieData(movieOne);
    profitM1 = getProfit(movieOne);
    repeatInput(movieOne, profitM1);

    getMovieData(movieTwo);
    profitM2 = getProfit(movieTwo);
    repeatInput(movieTwo, profitM2);

    cout << "\n\tThese movies are now stored in your database:\n";
    showMovieData(movieOne, profitM1);
    showMovieData(movieTwo, profitM2);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: repeatInput

    This function uses a structure reference variable as its
    argument. It asks the user to confirm his or her input. If
    the displayed information is incorrect, the user is asked
    to repeat the input. If the user finds that the input is
    correct, a message is displayed, telling the user that the
    movie info has been added.
   ********************************************************** */

void repeatInput(MovieData &movieInfo, double profit)
{
    char again = ' ';        /* Holds the user's choice */

    do
    {
        showMovieData(movieInfo, profit);

        cout << "\tIs the data you entered correct? (Y/N): ";
        cin >> again;
        cin.ignore();

        if (toupper(again) == 'Y')
        {
            cout << "\n\tThe following movie info has been added:\n";

            showMovieData(movieInfo, profit);
        }
        else
        {
            cout << "\n\n\tPlease repeat your input:\n";

            getMovieData(movieInfo);
            profit = getProfit(movieInfo);
        }
    } while (toupper(again) == 'N' || toupper(again) != 'Y');
}

/* **********************************************************
   Definition: getMovieData

    This function uses a structure reference variable as its
    argument. It asks the user for data about his or her two
    favorite movies. This data is stored in the structure.
   ********************************************************** */

void getMovieData(MovieData &movieInfo)
{
        cout << "\n\tEnter the name of your favorite movie:  ";
        getline(cin, movieInfo.title);

        cout << "\tEnter the name of the movie's director: ";
        getline(cin, movieInfo.director);

        cout << "\tEnter the year the movie was released:  ";
        cin >> movieInfo.releaseYear;

        cout << "\tEnter the running time (in minutes):    ";
        cin >> movieInfo.runningTime;

        cout << "\tEnter the movie's production cost:     $";
        cin >> movieInfo.productionCost;

        cout << "\tEnter the movie's first-year revenue:  $";
        cin >> movieInfo.revenue;

        cin.ignore();
}

/* **********************************************************
   Definition: getProfit

    This function accepts a constant reference to a structure
    variable as argument. It calculates the profit or loss of
    the movies. The result of this calculation is returned.
   ********************************************************** */

double getProfit(const MovieData &movieInfo)
{
    double total = 0.0;        /* Holds the total profit or loss */

    return total = movieInfo.revenue - movieInfo.productionCost;
}

/* **********************************************************
   Definition: showMovieData

    This function accepts a constant reference to a structure
    variable as argument. It displays the movie data.
   ********************************************************** */

void showMovieData(const MovieData &mData, const double profit)
{
    string revenue;    /* Holds the formatted profit */

    cout << "\n\tMovie Title:        " << mData.title           << "\n"
          << "\tDirector:         "       << mData.director       << "\n"
          << "\tRelease Year:        " << mData.releaseYear << "\n"
          << "\tRunning Time:        " << mData.runningTime << " minutes\n";

    revenue = formatOutput(profit);

    profit > 0 ? cout << "\n\tFirst-Year Profit: $" << revenue << "\n\n" :
                     cout << "\n\tFirst-Year Loss:   $" << revenue << "\n\n";
}

/* **********************************************************
   Definition: showMovieData

    This function formats the profit to make it more legible.
    To achieve this, commata are inserted in the string object
    holding the profit. Amounts between one-thousand, up to
    nine-billion, are considered. Example:
   
        *   1985456    becomes  1,985,456
        * -92482948 becomes 92,482,948

    If the string object contains a negative amount, the minus
    sign is erased, before formatting takes place. The string
    object is returned.
   ********************************************************** */

string formatOutput(const double profit)
{
    string revenue = to_string((int)profit);

    if (revenue.at(0) == '-')
    {
        revenue.erase(0,1);
    }

    /* This chained ternary operator determines the length of the
        string object's contents. Based on the result, commata are
        inserted at the appropriate substring position(s). */
    revenue.length() == 4  ? revenue.insert(1, ",")                                    :
    revenue.length() == 5  ? revenue.insert(2, ",")                                    :
    revenue.length() == 6  ? revenue.insert(3, ",")                                    :
    revenue.length() == 7  ? revenue.insert(1, ","), revenue.insert(5, ",") :
    revenue.length() == 8  ? revenue.insert(2, ","), revenue.insert(6, ",") :
    revenue.length() == 9  ? revenue.insert(3, ","), revenue.insert(7, ",") :
    revenue.length() == 10 ? revenue.insert(1, ","), revenue.insert(5, ","),
                                     revenue.insert(9, ",")    : revenue;

    return revenue;
}

Example Output:





No comments:

Post a Comment