Monday , 25 November 2024

CS201 Assignments # 3 Solution Spring 2012

Problem Statement:     Calculating customer’s bill using classes     

You are required to write a program for calculating the bill of a customer according to the amount spent on shopping. A customer will be an object of a class. Each object has a name, customer ID and amount spent on shopping, as its data members. You will calculate the total bill by adding tax in the spending and subtracting discount offered according to spending.

Detailed Description:

  • You are required to create a class named Customer.
  • It will have name, customer-ID and spending as its private data members.
  • You have to make two constructors for this class, i.e. default constructor and parameterized constructor.
  • Default constructor will initialize the name with “No name”, customer-id with “0” and spending with “0”;
  • Parameterized constructor will take three arguments i.e. name, customer-id and spending.
  • Create getter and setter functions for each data member of the class.
  • A friend function will calculate the total bill for each object of class Customer, passed to it as argument.
  • If total spending is less than 5000, then tax added on total spending is 5% and discount is 1%. If spending is more than 5000 and less than 10,000 then 10% tax and discount is 2% and if spending is more than 10,000 then 15% tax and 3% discount is applicable.
  • Remember that all this calculation will be done inside friend function.
  • In the main() function, create three customer type objects by using new operator. Initialize first object with default constructor, second with parameterized and take input from user for third object (use setter functions to set third object’s values taken from the user).
  • Calculate total bill for all three objects by passing them to friend function and then display the result of calculation as shown in example output.

Sample Output:




Solution:-

/****************** Assignment No. 3 : Solution **********************/

using namespace std;

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

// Definition of customer class
class customer{

friend double calculateBill(customer*); // Declaration of Non-member friend 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*/
void setName(char[]);
void setID(int);
void setSpending(double);
char* getName();
int getID();
double getSpending();

};
// Default constructor Definition
customer::customer()
{
strcpy(name, “No Name”);
custID = 0;
spending = tax = discount = 0;

}

// Parameterized constructor Definition
customer :: customer(char Name[], int ID, double Spending)
{
strcpy(name, Name);
custID = ID;
spending = Spending;
tax = 0;
discount = 0;

}

//Definition of setter and getter functions
void customer :: setName(char Name[])
{
strcpy(name, Name);
}

void customer :: setID(int ID)
{
custID = ID;
}

void customer :: setSpending(double Spending)
{
spending = Spending;
}

char* customer :: getName()
{
return name;
}

int customer :: getID()
{
return custID;
}

double customer :: getSpending()
{
return spending;
}
/* Defining friend function calculateBill which takes an object as parameter and calculates total bill
after adding tax and subtracting discount from the bill*/
double calculateBill(customer *obj)
{
double bill = 0;

if(obj->spending <= 5000) //if the spending is less than 5000, then tax is 5% and discount is 1%
{
obj->tax = obj->spending * 5 / 100;
obj->discount = obj->spending * 1 / 100;
bill = obj->spending + obj->tax – obj->discount;
}

else if(obj->spending > 5000 && obj->spending <=10000) //if the spending is more than 5000 and less than 10000, then tax is 10% and discount is 2%
{
obj->tax = obj->spending * 10 / 100;
obj->discount = obj->spending * 2 / 100;
bill = obj->spending + obj->tax – obj->discount;
}

else //if the spending is more than than 10000, then tax is 15% and discount is 3%
{
obj->tax = obj->spending * 15 / 100;
obj->discount = obj->spending * 3 / 100;
bill = obj->spending + obj->tax – obj->discount;
}
return bill; // Function returns the final value of bill calculated in one of above three condition blocks
}

 

int main(){

char NAME[30];
int id = 0;
double Spending = 0;

//Creating object with default constructor
customer *cus1 = new customer(); //cus1 is a pointer to an object of type customer

/*Following code is showing the values of data members of object pointed by cus1*/
cout << “Bill detail for customer 1, initialized with default constructor” <<endl;
cout <<“Customer Name : ” << cus1->getName() << endl << “ID : ” << cus1->getID() << endl << “Spending : ” << cus1->getSpending() << endl << “Total Bill : ” << calculateBill(cus1) << endl;
cout << endl << endl;

//Creating object with parameterized construcor
customer *cus2 = new customer(“Ali”, 112, 8550);

/*Following code is showing the values of data members of object pointed by cus2*/
cout << “Bill detail for customer 2, initialized with parameterized constructor” <<endl;
cout <<“Customer Name : ” << cus2->getName() << endl << “ID : ” << cus2->getID() << endl << “Spending : ” << cus2->getSpending() << endl << “Total Bill : ” << calculateBill(cus2) << endl;
cout << endl << endl;

//Creating object with default constructor
customer *cus3 = new customer();

/*The following code takes input in three ordinary variables and then these variables are passed to
object pointed by the pointer cus3, through setter functions*/
cout << “Enter name of customer 3 : “;
cin >> NAME;

cout << “Enter ID of customer 3 : “;
cin >> id;

cout << “Enter total spending of customer 3 : “;
cin >> Spending;

cus3->setName(NAME);
cus3->setID(id);
cus3->setSpending(Spending);
cout << endl;

//Showing the values of object pointed by the pointer cus3.
cout << “Bill detail for customer 3, input entered by the user” <<endl;
cout <<“Customer Name : ” << cus3->getName() << endl << “ID : ” << cus3->getID() << endl << “Spending : ” << cus3->getSpending() << endl << “Total Bill : ” << calculateBill(cus3) << endl;
cout << endl << endl;

getch();

}

Download The Solution From here

 


CS201_Assignments_Solution_3_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 *

*