C++ Programming Question:
Construct a Customer class that keeps track of a Customer purchases, credit line, and total balance. An object in the Customer class will have a name, an address, a credit limit (set to $1500) as well as other data fields. Declare and define data fields, include a constructor to populate the data fields, and implement member functions such that you can perform the following:
Extract data members if needed.
Keep track of number of sales, and total balance.
Specify if a new purchase would exceed the credit limit. If the cost of an item to be purchased, added to the unpaid balance, surpasses the Customer’s credit limit , do not allow the sale and output “Not enough credit limit.Purchase cannot be completed.” . Otherwise show “Purchase successful.”
Write a program that tests the features of the Customer class. Figure 1 shows a sample output. You can use the following pseudocode as a template:
#include header files using namespace std; class Customer {public:
Customer(parameters);
string get_name() const; double get_credit_limit() const; bool add_purchase(double val); double get_total_balance() const; int get_num_purchases() const;
private: //data field }; // Definition of constructor and member functions
1
int main () { construct Customer object cout<< "Customer: " << Customer name << endl; cout << "Credit Limit: " << Customer credit limit<<endl;
//while loop
purchase value? cin >> val; c.add_purchase(val) ; Purchase another item (y/n) ?
cout << "The total of " << number of purchases << " purchase(s) is $"<<total balance << endl;
return 0; }
Costumer:John Doe Credit Limit: 1500 Purchase value (in $): 100 Purchase successful. Do you want to purchase another item (y/n)? y Purchase value (in $): 452 Purchase successful. Do you want to purchase another item (y/n)? >y Purchase value (in $): 1280 Not enough credit limit. Purchase cannot be completed. Do you want to purchase another item (y/n)? y Purchase value (in $): 923 Purchase successful. Do you want to purchase another item (y/n)?>y Purchase value (in $): 130 Not enough credit limit. Purchase cannot be completed Do you want to purchase another item (y/n)? >y Purchase value (in $): 10 Purchase successful. Do you want to purchase another item (y/n)? n The total of 4 purchase(s) is $1485 Figure 1: Hmw7 sample output
Expert Answer
#include <iostream>
#include<vector>
#include<stdlib.h>
using namespace std;
class Customer
{
private:
string name;
double credit_limit;
int num_purchases;
double total_purchase;
public:
Customer(string name_, double credit_limit_)// constructor
{
name = name_;
credit_limit = credit_limit_;
num_purchases = 0;
}
// Get and Set methods defined here
string get_name()
{
return name;
}
double get_credit_limit()
{
return credit_limit;
}
bool add_purchase(double val)
{
if (credit_limit >= val)
{
credit_limit -= val;
cout << “Purchase successful.n”;
total_purchase += val;
num_purchases++;
return true;
}
cout << “Not enough credit limit.Purchase cannot be completed.n”;
return false;
}
double get_total_balance()
{
return total_purchase;
}
int get_num_purchases()
{
return num_purchases;
}
};
int main ()
{
Customer c(“John Doe”, 1500);
cout<< “Customer: ” << c.get_name() << endl;
cout << “Credit Limit: ” << c.get_credit_limit() <<endl;
while(true)
{
cout << “purchase value(in $): “; // Get purchase value from user
double val;
cin >> val;
c.add_purchase(val);
cout << “Purchase another item (y/n) ? “;
string response;
cin >> response;
if (response == “n”)
break;
}
cout << “The total of ” << c.get_num_purchases() // Show total purchase
<< ” purchase(s) is $”<< c.get_total_balance() << endl;
}
The output of above program is: