I am working on designing a basic shopping cart and below is my design approach.
I would be glad to know the shortcomings of the below design and suggestions to enhance the same are most welcome.
I am not putting down the requirements just to keep alive the scope of adding some important functionalities which I might have missed in my perception of a basic cart.
Any and all suggestions are welcome, related to design or performance or a cleaner code.
Note : The design assumes that the computing environment is single threaded.
#ifndef _SIMPLE_CART_H_
#define _SIMPLE_CART_H_
#include <unordered_map>
#include <unordered_set>
class Cart
{
public:
static Cart& GetCart();
bool AddItem(unsignedlong itemID); //check current availability of item in DB. If there, add item and return true, else return false.
bool DeleteItem(unsignedlong itemID); //return true if delete successful
unsignedlong DecreaseItemCount(unsignedlong itemID); //Decrease item copies and return current copies of this item in cart.
bool SaveToWishList(unsignedlong itemID); //save the item to wishlist
bool Checkout(); //checkout
private:
static std::unordered_map<unsignedlong, unsignedlong> cartMap; //key is itemID and value is the number of instances of items added.
static std::unordered_set<unsignedlong> wishList; //this list contains the itemID's of the items added to wishlist.
staticunsignedlong cartTotal;
Cart() = default;
Cart(const Cart&) = delete;
Cart(Cart&&) = delete;
Cart& operator =(const Cart&) = delete;
Cart& operator =(Cart&&) = delete;
};
#endif
Actually a class should have member variables. So I would suggest to think what data a class like Cart may contain. The next step would be a container for Cart. Probably a vector. This would make thinks easier.
1) Did you mean that I should have a container for multiple carts ?
2)Or a container to store the collection of items in the Cart ?
Both.
You have multiple carts hence use a container for the carts.
You have multiple items in the cart hence use a container for the items as a member of carts. For instance: