Wednesday, January 31, 2018

Programming Challenge 15.8 - PreferredCustomer Class

Example Files: PreferredCustomer.7z

PersonData.h


#ifndef PERSON_DATA_H_
#define PERSON_DATA_H_

#include <string>
using std::string;

class PersonData
{
    private:
        string lastName;
        string firstName;
        string address;
        string city;
        string state;
        string zip;
        string phone;

    public:
        // Constructor
        PersonData()
        {
            lastName = " ";
            firstName = " ";
            address = " ";
            city = " ";
            state = " ";
            zip = " ";
            phone = " ";
        }

        // Parameterized constructor
        PersonData(string lName, string fName, string addr,
                      string cty, string st, string z, string telNum)
        {
            set(lName, fName, addr, cty, st, z, telNum);
        }

        // Mutator function
        // Sets the attribute data
        virtual void set(string lName, string fName, string addr, string cty,
                              string st, string z, string telNum)
        {
            lastName = lName;
            firstName = fName;
            address = addr;
            city = cty;
            state = st;
            zip = z;
            phone = telNum;
        }

        // Accessor functions
        string getLastName() const
        { return lastName; }

        string getFirstName() const
        { return firstName; }

        string getAddress() const
        { return address; }

        string getCity() const
        { return city; }

        string getState() const
        { return state; }

        string getZip() const
        { return zip; }

        string getPhone() const
        { return phone; }
};

#endif

CustomerData.h


#ifndef CUSTOMER_DATA_H_
#define CUSTOMER_DATA_H_

#include "PersonData.h"

// CustomerData class derived from PersonData class
class CustomerData : public PersonData
{
    private:
        int  customerNumber;            // A unique customer number
        bool mailingList;                // Holds the customer's wish to be on the mailing list

    public:
        // Constructor
        CustomerData()
        {
            customerNumber = 0;
            mailingList = false;
        }

        // Parameterized Constructor
        CustomerData(string lName, string fName, string addr, string cty, string st,
                         string zipNum, string telNum, int custID, bool onList) :
        PersonData(lName, fName, addr, cty, st, zipNum, telNum)
        {
            set(custID, onList);
        }

        // Mutator function
        // Sets the attribute data
        void CustomerData::set(int custID, bool onList)
        {
            customerNumber = custID;
            mailingList = onList;
        }

        // Accessor functions
        int getCustomerNumber() const
        { return customerNumber; }

        bool getMailingList() const
        { return mailingList; }
};

#endif

PreferredCustomer.h


#ifndef PREFERRED_CUSTOMER_H_
#define PREFERRED_CUSTOMER_H_

#include "CustomerData.h"

// PreferredCustomer class derived from the CustomerData class
class PreferredCustomer : public CustomerData
{
    private:
        double purchaseAmount;        // The total sum of purchases to date
        double discountLevel;        // The level of discount a customer has earned

    public:
        PreferredCustomer() : CustomerData()
        {
            purchaseAmount = 0.0;
            discountLevel = 0.0;
        }

        PreferredCustomer(string lName, string fName, string addr, string cty,
                                string st, string zipNum, string telNum, int custID,
                                bool onList, double pAmnt) :
        CustomerData(lName, fName, addr, cty, st, zipNum, telNum, custID, onList)
        {
            discountLevel = 0.0;

            setPurchaseAmount(pAmnt);
            setDiscountLevel();
        }

        // Mutator functions
        void setPurchaseAmount(double);        // Defined in PreferredCustomer.cpp
        void setDiscountLevel();               // Defined in PreferredCustomer.cpp

        // Accessor functions
        double getPurchaseAmount() const
        { return purchaseAmount; }

        double getDiscountLevel() const
        { return discountLevel * 100.0; }
};

#endif

PreferredCustomer.cpp


#include "PreferredCustomer.h"

#include <cstdlib>

#include <iostream>
using std::cout;

// Customer Discount Levels
enum DiscountLevels { LVL_ONE = 500, LVL_TWO = 1000, LVL_THREE = 1500, LVL_FOUR = 2000 };

