preferred customer discount for retail store using C ++
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 $500, he or she gets a 5% discount on all
future purchases.
Review Questions and Exercises
• When a preferred customer spends $1,000, he or she gets a 6% discount on an
future purchases.
• When a preferred customer spends $1,500, he or she gets a 7% discount on all
future purchases.
• When a preferred customer spends $2,000 or more, he or she gets a 10% discount
on all future purchases.
Design a class named PreferredCustomer. 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
and demonstrate it in a simple program
Answer:
PreferredCustomer Class
#include <iostream> #include <iomanip> #include "PreferredCustomer.h" using namespace std; int main() { // Create a PreferredCustomer object. PreferredCustomer customer; // Variable to hold the amount of purchases. double purchase; // Set the customer information in the // PreferredCustomer object. customer.setLastName("Smith"); customer.setFirstName("Herman"); customer.setAddress("487 Main Street"); customer.setCity("Mayberry"); customer.setState("NC"); customer.setZip("55555"); customer.setPhone("555-5555"); customer.setCustomerNumber(147); customer.setMailingList(true); customer.setPurchasesAmount(550.0); // Display the basic customer information. cout << fixed << showpoint << setprecision(2); cout << "Initial Customer Data\n"; cout << "---------------------\n"; cout << "Last name: " << customer.getLastName() << endl; cout << "First name: " << customer.getFirstName() << endl; cout << "Address: " << customer.getAddress() << endl; cout << "City: " << customer.getCity() << endl; cout << "State: " << customer.getState() << endl; cout << "ZIP: " << customer.getZip() << endl; cout << "Phone: " << customer.getPhone() << endl; cout << "Customer number: " << customer.getCustomerNumber() << endl; // Display the mailing list status. if (customer.getMailingList()) cout << "Mailing list: Yes\n"; else cout << "Mailing list: No\n"; // Display the amount of purchases. cout << "Purchases amount: $" << customer.getPurchasesAmount() << endl; // Display the discount level. cout << "Current discount level: " << customer.getDiscountLevel() << endl << endl; // Add another purchase. cout << "Enter an additional purchase amount: "; cin >> purchase; customer.addPurchase(purchase); // Display the amount of purchases now. cout << "Purchases amount: $" << customer.getPurchasesAmount() << endl; // Display the discount level now. cout << "Current discount level: " << customer.getDiscountLevel() << endl << endl; return 0; }
Specification file for the PreferredCustomer class
#ifndef PREFERREDCUSTOMER_H #define PREFERREDCUSTOMER_H #include "CustomerData.H" class PreferredCustomer : public CustomerData { private: double purchasesAmount; // Holds the amount of a customer's purchases double discountLevel; // Holds the customer's discount level void determineDiscountLevel(); // Determines the discount level public: // Default Constructor PreferredCustomer() : CustomerData() { purchasesAmount = 0; discountLevel = 0; } // Constructor PreferredCustomer(string ln, string fn, string addr, string c, string s, string z, string ph, int cn, bool ml, double pa, double dl) : CustomerData(ln, fn, addr, c, s, z, ph, cn, ml) { purchasesAmount = pa; discountLevel = dl; } // Mutator void setPurchasesAmount(double p) { purchasesAmount = p; determineDiscountLevel(); } // Get functions double getPurchasesAmount() { return purchasesAmount; } double getDiscountLevel() { determineDiscountLevel(); return discountLevel; } // Function to add an amount to the purchases amount void addPurchase(double p) { purchasesAmount += p; } }; #endif
Implementation file for the PreferredCustomer class
#include "PreferredCustomer.h" //****************************************** // determineDiscountLevel member function * //****************************************** void PreferredCustomer::determineDiscountLevel() { if (purchasesAmount >= 2000) discountLevel = 0.10; else if (purchasesAmount >= 1500) discountLevel = 0.07; else if (purchasesAmount >= 1000) discountLevel = 0.06; else if (purchasesAmount >= 500) discountLevel = 0.05; }
PersonData class
#ifndef PERSONDATA_H #define PERSONDATA_H #include <string> using namespace std; class PersonData { private: string lastName; // The person's last name string firstName; // The person's first name string address; // The person's address string city; // The person's city string state; // The person's state string zip; // The person's ZIP code string phone; // The person's phone number public: //********************************************** // Constructor * //********************************************** PersonData(string ln, string fn, string addr, string c, string s, string z, string ph) { lastName = ln; firstName = fn; address = addr; city = c; state = s; zip = z; phone = ph; } //********************************************** // Default Constructor * //********************************************** PersonData() { lastName = ""; firstName = ""; address = ""; city = ""; state = ""; zip = ""; phone = ""; } //********************************************** // Mutator functions * //********************************************** void setLastName(string ln) { lastName = ln; } void setFirstName(string fn) { firstName = fn; } void setAddress(string addr) { address = addr; } void setCity(string c) { city = c; } void setState(string s) { state = s; } void setZip(string z) { zip = z; } void setPhone(string ph) { phone = ph; } //********************************************** // 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 class
#ifndef CUSTOMERDATA_H #define CUSTOMERDATA_H #include <string> #include "PersonData.h" using namespace std; class CustomerData : public PersonData { private: int customerNumber; // Customer number bool mailingList; // Include in mailing list? public: //********************************************** // Constructor * //********************************************** CustomerData(string ln, string fn, string addr, string c, string s, string z, string ph, int cn, bool ml) : PersonData(ln, fn, addr, c, s, z, ph) { customerNumber = cn; mailingList = ml; } //********************************************** // Default Constructor * //********************************************** CustomerData() : PersonData() { customerNumber = 0; mailingList = false; } //********************************************** // Mutator functions * //********************************************** void setCustomerNumber(int cn) { customerNumber = cn; } void setMailingList(bool ml) { mailingList = ml; } //********************************************** // Accessor functions * //********************************************** int getCustomerNumber() { return customerNumber; } bool getMailingList() { return mailingList; } }; #endif
Leave a reply