Creating a Public Item & Cart Class

  • MHB
  • Thread starter needOfHelpCMath
  • Start date
  • Tags
    Cart Class
In summary: You're using cout to display the data. This is a good practice, but you might want to consider using a more efficient library like cin or clog.In summary, a class with the following members and functions can be created: Item_nameItem_priceitem_qtyget/set functionsA "Cart" class with the following members and functions can also be created:vector to hold itemsinsertToCart // also should update item prices by qty amount when insertingaveragePrice // adds all items prices and computes the average pricecartTotal // adds all items prices and returns totalprintCartIn summary, a class with
  • #1
needOfHelpCMath
72
0
Make everything in public to save time or have public and private data members and functions:

1) Create a "Item" class with the following members and functions:
HTML:
 Item_name
   Item_price
   item_qty
   get/set functions

2) Create a "Cart" class with the following members and functions:

vector to hold items
HTML:
insertToCart // also should update item prices by qty amount when inserting
deleteItem //  searches for items and deletes it
averagePrice //  adds all items prices and computes the average price
cartTotal //  adds all items prices and returns total
printtCart

3) show how to create two items and insert it to the cart.

OUTPUT example:
HTML:
Items: Pens
Prices: $3  // pens price $1.50
Qty: 2
etc...
This is my code and so far I am not sure if I am on the right track. May anyone help guide me please. I have also decided to just input all the functions into public to save time.

HTML:
#include<iostream>
#include<string>
#include<vector>

class Item{
	
	public:
		
		string item_name;
		int item_price;
		int item_qty; 
		
		void setNamePriceQty(string a, int b , int c)
		void Print();
		int getPrice();
		int getQty();
		
	void Item::setNamePriceQty(string a, int b, int c) {
		item_name = a;
		item_price = b;
		item_qty =  c;
		
		return;
	}
	
	
	void Item::Print() {
		
		cout << item_name;
		cout << item_price << item_qty;
		
		return;
	}
};
	class Cart {
		public:
			
		vector<int>list;
		
		int total();
		
		void deleteItem (string a); // also should update item prices by qty amount when inserting
		void insertToCart(item b); // seaches for items and deletes  it
		void averagePrice; // adds all item prices and computers average price
		void printCart(); 
		void cartTotal; // adds all items prices and returns total
		
		
		void Cart::insertToCart(item b) {
		
			for (int i = 0; i <= list.size(); ++i) {
				if (list.at(i).item_name == b.item_name) {
					items.at(i).item_qty += b.itme_qty;
					items.at(i).item_price += b.item_price
					return(); }
					
					items.push_back(b)
					
					return; }
				}
				
		void Cart::deleteItem(string a) {
			for (int i = 0; i < list_size();++i) {
				if (list.at(i).item_name == b.item) {
					temp = list.at(i);
					list.at(i) =  list.at(list.size()-1);
					list.at(list.size() = temp;
					list.pop_back();
				}
			}
				}
			};
	
	
		
		

int main () {
	
	
	return 0;
}
 
Technology news on Phys.org
  • #2
You know you can test your own code right? One of the great things about it. A few notes:

1. You haven't declared constructors for each class. How will you create objects? What will their initial values be?
2. There's no need to have a return statement in a void function unless you're exiting from a loop perhaps. When you `insertToCart`, the item list will be updated within the class.
3. It's a good habit to pass constant references to variables when possible. e.g.

C++:
    // a,b,c are not modified by the function so they're constants.
    // Passing by reference avoids copying objects around the place.
    void Item::setNamePriceQty(const string& a, const int& b, const int& c) {
        item_name = a;
        item_price = b;
        item_qty =  c;
    }
 

FAQ: Creating a Public Item & Cart Class

1. What is a public item and cart class?

A public item and cart class is a set of code that allows you to create and manage items and shopping carts within a public setting, such as an online store or marketplace.

2. How do I create a public item and cart class?

To create a public item and cart class, you will need to have a basic understanding of programming languages such as Java or Python. You can start by defining the necessary variables, functions, and methods for your items and carts, and then implement them in your code.

3. What are the benefits of using a public item and cart class?

Using a public item and cart class can help you easily organize and manage your items and shopping carts, making it more efficient and convenient for both you and your customers. It also allows for easy customization and scalability.

4. Can a public item and cart class be used for any type of product or service?

Yes, a public item and cart class can be used for any type of product or service, as long as you have the necessary information and functionality defined in your code. This includes physical products, digital goods, subscriptions, and more.

5. Are there any limitations to using a public item and cart class?

One limitation of using a public item and cart class is that it requires some knowledge and experience in programming. Additionally, you may need to continually update and maintain your code to ensure it functions properly as your business grows and evolves.

Similar threads

Replies
1
Views
1K
Replies
2
Views
991
Replies
35
Views
3K
Replies
36
Views
2K
Replies
36
Views
4K
Replies
19
Views
1K
Replies
23
Views
2K
Replies
8
Views
2K
Back
Top