Friday , 22 November 2024

CS201 Assignments # 5 Solution Spring 2012

Problem Statement:     Customer Bill Information     

Write a program that takes customer’s information from user i.e. name, ID and spending and displays the same customer information plus total bill on the screen. You will use the same Customer class implemented in Assignment#3. You need to overload stream insertion << and stream extraction >> operators for the Customer class.

Detailed Description:

  • Use the same Customer class you implemented in Assignment#3.
  • This time you don’t need the friend function for calculation of total bill according to spending.
  • You can modify the class (add or delete some functions) as per program requirement.
  • You will overload extraction operator >> for getting input for an object of Customer class.
  • You will also overload insertion operator << for displaying the customer information on screen.
  • On calling extraction operator in main() e.g. cin>>obj where obj is an object of Customer class, information about customer name, ID and spending should be taken from the user.
  • Take a look at the sample output for better understanding.
  • On calling insertion operator in main() e.g. cout<<obj, the output in the following format should be displayed.
  • You must use the formatting manipulators setw() and setfill() for formatting the text as shown in the output.
  • All the formatting of text should be implemented inside overloaded function for extraction operator <<
  • The main() function of your solution should contain only these instructions

Customer obj;

    cin >> obj;

    cout << obj;

    system(“pause”);

 

/****************** Assignment No. 5 : Solution **********************\

using namespace std;

#include<iostream>
#include <conio.h>
#include <iomanip>

// Definition of Customer class
class Customer{

friend ostream& operator << (ostream& ,Customer); // Decarlation of output stream overloading function
friend istream& operator >> (istream& ,Customer&); // Decarlation of output stream overloading function

private:
char name[30];
int custID;
double spending;
double tax;
double discount;

public:
Customer(); //Default constructor
Customer(char[], int, double); //Parameterized constructor

/*Setter and getter functions for the class*/


char* getName();
int getID();
double getSpending();
double getTax();
double getDiscount();
double getBill();
};
// Default constructor Definition
Customer::Customer()
{
strcpy(name, “No Name”);
custID = 0;
spending = tax = discount = 0;
}
char* Customer :: getName() // getName() returns the name of the Customer
{
return name;
}

int Customer :: getID() // Returns ID of the Customer
{
return custID;
}
double Customer :: getSpending() // returns spending by the Customer
{
return spending;
}
double Customer :: getTax() // getTax() returns the tax based on spending by Customer
{
double Tax;

if(spending <= 5000)
{
Tax = spending * 5 / 100;
}

if(spending > 5000 && spending <=10000)
{
Tax = spending * 10 / 100;
}

else
{
Tax = spending * 15 / 100;
}

return Tax;
}

double Customer :: getDiscount() // This function returns total discount on spending
{
double Discount;
if(spending <= 5000)
{
Discount = spending * 1 / 100;
}

if(spending > 5000 && spending <=10000)
{
Discount = spending * 2 / 100;
}

else
{
Discount = spending * 3 / 100;
}

return Discount;
}
double Customer :: getBill() //This function returns the total bill by adding the tax and subtracting the discount amounts
{
return (spending + getTax() – getDiscount());
}
/*Definition of output stream overloading function
Whenever << operator will be used with an object of class Customer, information about Customer’s name,
ID, spending, tax, discount and total bill will be displayed on output*/
ostream& operator << (ostream& output, Customer Cust)
{

output << endl << endl;
output << “**************************Customer Billing Information ************************* ” <<endl;
output << “Name ” << setw(15) <<setfill(‘*’) << ” ” << Cust.name << endl;
output << “ID ” << setw(15) <<setfill(‘*’) << ” ” << Cust.custID << endl;
output << “Spending ” << setw(15) <<setfill(‘*’) << ” ” << Cust.spending << endl;
output << “Tax ” << setw(15) <<setfill(‘*’) << ” ” << Cust.getTax() << endl;
output << “Discount ” << setw(15) <<setfill(‘*’) << ” ” << Cust.getDiscount() << endl;
output << “Total ” << setw(15) <<setfill(‘*’) << ” ” << Cust.getBill() << endl;
return output;
}

 

/*Definition of input stream overloading function
Whenever >> operator will be used with an object of class Customer, information about Customer’s name,
ID and spending will be taken from the user*/
istream& operator >> (istream& input, Customer& Cust)
{
cout << “Enter name of the customer : “;
gets(Cust.name);
cout << “Enter ID of the customer : “;
cin >> Cust.custID;
cout << “Enter total spending by customer : “;
cin >> Cust.spending;
return input;

}

main()
{
Customer obj; //createing an object of Customer class
cin >> obj; // Overloaded input stream operator >> is called here
cout << obj; // Overloaded output stream operator << is called here

getch();
}

 

/******************DOWNLOAD FROM HERE **********************\

 


CS201_Assignments_Solution_5_Spring_2012

Check Also

CS201 Assignment no 02 complete idea solution has been uploaded

CS201 Colpmete Idea Solution Step 1 Copy and paste below given code on Dev C++ …

Leave a Reply

Your email address will not be published. Required fields are marked *

*