I completed the following task:
You need to create the foundations of an e-commerce engine for a B2C (business-to-consumer) retailer. You need to have a class for a customer called User, a class for items in inventory called Item, and a shopping cart class calledCart. Items go in Carts, and Users can have multiple Carts. Also, multiple items can go into Carts, including more than one of any single item.
I am a new Pythoner, and don't have good knowledge of OOP. I feel the code is not good. I welcome recommendations to improve the code.Here's my code:
class Item(object):
def __init__(self,itemname,itemprice):
self.__itemname = itemname
self.__itemprice = itemprice
def GetItemName(self):
return self.__itemname
def GetItemPrice(self):
return self.__itemprice
def ChangeItemPrice(self,newprcie):
self.__itemprice = newprcie
class Cart(dict): #cart dict format: {itemname:[price,number]}
def ShowCart(self):
return self
class User(object):
def __init__(self, name):
self.name = name
self.__cartlist = {}
self.__cartlist[0] = Cart()
def AddCart(self):
self.__cartlist[len(self.__cartlist)] = Cart()
def GetCart(self, cartindex = 0):
return self.__cartlist[cartindex]
def BuyItem(self, item, itemnum, cartindex = 0):
try:
self.__cartlist[cartindex][item.GetItemName()][1] += itemnum
except:
self.__cartlist[cartindex].update({item.GetItemName():[item.GetItemPrice(),itemnum]})
def BuyCancle(self, itemname, itemnum, cartindex = 0):
pass
if __name__ == '__main__':
item1 = Item('apple', 7.8)
item2 = Item('pear', 5)
user1 = User('John')
user1.BuyItem(item1, 5)
print("user1 cart0 have: %s" % user1.GetCart(0).ShowCart())
user1.BuyItem(item2, 6)
print("user1 cart0 have: %s" % user1.GetCart(0).ShowCart())
user1.AddCart()
user1.BuyItem(item1, 5, 1)
print("user1 cart1 have: %s" % user1.GetCart(1).ShowCart())