Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PreferredCustomer Class A retail store has a preferred customer plan where custo

ID: 3854628 • Letter: P

Question

PreferredCustomer Class A retail store has a preferred customer plan where customers may earn discounts on all their purchases. The amount of a customer's discount is determined by the amount of the customer's cumulative purchases in the store: When a preferred customer spends exist500, he or she gets a 5% discount on all future purchases. When a preferred customer spends exist1,000, he or she gets a 6% discount on all future purchases. When a preferred customer spends exist1, 500, he or she gets a 7% discount on all future purchases. When a preferred customer spends exist2,000 or more, he or she gets a 10% discount on all future purchases. Design a class named PreferredCustomer, which is derived from the CustomerData class you created in the previous problem. The PreferredCustomer class should have the following member variables: purchasesAmount (a double) discountLevel (a double) The purchasesAmount variable holds the total of a customer's purchases to date. The discountLevel variable should be set to the correct discount percentage, according to the store's preferred customer plan. Write appropriate member functions for this class. Create a modified enterNCustomers (int n) function that asks to enter n number customers. All the customer fields must be filled in, and the result of the function is a vector containing n PreferredCustomer objects. You have the same options of text file or user input as the previous problem. Just like the previous problem, create another function that displays all the customer information to the console. Input Validation: Do not accept negative values for any sales figures.

Explanation / Answer

Main.cpp
-------------------------------------------
//Displays menu for testing CustomerData and PreferredCustomer class.

#include "PreferredCustomer.h"

//function prototypes
void demonstrateCustomerData(CustomerData);
void demonstratePreferredCustomer(PreferredCustomer);

int main()
{
   int input;
   while (true) //prompt user to choose what to test
   {
       cout << "What would you like to do?" << endl;
       cout << "1. Test CustomerData Class." << endl;
       cout << "2. Test PreferredCustomer Class." << endl;
       cout << "3. Exit Program." << endl;
       cin >> input;
       if (input == 1) // test CustomerData class
       {
           CustomerData customer;
           demonstrateCustomerData(customer);
       }
       else if (input == 2) //test PreferredCustomer class
       {
           PreferredCustomer customer;
           demonstratePreferredCustomer(customer);
       }
       else if (input == 3) //exit
       {
           return 0;
       }

   }
    return 0;
}

void demonstrateCustomerData(CustomerData customer)
//code to demonstrate CustomerData class by allowing user to
//input data for fields and display them to console
{
   int input;
   while (true) //prompt user to either input fields or view fields
   {
       cout << "1. Input CustomerData Fields." << endl;
       cout << "2. Display CustomerData Fields." << endl;
       cout << "3. Exit to main menu." << endl;
       cin >> input;
       if (input == 1) //input fields
       {
           customer.input();
       }
       else if (input == 2) //view fields
       {
           customer.output();
       }
       else if (input == 3) //exit
       {
           break;
       }
   }
}

void demonstratePreferredCustomer(PreferredCustomer customer)
//code to demonstrate PreferredCustomer class by allowing user to
//input data for fields and display them to console
{
   int input;
   while (true)
   {
       cout << "1. Input PreferredCustomer Fields." << endl;
       cout << "2. Display PreferredCustomer Fields." << endl;
       cout << "3. Exit to main menu." << endl;
       cin >> input;
       if (input == 1) //input fields
       {
           customer.input();
       }
       else if (input == 2) //view fields
       {
           customer.output();
       }
       else if (input == 3) //exit
       {
           break;
       }
   }
}
-----------------------------------------------------------------
CustomerData.cpp
------------------------------
//Definitions for CustomerData class.

#include "CustomerData.h"

//prototype
void clear_input();

