calculating the bill of a customer according to the amount spent on shopping

Pages: 12
kindly i want an idea coding of this program within 2 days....

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.
Details:
 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.
closed account (o3hC5Di1)
Hi there,

The task seems pretty specific, so what are your actual troubles?

Some tips on getting started (for this specific task):

- Build it gradually, making core functionality first, adding things to it bit by bit, this gives you overview and structure. For instance, make sure you build the basic customer class with it's properties and constructors. Then go on to make the getters and setters. Then create the friend function, first calculating a basic total, then add the restrictions in your task description.
- Test frequently, preferably with your end result in mind, so create the three objects in main(){} as soon as your class is built and test all the functionality you add to it.
- If you run into troubles (after actually making a fair attempt), get back to us with your code and description of what the problem is.

Hope that helps.

All the best,
NwN
Thank you for you kind reply
actually i am not very much good in programming, i have a key book but there is more explanation then the final codes, so i need to have a look on it. i want to know how data flows in classes.
can you plz help me and send me few codes in classes?
closed account (o3hC5Di1)
Hi there,

For some examples on classes, have a look here:

http://cplusplus.com/doc/tutorial/classes/
http://cplusplus.com/doc/tutorial/classes2/
http://cplusplus.com/doc/tutorial/inheritance/

Classes should be seen as blueprints (plans), for example as you have a plan for building a house. A class (plan) in itself isn't of much value, you need to make an actual object (house) in order to use it. So in a class declaration, you simply define the data that an object needs and the functions it will need to perform, but it cannot do anything in itself, like a plan of a house, is not a house.

In your specific case, you would start out by making a customer class.

This would look like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//this is a class declaration, or the "plan" of what a "customer"-object should be like
class Customer {
    //private data, this means this data will only be accessible from within the object
    private:
        int id, spending;

    //public data, accessible from both within and without the object
    public:
        //getter/setter are public methods to access private data
        void set_id(int cust_id) { id = cust_id; }  //a setter
        int get_id() { return id; } //a getter
};

int main()
{
    //now let's make an actual object:
    Customer cu1 = new Customer;

    //let's do something with the customer
    cu1.set_id(34);  //set this customers id to 34
}


Hope that will get you started, let me know if you have any further questions :)

All the best,
NwN
Last edited on
closed account (o3hC5Di1)
Hi there,

I found some time and thought your assignment was a good exercise for me to try.

I won't just give you all the code, but here's my class declaration to get you started on what you'll need:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*************************
**  CLASS DECLARATION    *
**************************/
class Customer {
    private:
        int id;
        float spent;
        std::string name;

    public:
        Customer();
        Customer(int, float, std::string);
        friend float calculate_total(Customer);
        void set_id(int);
        int get_id();
        void set_spent(float);
        float get_spent();
        void set_name(std::string);
        std::string get_name();
};


Hope that helps.

All the best,
NwN
thanks a lot brother:)
this is really helpful for me
but few questions are:
how can i take user input while using classes
i want to take customer name as an input
how would i declare a variable?
secondly
i want to use friend function, which will do tax and discount calculations
how will it work?
guide me step by step
i have learnt the way you write a class and its data members
regards
nAd
wao it is really so nice of you :)
this is almost the same way what i am trying to write
but i am using this statement for name as an input
char name;
but you wrote std::string name;
what is the difference in these two statements?
i did not study std::string in lectures :o
std::string is the actual name for the string data type and is the standard way of doing so.

And to accept the classes name as an input, you simply use the . operator to access the public variable. But since name is private, as it should be, I'd suggest using a temp string to store the user input and then setting the name using the set_name function. It would look something like:
1
2
3
4
5
6
7
Customer myCustomer;
std::string customerName;

std::cout << "Please enter the customer's name: ";
std::cin >> customerName;

myCustomer.set_name(customerName);


This would assign the string entered as the customer myCustomer's name.
which header file will manipulate this data type?
<iostream.h>
dear i am really getting this right now:)
i try to write it with best of my knowledge
tomorrow i will discuss my progress on it
there will be two questions i think
writing the friend function and its working
secondly the display function, because the main thing is the display function i think
regards
thanks for you time and sincere help
may ALLAH bless you
closed account (o3hC5Di1)
Hi there,

some explanation to your questions:

nAd wrote:
but i am using this statement for name as an input
char name;
but you wrote std::string name;
what is the difference in these two statements?
i did not study std::string in lectures


Char reserves space in the memory for just one character.
If you want to store more characters, the easiest way is to use a string.
Strings are objects that allow you to manipulate series of chars in a clean and easy way. For more info see http://www.cplusplus.com/reference/string/string/ .
Note that in order to use them you need to do #include <string> at the top of your program.

nAd wrote:
which header file will manipulate this data type?
<iostream.h>


I'm not sure what you meant by that, if you meant what header file you need to use string, that's #include <string>

nAd wrote:
writing the friend function and its working


Friend functions are functions which are allowed to access a class's private data, whilst not being a member of the class.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Person {
    private:
        //this variable is private and thus only accessible normally within the object
        int phonenumber;
    
    public:
        friend void telephone(Person);
        Person(int phone) { phonenumber = phone; }
};

//note how we don't say Person::telephone, this is not a member of the Person class
void telephone(Person recipient)
{
    dial(recipient.phonenumber);
}

int main()
{
    //make a person wth telephonenumber
    Person mark(7777444);
    //telephone the person, because "telephone()" is a friend function,
    //it can access the "phonenumber" private property.
    telephone(mark);
}