/* **********************************************************
            PreferredCustomer::setPurchaseAmount() : double
    Sets the purchaseAmount attribute data. If the amount
    passed to this function is 0 or negative, the program
    will exit with an error message.
   ********************************************************** */

void PreferredCustomer::setPurchaseAmount(double pAmnt)
{
    if (pAmnt <= 0)
    {
        cout << "Input Error: " << pAmnt
              << " is an invalid purchase amount!\n"
              << "This program will now exit ...\n\n";
        exit(1);
    }
    else
    {
        purchaseAmount = pAmnt;
    }
}

/* **********************************************************
            PreferredCustomer::setDiscountLevel()
    Determines the amount of discount in % a customer is
    entitled to receive, based on the Preferred Customer
    Plan's purchase amounts to date.
   ********************************************************** */

void PreferredCustomer::setDiscountLevel()
{
    if (purchaseAmount < LVL_ONE)
    {
        discountLevel = 0.0;
    }
    else if (purchaseAmount < LVL_TWO)
    {
        discountLevel = 0.05;
    }
    else if (purchaseAmount < LVL_THREE)
    {
        discountLevel = 0.06;
    }
    else if (purchaseAmount < LVL_FOUR)
    {
        discountLevel = 0.07;
    }
    else
    {
        discountLevel = 0.1;
    }
}

PreferredCustomerDm.cpp


#include "PreferredCustomer.h"

#include <array>
using std::array;

#include <iomanip>
using std::setprecision;
using std::setw;

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

void printCustData(const array<PreferredCustomer, 3> &pData);

int main()
{
    // Create an array of 3 CustomerData objects
    array<PreferredCustomer, 3> pData
    {
        PreferredCustomer("Yamashita", "Tatsuro", "1 Chome-10 Haibaraakanedai",
                                "Uda-Shi", "Nara", "633-0256", "08984-1525", 15, false, 190.0),
        PreferredCustomer("Wang", "Kai", "2 Chome-7 Haibaraharumigaoka",
                                "Uda-Shi", "Nara", "633-0255", "08984-3256", 632, true, 587.70),
        PreferredCustomer("Hayakawa", "Keiko", "1 Chome-13-9 Haibaraakanedai",
                                "Uda-Shi", "Nara", "633-0257", "08984-4278", 947, true, 1489.90)
    };

    cout << "SunQ CITY SHOPPING MALL - CUSTOMER DATABASE\n\n";

    printCustData(pData);

    cout << "SunQ CITY SHOPPING MALL - Shopping Happiness!";
   
    cin.get();
    cin.ignore();
    return 0;
}

/* **********************************************************
            void printCustData() : const array &obj
    This function accepts an array of CustomerData objects. It
    outputs the customer information to screen.
   ********************************************************** */

void printCustData(const array<PreferredCustomer, 3> &pData)
{
    for (auto cData : pData)
    {
        cout << "Customer ID:     " << cData.getCustomerNumber() << "\n"
              << "On Mailing List: ";

        cData.getMailingList() == true ? cout << "Yes\n\n" : cout << "No\n\n";

        cout << "Last Name:       " << cData.getLastName() << "\n"
              << "First Name:      " << cData.getFirstName() << "\n"
              << "Address:         " << cData.getAddress() << "\n"
              << "City:            " << cData.getCity() << "\n"
              << "State:           " << cData.getState() << "\n"
              << "ZIP:             " << cData.getZip() << "\n"
              << "Phone Number:    " << cData.getPhone() << "\n\n";

        cout << fixed << setprecision(2);
        cout << "Purchase History:  " << setw(7) << cData.getPurchaseAmount() << " USD\n";
        cout << "Customer Discount: ";

        if (cData.getDiscountLevel() > 0)
        {
            cout << setw(7) << cData.getDiscountLevel() << " %\n";
        }
        else
        {
            cout << "Not a preferred customer\n";
        }
        cout << "\n---------------------------------------------" << "\n\n";
    }
}

Example Output:




No comments:

Post a Comment