CustomerData::CustomerData() : PersonData()
{
   customerNumber = 0;
   mailingList = false;
}
CustomerData::CustomerData(string s1, string s2, string s3, string s4, string s5, string s6, string s7, int n, bool t) : PersonData(s1, s2, s3, s4, s5, s6, s7)
{
   customerNumber = n;
   mailingList = t;
}
void CustomerData::setCustomerNumber(int n)
{
   customerNumber = n;
}
void CustomerData::setMailingList(bool t)
{
   mailingList = t;
}
int CustomerData::getCustomerNumber()
{
   return customerNumber;
}
bool CustomerData::getMailingList()
{
   return mailingList;
}

void CustomerData::input()
{
   PersonData::input();
   cout << "Customer Number: ";
   cin >> customerNumber;
   cout << "Mailing List: " << endl;
   cout << "Enter 0 if customer is not on mailing list." << endl;
   cout << "Enter 1 if customer is on mailing list." << endl;
   int input;
   cin >> input;
   if (input == 0)
   {
       mailingList = false;
   }
   else
   {
       mailingList = true;
   }
}

void CustomerData::output()
{
   PersonData::output();
   cout << "Customer Number: " << customerNumber << endl;
   cout << "Mailing List: ";
   if (mailingList)
   {
       cout << "Yes" << endl;
   }
   else
   {
       cout << "No" << endl;
   }
}
----------------------------------------------------------------------------------
CustomerData.h
--------------------------
#ifndef CUSTOMER_DATA_H
#define CUSTOMER_DATA_H
#include "PersonData.h"

class CustomerData : public PersonData
{
   private:
       int customerNumber;
       bool mailingList;
   public:
       CustomerData();
       CustomerData(string, string, string, string, string, string, string, int, bool);
       void setCustomerNumber(int);
       void setMailingList(bool);
       int getCustomerNumber();
       bool getMailingList();
       void input();
       void output();
};

#endif
--------------------------------------------------------------------------------------
PreferredCustomer.cpp
--------------------------------
#include "PreferredCustomer.h"
#include <cstdlib>

using namespace std;

//prototype
void clear_input();

PreferredCustomer::PreferredCustomer() : CustomerData()
{
   purchasesAmount = 0.0;
   setDiscountLevel();
}
PreferredCustomer::PreferredCustomer(string s1, string s2, string s3, string s4, string s5, string s6, string s7, int n, bool t, double d) : CustomerData(s1, s2, s3, s4, s5, s6, s7, n, t)
{
   purchasesAmount = d;
   setDiscountLevel();
}
void PreferredCustomer::setPurchasesAmount(double d)
{
   if (d >= 0)
   {
       purchasesAmount = d;
       setDiscountLevel();
   }
   else
   {
       cout << "Invalid Purchase Amount! ";
       exit(EXIT_FAILURE);
   }
}
void PreferredCustomer::setDiscountLevel()
{
   if (purchasesAmount < 500)
   {
       discountLevel = 0.0;
   }
   else if (purchasesAmount < 1000)
   {
       discountLevel = 0.05;
   }
   else if (purchasesAmount < 1500)
   {
       discountLevel = 0.06;
   }
   else if (purchasesAmount < 2000)
   {
       discountLevel = 0.07;
   }
   else
   {
       discountLevel = 0.10;
   }
}
double PreferredCustomer::getPurchasesAmount()
{
   return purchasesAmount;
}
double PreferredCustomer::getDiscountLevel()
{
   return discountLevel;
}

void PreferredCustomer::input()
{
   CustomerData::input();
   cout << "Purchases Amount: ";
   cin >> purchasesAmount;
   if (purchasesAmount < 0)
   {
       cout << "Invalid Purchase Amount! ";
       exit(EXIT_FAILURE);
   }
   setDiscountLevel();
}

void PreferredCustomer::output()
{
   CustomerData::output();
   cout << "Purchases Amount: " << purchasesAmount << endl;
   cout << "Discount Level: " << int(100 * discountLevel) << "%" << endl;
}
----------------------------------------------------------------------------------------
PreferredCustomer.h
---------------------------------
#ifndef PREFERRED_CUSTOMER_H
#define PREFERRED_CUSTOMER_H
#include "CustomerData.h"
#include <cstdlib>

class PreferredCustomer : public CustomerData
{
   double purchasesAmount;
   double discountLevel;
   void setDiscountLevel();
public:
   PreferredCustomer();
   PreferredCustomer(string, string, string, string, string, string, string, int, bool, double);
   void setPurchasesAmount(double);
   double getPurchasesAmount();
   double getDiscountLevel();
   void input();
   void output();
};

#endif
--------------------------------------------------------------------------------------------
ClearInput.cpp
-------------------------
#include <string>
#include <iostream>
#include <limits>

using namespace std;


void clear_input() //reset cin flags and clear standard input
{
   cin.clear(); //reset cin flags
   cin.ignore(numeric_limits<streamsize>::max(), ' '); //clear cin
}
-------------------------------------------------------------------------------
PersonData.cpp
-----------------------------
#include "PersonData.h"

using namespace std;

//Function Prototype
void clear_input();

PersonData::PersonData()
   {
       lastName = "";
       firstName = "";
       address = "";
       city = "";
       state = "";
       zip = "";
       phone = "";
   }
PersonData::PersonData(string s1, string s2, string s3, string s4, string s5, string s6, string s7)
   {
       lastName = s1;
       firstName = s2;
       address = s3;
       city = s4;
       state = s5;
       zip = s6;
       phone = s7;
   }

void PersonData::setLastName(string s)
{
   lastName = s;
}
void PersonData::setFirstName(string s)
{
   firstName = s;
}
void PersonData::setAddress(string s)
{
   address = s;
}
void PersonData::setCity(string s)
{
   city = s;
}
void PersonData::setState(string s)
{
   state = s;
}
void PersonData::setZip(string s)
{
   zip = s;
}
void PersonData::setPhone(string s)
{
   phone = s;
}
string PersonData::getLastName()
{
   return lastName;
}
string PersonData::getFirstName()
{
   return firstName;
}
string PersonData::getAddress()
{
   return address;
}
string PersonData::getCity()
{
   return city;
}
string PersonData::getState()
{
   return state;
}
string PersonData::getZip()
{
   return zip;
}
string PersonData::getPhone()
{
   return phone;
}

void PersonData::input()
{
   clear_input();
   cout << "Last Name: ";
   getline(cin, lastName);

   cout << "First Name: ";
   getline(cin, firstName);

   cout << "Address: ";
   getline(cin, address);

   cout << "City: ";
   getline(cin, city);

   cout << "State: ";
   getline(cin, state);

   cout << "Customer ZIP: ";
   getline(cin, zip);

   cout << "Customer Phone: ";
   getline(cin, phone);
}

void PersonData::output()
{
   cout << "Last Name: " << lastName << endl;

   cout << "First Name: " << firstName << endl;

   cout << "Address: " << address << endl;

   cout << "City: " << city << endl;

   cout << "State: " << state << endl   ;

   cout << "Customer ZIP: " << zip << endl;

   cout << "Customer Phone: " << phone << endl;
}
------------------------------------------------------------------------
PersonData.h
---------------------
#ifndef PERSONDATA_H
#define PERSONDATA_H

#include <string>
#include <iostream>

using namespace std;

class PersonData
{
   private:
       string lastName;
       string firstName;
       string address;
       string city;
       string state;
       string zip;
       string phone;
   public:
       PersonData();
       PersonData(string, string, string, string, string, string, string);
       void setLastName(string);
       void setFirstName(string);
       void setAddress(string);
       void setCity(string);
       void setState(string);
       void setZip(string);
       void setPhone(string);
       string getLastName();
       string getFirstName();
       string getAddress();
       string getCity();
       string getState();
       string getZip();
       string getPhone();
       void input();
       void output();
};

#endif
--------------------------------------------------------------------------