CS201 Assignment No 5 Spring 2012 solution

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.

Solution:

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class Customer
{
private:
string Name;
int CustomerId;
int Spending;
int tax;
int discount;
int totalbill;
public:
void setName(string);
int setCustomerId(int);
int setSpending(int);
int getName(string);
int getCustomerId(int);
int getSpending(int);
void display()
{
cout<<“Customer Name : “<<Name<<endl<<endl<<“ID : “<<CustomerId<<endl<<endl<<“Spending : “<<Spending<<endl<<endl;
}
Customer();
Customer(string, int, int);
friend ostream & operator<<(ostream &, Customer&);
friend istream & operator>>(istream &, Customer&);
friend ostream &star(ostream &);
};
ostream & star(ostream & output)
{
output<<“*”;
return output;
}
Customer::Customer()
{
Name=”No Name”;
CustomerId=0;
Spending=0;
}
Customer::Customer(string s, int i, int j)
{
Name=s;
CustomerId=i;
Spending=j;
}
istream & operator>>(istream & input, Customer &c)
{
cout<<“Enter the name of customer : “;
input>>c.Name;
cout<<“Enter the customer-id : “;
input>>c.CustomerId;
cout<<“Enter the Spending : “;
input>>c.Spending;
return input;
}
ostream & operator<<(ostream & output, Customer &c)
{
for(int i=0;i<55;i++)
{
if(i==25)
{
output<<“Customer Bill Information”;
}
else
{
output<<star;
}
}
output<<endl;
output<<“Name :”;
for(int a=0;a<21;a++)
{
if(a==20)
{
output<<c.Name<<endl;
}

else
{
output<<star;
}
}
output<<“ID :”;
for(int a=0;a<23;a++)
{
if(a==22)
{
output<<c.CustomerId<<endl;
}

else
{
output<<star;
}
}
output<<“Spending :”;
for(int a=0;a<19;a++)
{
if(a==18)
{
output<<c.Spending<<endl;
}

else
{
output<<star;
}
}

if(c.Spending<=5000)
{
c.tax=(c.Spending*5)/100;
c.discount=(c.Spending*1)/100;
c.totalbill=c.Spending+c.tax-c.discount;
output<<“Tax :”;
for(int a=0;a<27;a++)
{
if(a==26)
{
output<<c.tax<<endl<<“Discount :”;
for(int a=0;a<19;a++)
{
if(a==18)
{
output<<c.discount<<endl<<“Total Bill :”;
for(int a=0;a<17;a++)
{
if(a==16)
{
output<<c.totalbill<<endl;
}

else
{
output<<star;
}
}

}

else
{
output<<star;
}
}

}

else
{
output<<star;
}
}

}
else if(c.Spending>5000 & c.Spending<10000)
{
c.tax=(c.Spending*10)/100;
c.discount=(c.Spending*2)/100;
c.totalbill=c.Spending+c.tax-c.discount;
output<<“Tax :”;
for(int a=0;a<27;a++)
{
if(a==26)
{
output<<c.tax<<endl<<“Discount :”;
for(int a=0;a<19;a++)
{
if(a==18)
{
output<<c.discount<<endl<<“Total Bill :”;
for(int a=0;a<17;a++)
{
if(a==16)
{
output<<c.totalbill<<endl;
}

else
{
output<<star;
}
}

}

else
{
output<<star;
}
}

}

else
{
output<<star;
}
}

}

else if(c.Spending>10000)
{
c.tax=(c.Spending*15)/100;
c.discount=(c.Spending*3)/100;
c.totalbill=c.Spending+c.tax-c.discount;
output<<“Tax :”;
for(int a=0;a<27;a++)
{
if(a==26)
{
output<<c.tax<<endl<<“Discount :”;
for(int a=0;a<19;a++)
{
if(a==18)
{
output<<c.discount<<endl<<“Total Bill :”;
for(int a=0;a<17;a++)
{
if(a==16)
{
output<<c.totalbill<<endl;
}

else
{
output<<star;
}
}

}

else
{
output<<star;
}
}

}

else
{
output<<star;
}
}

}
return output;
}

main()
{
Customer obj;
cin>>obj;
cout<<obj;
getch();
}

DOWNLOAD SOLUTION HERE
loading...