A shopping cart : Design issues & enhancements.

Hi All :),

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.



cart.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef _SIMPLE_CART_H_
#define _SIMPLE_CART_H_

#include <unordered_map>
#include <unordered_set>

class Cart
{
    public:
        static Cart& GetCart();
        bool AddItem(unsigned long itemID); //check current availability of item in DB. If there, add item and return true, else return false.
        bool DeleteItem(unsigned long itemID);  //return true if delete successful
        unsigned long DecreaseItemCount(unsigned long itemID); //Decrease item copies and return current copies of this item in cart.
        bool SaveToWishList(unsigned long itemID); //save the item to wishlist
        bool Checkout();  //checkout

    private:
        static std::unordered_map<unsigned long, unsigned long> cartMap; //key is itemID and value is the number of instances of items added.
        static std::unordered_set<unsigned long> wishList;  //this list contains the itemID's of the items added to wishlist.
        static unsigned long cartTotal;
        Cart() = default;
        Cart(const Cart&) = delete;
        Cart(Cart&&) = delete;
        Cart& operator =(const Cart&) = delete;
        Cart& operator =(Cart&&) = delete;

};

#endif 




cart.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "simple_cart.h"

std::unordered_map<unsigned long, unsigned long> Cart::cartMap;
std::unordered_set<unsigned long> Cart::wishList;
unsigned long Cart::cartTotal{0};

Cart& Cart::GetCart()
{
    static Cart cart{};
    return cart;
}

bool Cart::AddItem(unsigned long itemID)
{
    //check current availability of item in DB. If available, add item and return true, else return false.
    //update cartTotal as well.

    auto it = cartMap.find(itemID);

    if (it != cartMap.end())
        ++(it->second);
    else
        cartMap.insert(it, std::make_pair((unsigned long)itemID, (unsigned long) 1));

    return true;
}   

bool Cart::DeleteItem(unsigned long itemID)
{   
    auto it = Cart::cartMap.find(itemID);

    if (it == Cart::cartMap.end())    return false;

    Cart::cartMap.erase(it);
    return true;
} 

unsigned long Cart::DecreaseItemCount(unsigned long itemID)
{
    auto it = Cart::cartMap.find(itemID);

    if (it == Cart::cartMap.end())    return 0;

    if (it->second == 1)    { Cart::cartMap.erase(it); return 0;}

    --(it->second);

    return it->second;
}

bool Cart::SaveToWishList(unsigned long itemCode)
{
    Cart::wishList.insert(itemCode);
    return true;
}

bool Cart::Checkout()
{
    //call appropriate function to take to payment gateway with the updated total.
    //if some issue/error, return false.

    return true;
}


Thanks :)
Last edited on
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.
Thanks
coder77
for taking a look :)

you have mentioned :
The next step would be a container for Cart.


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 ?

For the second requirement, I have used
unordered_map
container.

Kindly elaborate on the mentioned statement.

Thanks.
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
struct Item
{
  int id;
  std::string Name;
  doubgle Price;
...
};
struct Cart
{
  std::set<int> itemSet;
};
struct Shop
{
  std::vector<Item> availableItems;
  int availableCarts = 100;

  Cart *checkoutCart()
  {
    Cart *result = nullptr;
    if(availableCarts > 0)
    {
      --availableCarts;
      result = new Cart{};
    }
    return result;
  }
  Item *buyItemCart *cart);
};
Last edited on
Thanks @coder777 for pointing out this.
I think I'll need to have a factory for carts as each user can have one cart associated to the account.
Topic archived. No new replies allowed.