So in your case, the friend function will take a "Customer" as an argument, allowing you to access his "spent amount". Then, you will have to check if that amount is higher than 5000, higher than 5000 and smaller than 10000, or else higher than 10000. Depending on that, you set the amount of tax and discount and you return the calculated result from the function.

nAd wrote:
secondly the display function, because the main thing is the display function i think


Let's come back to that when everything else works.

Hope that helps.

Salaum Aleikum (I think that's how you write it?)
NwN
yes it is Assalam-o-Alikum means (may you be in peace) and Wa-alikum Assalam in reply means (you also be in peace)
and yeah the friend function is little bit tough but i am gonna practice it , then in the evening i would let you know about the results
Thanks for you kind help
Stay blessed
nAd
i want to use new operator for 3 objects, but new operator calls the default constructor, how can i call parametrize constructor using new operator??
i am still so much confused:)
what will happen to me :)
plz help brother
 In the main() function, create three customer type objects by using new operator. Initialize first object with default constructor, second with parametrized and take input from user for third object (use setter functions to set third object’s values taken from the user).

this is my requirement
i wrote as follows:
customer *a;
a= new customer; OR a= new customer();
this statements calls the default constructor with no parameters, how can i call parametrize constructor?
and secondly i want an object which will take user's input of name, id, and speeding.
closed account (o3hC5Di1)
Hi there,

nAd wrote:
 In the main() function, create three customer type objects by using new operator. Initialize first object with default constructor, second with parametrized and take input from user for third object (use setter functions to set third object’s values taken from the user).


Using my class declaration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*************************
**  CLASS DECLARATION    *
**************************/
class Customer {
    private:
        int id;
        float spent;
        std::string name;

    public:
        Customer();
        Customer(int, float, std::string); // <- parameterized constructor
        friend float calculate_total(Customer);
        void set_id(int);
        int get_id();
        void set_spent(float);
        float get_spent();
        void set_name(std::string);
        std::string get_name();
};


So to implement it you would do:

1
2
3
4
5
Customer::Customer(int cu_id, float cu_spent, std::string cu_name)
{
    id = cu_id;
    //... and so on
}


In order to use the new operator, you must first declare a pointer that will point to (refer to) the actual data, like so:

1
2
3
4
5
6
7
8
9
10
11
12
Customer * anonymous;
Customer * mark;

anonymous = new Customer(); // default constructor
mark = new Customer(23, 7000, "Mark"); // parameterized constructor

// <code that uses these two customer-objects>

//when you no longer need dynamically assigned variables (using the 'new' keyword)
//you have to delete them to prevent memory leaks
delete anonymous;
delete mark;


Hope that helps.

All the best,
NwN
hi dear
mark = new Customer(23, 7000, "Mark"); // parameterized constructor
i tried this line in my code, but it gives some logical error i guess, the windows halts and program terminates :(
i am more confuse now
i am still unable to run the parametrized constructor, and i have to go for calculations and 3 objects, its making me freak out :x
help me brother
regards
:)
no brother i did it finally :P
it was some logical error because i was writing different parametric values and had initialize the constructor with different values, so i found it at last :)
now here comes my next objective :
i have to define 3rd object which will take user inputs i,e Name, Customer ID and Spending amount
and i have to calculate the Tax and Discount in the friend function and have to return the final answer to main()
please guide me here
regards
nAd
closed account (o3hC5Di1)
Hi there,

Good to see you're learning and making progress, well done! :)

As for the third object, this is where the setters/getters come in, you will need them because when you use the default constructor sets id=0,spent=0 and name="No Name" . So to changes those values, which are private, you will need to make public methods (functions) that change these values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*************************
**  CLASS DECLARATION    *
**************************/
class Customer {
    private:
        int id;
        float spent;
        std::string name;

    public:
        Customer();
        Customer(int, float, std::string);
        friend float calculate_total(Customer);
        //Getters & setters below
        void set_id(int);
        int get_id();
        void set_spent(float);
        float get_spent();
        void set_name(std::string);
        std::string get_name(); 
};



An example of their definition would be:

1
2
3
4
5
6
7
8
9
10
11
//id getter - it does nothing more than return the private property
int Customer::get_id()
{
    return id;
}

//id setter - it sets the private id property to what you pass as an argument
void Customer::set_id(int cu_id)
{
    id = cu_id;
}


Then, to create the third object, you would go about it like this:

1
2
3
4
5
6
Customer * generic;
generic = new Customer(); //default constructor, values are 0 / no name
generic->set_id(34); //sets the id to 34

//...
delete generic;


Please note that you need to use the arrow operator (->) because "generic" is a pointer to the dynamically created object.

As for your calculation in the friend function, here's some pseudo-code that may help you:


friend function (customer)
{
    variables tax, discount;
    if customer.spent is less than 5000: tax = 0.05 and discount = 0.01
    ...
    return customer.spent + (customer.spent*tax) - (customer.spent*discount);
    //original + x% tax - y% discount
}



Hope that helps, you're almost there :)

All the best,
NwN
hi dear
i understood this code below:
Customer * generic;
generic = new Customer(); //default constructor, values are 0 / no name
generic->set_id(34); //sets the id to 34

//...
delete generic;
but i am confused about taking name as an input from the user
how i would right this in code?
please guide me through this step
regards
closed account (o3hC5Di1)
Hi there,

taking input from the user is as easy as following:

1
2
3
4
5
6
7
8
9
Customer * generic;             //create our object
generic = new Customer();

std::string cu_name;           //temporary string to hold the name

cout << "Please enter the name for the third customer: ";   //tell the user what to input
cin >> cu_name;  //read the input into the cu_name variable

generic->set_name(cu_name);  //set the name 


Hope that helps.

All the best,
NwN
Pages: 12