Friday, January 5, 2018

Programming Challenge 14.13 - Carpet Calculator

Example Files: CarpetCalculator.7z

Note: Since the only addition to the FeetInches.h and FeetInches.cpp files written for Programming Challenge 14.11 is a conversion function that converts a FeetInches object to a double, there is no listing published for both of these files here.

RoomDimension.h


#ifndef ROOM_DIMENSION_H_
#define ROOM_DIMENSION_H_

#include "FeetInches.h"

class RoomDimension
{
    private:
        FeetInches width;
        FeetInches length;
   
    public:
        RoomDimension() {}

        RoomDimension(FeetInches w, FeetInches l) : width(w), length(l)
        {
        }

        FeetInches calcArea()
        {
            return width.multiply(length);
        }
};

#endif

RoomCarpet.h


#ifndef ROOM_CARPET_H_
#define ROOM_CARPET_H_

#include "RoomDimension.h"

class RoomCarpet
{
    private:
        RoomDimension dimension;

        double sqFtPrice;                // The cost per square footage of carpeting
        double totalCost;                // The total cost of a carpet

    public:
        RoomCarpet(RoomDimension dim, double price = 0.0) : dimension(dim), sqFtPrice(price)
        {
            calcTotalCost();
        }

        void calcTotalCost()
        {   
            totalCost = (dimension.calcArea()) * sqFtPrice;
        }
       
        double getTotalCost() const
        { return totalCost; }
};

#endif

CarpetCalculatorDm.cpp


#include "RoomCarpet.h"

#include <iomanip>
using std::setprecision;

#include <iostream>
using std::fixed;
using std::cin;
using std::cout;

int main()
{
    double price = 0.0;
    char choice = ' ';

    FeetInches width, length;

    cout << "WESTFIELD CARPET COMPANY - CARPET CALCULATOR\n\n";

    do
    {
        cout << "Please enter the width of your room:\n";
        cin >> width;
        cout << "\nPlease enter the length of your room:\n";
        cin >> length;

        RoomDimension dim(width, length);

        cout << "\nPlease enter the price per square yard: $ ";
        cin >> price;

        RoomCarpet purchase(dim, price);

        cout << setprecision(2) << fixed;
        cout << "\nThe total price of your "
              << width << " by " << length << " carpet is $ "
              << purchase.getTotalCost() << "\n\n";

        cout << "Do you wish to calculate the price for another room? ";
        cin >> choice;

        while (toupper(choice) != 'Y' && toupper(choice) != 'N')
        {
            cout << "Do you wish to calculate the price for another room? ";
            cin >> choice;
        }

        if (toupper(choice) == 'N')
        {
            cout << "\nThe Westfield Carpet Company thanks you for your patronage.";
        }
        cout << "\n";

    } while (toupper(choice) != 'N');

    cin.get();
    cin.ignore();
    return  0;
}

Example Output:



No comments:

Post a Comment