The Product
class has been defined.
class Product:
def __init__(self, code, description, price=1.0, quantity=1):
self.code = int(code)
self.description = str(description)
self.price = float(price)
self.quantity = int(quantity)
def __str__(self):
if self.quantity == 0:
return ("Code = {}, {} - Out of Stock".format(self.code, self.description))
else:
return ("Code = {}, {}, ${:.2f}, quantity = {}".format(self.code, self.description, self.price, self.quantity))
I have also defined another class - PurchaseProduct
Here I am having problem in the purchasing(self, search_code)
method. what this method does is it takes a code value as a parameter and decrements the quantity value by 1 if the the search code exists in the shopping items list and the item is available (i.e. quantity > 0). It also prints the description and price (2 decimal places) of that item. The method invokes the search() method.
If the search code item exists but it is not available, the method should print the message 'XXX Out of Stock!' where XXX indicates the item code. If the search ode does not exist, the method should print the message "XXX Not Found!" where XXX indicates the item code. Finally, the method returns the price of the item if the item exists and is available.
class PurchaseProduct:
def __init__(self, filename = 'items.txt'):
self.filename = filename
PurchaseProduct.items_list = []
def read_item_file(self):
try:
file = open(self.filename, 'r')
res = file.read()
content_list = res.split('\n')
except FileNotFoundError:
print ("ERROR: The file '{}' does not exist.".format(self.filename))
return 0
else:
file.close()
return res_list
def load_items(self):
try:
list_items = self.read_item_file()
for item in list_items:
code, desc, price, quant = item.split(',')
self.items_list.append(Product(code= int(code), description= str(desc), price=price, quantity= int(quant)))
except:
list_items = []
def search(self, search_code):
self.search_code = search_code
for index in range(len(PurchaseProduct.items_list)):
if PurchaseProduct.items_list[index].code == self.search_code:
return (PurchaseProduct.items_list[index])
def purchasing(self, search_code): #ISSUE HERE
self.search_code = search_code
for index in range(len(PurchaseProduct.items_list)):
if self.search(search_code) != None:
if PurchaseProduct.items_list[index].quantity >= 1:
print ("{} ${:.2f}".format(PurchaseProduct.items_list[index].description,PurchaseProduct.items_list[index].price))
PurchaseProduct.items_list[index].quantity -= 1
return PurchaseProduct.items_list[index].price
else:
print ("{} Out of Stock!".format(PurchaseProduct.items_list[index].code))
return PurchaseProduct.items_list[index].price
else:
print ("{} Not Found!".format(self.search_code))
return PurchaseProduct.items_list[index].price
Test:
shop_cart = PurchaseProduct('item.txt')
shop.load_items()
cost = shop_cart.purchasing(11)
cost += shop_cart.purchasing(11)
print(cost)
cost += shop_cart.purchasing(11)
print(cost)
cost += shop_cart.purchasing(999)
print(cost)
Expected Output:
Coca Cola Soft Drink 500ml $4.00
Coca Cola Soft Drink 500ml $4.00
8.0
11 Out of Stock!
8.0
999 Not Found!
8.0
Actual Output:
Coca Cola Soft Drink 500ml $4.00
Coca Cola Soft Drink 500ml $4.00
8.0
11 Out of Stock!
12.0
999 Not Found!
14.0
Contents of 'item.txt'
:
11,Coca Cola Soft Drink 500ml,4,2
12,L & P Soft Drink Lemon & Paeroa 500ml,4,9
13,V Blue Drink can 500mL,3.5,8
14,V Vitalise Energy Drink 500ml,3.5,5
15,Pump Water NZ Spring 750ml,2.5,9
16,Twix Chocolate Bar 50g,2.5,12
17,Nestle Kit Kat Chocolate Bar 4 Finger, 2.4,15
18,Snickers Chocolate Bar 50g,2,11
19,Cadbury Chocolate Bar Crunchie 50g, 2,13
20,Cadbury Picnic Chocolate Bar 46g,2,15
In your specific case, it seems that you can simply return 0
when the item is out of stock, as it won't change the total cost.
if PurchaseProduct.items_list[index].quantity >= 1:
print("{} ${:.2f}".format(PurchaseProduct.items_list[index].description,PurchaseProduct.items_list[index].price))
PurchaseProduct.items_list[index].quantity -= 1
return PurchaseProduct.items_list[index].price
else:
print ("{} Out of Stock!".format(PurchaseProduct.items_list[index].code))
return 0