Question
Programming Language Python: ------------------------------------------------------- The following is the item class used: class Item: def __init__(self, code, desc, price, quantity): self.__code = code # the item
Programming Language Python:
-------------------------------------------------------
The following is the item class used:
class Item: def __init__(self, code, desc, price, quantity): self.__code = code # the item code self.__description = desc # the item description self.__price = price # the item unit price self.__quantity = quantity # the number of item available def getCode(self): return self.__code def setCode(self, code): self.__code = code; def getDescription(self): return self.__description def setDescription(self, desc): self.__description = desc def getPrice(self): return self.__price def setPrice(self, price): self.__price = price def getQuantity(self): return self.__quantity def setQuantity(self, quantity): self.__quantity = quantity
def __repr__(self): return 'Item({0}, {1}, {2}, {3})'.format(self.__code, self.__description, self.__price, self.__quantity)
def __str__(self): return '{0}, {1}, {2}, {3}'.format(self.__code, self.__description, str(self.__price), str(self.__quantity))
-------------------------------------------------------------------------------------------------------------------------------------------------------
The stock class needs to be completed.
lass Stock: # the constructor def __init__(self): self.__items = []
# This function gets the number of items in stock. def get_size(self): return len(self.__items)
# This function loads all the items on sale into the list. def load_items(self, filename): try: infile = open(filename, "r") stock_str = infile.read() infile.close() stock_list = stock_str.split(" ") for item in stock_list: if item != "": item_list = json.loads(item) self.__items += [Item(item_list[0], item_list[1], item_list[2], item_list[3])] except IOError: print("Error: File does not exist.") # This function saves all the items on sale into a file. def save_items(self, filename): try: out_file = open(filename, 'w') for item in self.__items: item_list = [item.getCode(), item.getDescription(), item.getPrice(), item.getQuantity()] str_out = json.dumps(item_list) out_file.write(str_out + " ") out_file.close() except IOError: print("Error: File writing error.")
# This function finds an item on sale based on the item code. def find_item(self, code): ## IMPLEMENT THIS METHOD # This function displays all the items on sale. def display_items(self): ## IMPLEMENT THIS METHOD
-----------------------------------------------------------------------------------------------------------------------
The Cart needs to be completed:
from Item import Item
class Cart:
# the constructor
def __init__(self):
self.__items = []
# This function gets the number of items in the shopping cart.
def get_size(self):
return len(self.__items)
# This function adds an item into the shopping cart.
def add_item(self, item):
## IMPLEMENT THIS METHOD
# This function finds an item on sale based on the item code.
def find_item(self, code):
## IMPLEMENT THIS METHOD
# This function removes an item from the shopping cart.
def delete_item(self, item):
## IMPLEMENT THIS METHOD
# This function clears everything in the shopping cart.
def discard_all(self):
## IMPLEMENT THIS METHOD
# This function prints out the items bought and calculates the total amount due.
def check_out(self):
## IMPLEMENT THIS METHOD
Pleae complete the find_item and display_items functions in the stock class
And
Please complete the functions in the cart class
Input File:
["BC001", "Fresh toast bread white (700g)", 3.99, 20] ["BC002", "Low-fat milk (2 liter)", 4.80, 10] ["BC003", "V-energy drink", 2.75, 10] ["BC004", "Fresh garlic (450g)", 1.98, 0] ["BC005", "Coca-Cola (300 ml)", 2.50, 10] ["BC006", "Pineapple", 3.60, 6] ["BC007", "Mango", 1.89, 4] ["BC008", "Snickers chocolate bar", 1.80, 20] ["BC009", "Broccoli", 1.47, 11] ["BC010", "Washed Potato (2.5kg)", 2.98, 7] ["BC011", "Good-morning cereal", 5.60, 10] ["BC012", "Rose apple (1.5kg bag)", 4.98, 5] ["BC013", "Avocado (4pk)", 4.99, 5] ["BC014", "Bananas (850g bag)", 2.96, 4] ["BC015", "Kiwi fruit green (1kg)", 2.45, 10] ["BC016", "Rock melon", 7.98, 2] ["BC017", "Lettuce", 2.99, 12] ["BC018", "Chocolate block (200g)", 3.59, 10] ["BC019", "Watermelon", 8.99, 0] ["BC020", "Parsley curly", 1.99, 6] ["BC021", "Grapefruit 1kg", 3.99, 7]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started