I'm trying to complete a lab on OOP in Python.
The assignment consists of creating a ‘ShoppingCart’ and ‘Shop’ class with ‘Shop’ inheriting from ‘ShoppingCart’.
In the Shopping Cart class is a constructor that takes no arguments and sets the ‘total’ attribute to zero and initializes an empty dict attribute named ‘items’.
Also the following methods are to be created ‘add_item’, ‘remove_item’ (both requiring similar arguments: item_name, quantity, price); the ‘add_item’ method should add the cost of the added items to the current value of total and also add an entry to the ‘items’ dict such that the key is ‘item_name’ and value is ‘quantity’ while the ‘remove_item’ method does the reverse; and a ‘checkout’ method that takes in ‘cash_paid’ and returns the balance from the payment
The ‘Shop’ class has a constructor that takes no arguments and initializes an attribute ‘quantity’ at 100.
It is also required to override the ‘remove_item’ method such that calling Shop’s ‘remove_item’ with no arguments decrements quantity by one.
I keep getting a stack trace error relating to the super method in the 'Shop' class.
Here's my code so far:
class ShoppingCart:
def __init__(self):
self.total = 0
self.items = {}
def add_item(self, item_name, quantity, price):
self.price = price
self.item_name = item_name
self.quantity = quantity
if self.item_name in items:
items[item_name] += quantity
else:
items[item_name] = quantity
self.total += price*quantity
def remove_item(self,item_name, quantity, price):
self.price = price
self.quantity = quantity
self.item_name = item_name
if self.item_name in items:
self.total -= price*quantity
del items[item_name]
def checkout(self, cash_paid):
self.cash_paid = cash_paid
if cash_paid < self.total:
return "Cash paid not enough"
else:
return cash_paid - self.total
class Shop(ShoppingCart):
def __init__(self):
super().__init__(quantity)
self.__quantity = 100
def __remove_item (self):
self.__quantity -= 1
Also my add_item and remove_item methods seem to raise errors, but I don't know what I'm doing wrong.
These are the unit tests for the lab, it's really challenging:
import unittest
class ShoppingCartTestCases(unittest.TestCase):
def setUp(self):
self.cart = ShoppingCart()
self.shop = Shop()
def test_cart_property_initialization(self):
self.assertEqual(self.cart.total, 0, msg='Initial value of total not correct')
self.assertIsInstance(self.cart.items, dict, msg='Items is not a dictionary')
def test_add_item(self):
self.cart.add_item('Mango', 3, 10)
self.assertEqual(self.cart.total, 30, msg='Cart total not correct after adding items')
self.assertEqual(self.cart.items['Mango'], 3, msg='Quantity of items not correct after adding item')
def test_remove_item(self):
self.cart.add_item('Mango', 3, 10)
self.cart.remove_item('Mango', 2, 10)
self.assertEqual(self.cart.total, 10, msg='Cart total not correct after removing item')
self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items not correct after removing item')
def test_checkout_returns_correct_balance(self):
self.cart.add_item('Mango', 3, 10)
self.cart.add_item('Orange', 16, 10)
self.assertEqual(self.cart.checkout(265), 75, msg='Balance of checkout not correct')
self.assertEqual(self.cart.checkout(25), 'Cash paid not enough', msg='Balance of checkout not correct')
def test_shop_is_instance_of_shopping_cart(self):
self.assertTrue(isinstance(self.shop, ShoppingCart), msg='Shop is not a subclass of ShoppingCart')
def test_shop_remove_item_method(self):
for i in range(15):
self.shop.remove_item()
self.assertEqual(self.shop.quantity, 85)