Object represent a value and add/remove cart

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ryukku1111
    New Member
    • Aug 2013
    • 8

    Object represent a value and add/remove cart

    hi, im working on Book Ordering system and i having trouble how to make the system recognize different price values when i add different books into it, also how to make the system to be able to add/remove cart?
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi ryukku1111 and welcome to bytes.com!

    The basic answer is: This depends on what you have so far. How do you represent a book? How to you represent an order? How are price values and books connected? And what is your implementation of the cart?

    If you can provide more information I'm sure we'll be able to help you.

    Comment

    • ryukku1111
      New Member
      • Aug 2013
      • 8

      #3
      i intended to make a list of books to buy like this:

      1 C++ for dummies RM250
      2 Creativity & Innovation RM200
      3 Introduction to Java RM200

      and then users can select the books by input the number of the books they chosen, and then from the book selected the system will ask either more selection into or remove the cart or check out, if wanna check, the system will automatically calculate the total price need to pay from the numbers they selected, its like having price tag on the numbers

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Sounds good so far, but I'm guessing you haven't written any code yet? If so, here are two ideas that will hopefully get you started:
        • A book can be represented as a set of structured values including a name and a price. Do you know of any language elements in C/C++ that would allow you to represent such structures?
        • The available books and the cart are both collections of books. There are several ways to represent collections in C/C++. Which ones do you know? Which of those would be best suited?

        Comment

        • ryukku1111
          New Member
          • Aug 2013
          • 8

          #5
          im guessing array for the list of books? i mean i show out the list of books using using "cout" first then write out some code like 1 = RM250, 2 = RM200 then "total = ???" and the cart part i think a= add cart b= remove cart c= reset d= check out, im still a amateur for C++



          my coding so far:
          Code:
          #include <iostream>
          
          using namespace std;
          
          int main()
          {
          	string c;
          	int booknum;
          	cout << "**********Welcome to BOOK ORDERING SYSTEM**************" << ".\n";
          	cout << "these are the books available now" << ".\n";
          	cout << "1 C++ for dummies RM250" << ".\n";
          	cout << "2 Creativity & Innovation RM200" << ".\n";
          	cout << "3 Intorduction to Java RM200" << ".\n"; //i will put more books into the list
          
          	//this is the part i need help
          
          
          	cout << "Total amount need to pay: RM" << booknum * (day * 2) << ".\n"; //display total amount
          	cout << "Full report: you are ordering " << booknum << " books so amount need to pay is RM" << booknum<< ".\n"; //display full report
          	system ("pause");
          }
          Last edited by Rabbit; Sep 25 '13, 03:42 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            An array is certainly an option, yes. If the elements in that list don't change it's probably even the best option.
            As to the cart, you could use an array here too. Alternatively you could use a linked list or a double linked list; that depends on how you want to process the items in the list.

            Have you figured out how you want to represent of the book structure?

            Comment

            • ryukku1111
              New Member
              • Aug 2013
              • 8

              #7
              u mean like this?

              input 1 for C++ for dummies = RM250
              input 2 for Creativity & Innovation = RM200
              input 3 for Introduction to Java = RM200

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                No, that's not what I mean. I mean that you will probably want to have an array or a list of book objects, right? Each book has a name, a price and some other values. Then you can list all books by going through the list and easily add a new book to the cart or remove it from there. There are ways of structuring data in C/C++ which one can use for such tasks and I would strongly recommend that you use one of them.

                Comment

                • ryukku1111
                  New Member
                  • Aug 2013
                  • 8

                  #9
                  ok i will try to do that, maybe u can show me some code reference?

                  Comment

                  • ryukku1111
                    New Member
                    • Aug 2013
                    • 8

                    #10
                    do i have to declare a value for every element of the arrays?

                    Comment

                    • ryukku1111
                      New Member
                      • Aug 2013
                      • 8

                      #11
                      so i declare them right like this?
                      Code:
                       int Cplusplus [1] = {1};
                      	int creativity [1] = {2};
                      	int Java [1] = {3};
                      	int management [1] = {4};
                      Last edited by Rabbit; Sep 25 '13, 03:42 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.

                      Comment

                      • ryukku1111
                        New Member
                        • Aug 2013
                        • 8

                        #12
                        why all the cin and cout become ambiguous?
                        Code:
                        #include <iostream>
                        using namespace std;
                        
                        int main()
                        {
                        	int booknum, n, day;
                        	double Cplusplus = {10};
                        	int Creativity ={5};
                        	int Java = {5};
                        	int Management = {6};
                        	Cplusplus, Java, Creativity, Management = n;
                        
                        	cout << "**********Welcome to BOOK ORDERING SYSTEM**************" << ".\n";
                        	cout << "these are the books available now" << ".\n";
                        	cout << "input Cplusplus for dummies RM10/day" << ".\n";
                        	cout << "input Creativity for Creativity & Innovation RM5/day" << ".\n";
                        	cout << "input Java for Intorduction to Java RM5/day" << ".\n"; //i will put more books into the list
                        	cout << "input Management for Intoduction to management RM6/day" << ".\n";
                        	cout << ".\n";
                        	cout << "cart list";
                        	cin >> n >> ".\n";
                        	cout >> 
                        	//this is the part i need help
                        
                        
                        	cout << "Total amount need to pay: RM" << booknum * (day * 2) << ".\n"; //display total amount
                        	cout << "Full report: you are ordering " << booknum << " books so amount need to pay is RM" << booknum<< ".\n"; //display full report
                        	system ("pause");
                        }
                        Last edited by Rabbit; Sep 25 '13, 03:43 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.

                        Comment

                        • Nepomuk
                          Recognized Expert Specialist
                          • Aug 2007
                          • 3111

                          #13
                          OK, let's see... You wanted a code example, ok. Well, the structures I was hinting at are called stucts and they are very useful for cases such as this one. Say you want to have a structure representing the clothes you own, then the code may look something like this:
                          Code:
                          #include<iostream>
                          #include<string>
                          
                          using namespace std;
                          
                          typedef struct clothing_item {
                          	int position;
                              string name;
                              string type;
                              string colour;
                              double size;
                              int amount;
                          } Item;
                          
                          int main() {
                          	Item * items = new Item[4];
                          	
                          	items[0].name = "red jacket";
                          	items[0].type = "jacket";
                          	items[0].colour = "red";
                          	items[0].size = 41;
                          	items[0].amount = 1;
                          	
                          	items[1].name = "white shirts";
                          	items[1].type = "shirt";
                          	items[1].colour = "white";
                          	items[1].size = 40.5;
                          	items[1].amount = 2;
                          	
                          	items[2].name = "pairs of blue jeans";
                          	items[2].type = "trousers";
                          	items[2].colour = "blue";
                          	items[2].size = 30;
                          	items[2].amount = 3;
                          	
                          	items[3].name = "pair of black socks";
                          	items[3].type = "socks";
                          	items[3].colour = "black";
                          	items[3].size = 42;
                          	items[3].amount = 1;
                          	
                          	cout << "Here are the items I own:" << endl;
                          	for(int i = 0; i<4; i++) {
                          		cout << (i + 1) << ". " << items[i].amount << " " << items[i].name << endl;
                          	}
                          	
                          	cout << "Which one do you want to know more about? ";
                          	int nr;
                          	cin >> nr;
                          	cout << "This item is " << items[nr - 1].colour << endl;
                          
                          	// Just to stop the window from closing
                          	cin >> nr;
                          }
                          Next I'd read in the books you want to add to the cart in a loop. The easiest way to do that is probably to have the user enter the number of an item to add that item to the cart or a 0 if he wants to finish. Then you can add up the prices.

                          Comment

                          Working...