In: Computer Science
USING IDLE PYTHON:
A greengrocer would like to maintain a linked lists about his products. For each product he saves it name, price and stock amount.
Write a program that creates an empty linked list and then prompts to user to do one of the following:
1. Add a product to the list (anywhere)
2. Print all products in the LinkedList
3. Print all products above a certain price
4. Print all low-stock products ( Less than 20 pounds)
5. Exit
Hint: The trick here is to make a node with 4 parts: one for the produce name, one for the price, one for the stock and one a ref to the next node.
class Node:
def __init__(self, nm, pr, st):
self.name = nm
self.price = pr
self.stock = st
self.ref = None
The program is given below:that display menu to user if user choose 1 then add product to the end of the list. if user choose option 2 then print all products in the linked list. if user choose option 3 then print all products that are above certain price. if user choose option 4 then print all products that are less than 20 price. if user choose 5 then exit. and if user choose any other option then it continue to ask until you enter valid option.
class Node:
#singly linked node
def __init__(self,nm,pr,st):
self.name=nm
self.price=pr
self.stock=st
self.ref=None
class linked_list:
def __init__(self):
#empty list
self.tail = None
self.head = None
self.count = 0
def print_all_products(self):
# Iterate through the list and print all products.
c_item = self.tail
while c_item:
name1=c_item.name
price1=c_item.price
stock1=c_item.stock
c_item = c_item.ref
print("Name: ",name1,"Price: ",price1,"Stock: ",stock1)
def print_all_products_above_given_price(self,certain_price):
# Iterate through the list and print all products above certain price.
c_item = self.tail
while c_item:
if(c_item.price>certain_price):
name1=c_item.name
price1=c_item.price
stock1=c_item.stock
c_item = c_item.ref
print("Name: ",name1,"Price: ",price1,"Stock: ",stock1)
else:
c_item=c_item.ref
def print_all_low_products(self):
# Iterate through the list and print all products less than 20 price.
c_item = self.tail
while c_item:
if (c_item.price<20):
name1=c_item.name
price1=c_item.price
stock1=c_item.stock
c_item = c_item.ref
print("Name: ",name1,"Price: ",price1,"Stock: ",stock1)
else:
c_item = c_item.ref
def add_item_to_end(self,nm,pr,st):
#add items to the end of the list
node = Node(nm,pr,st)
if self.head:
self.head.ref=node
self.head = node
else:
self.tail = node
self.head = node
self.count += 1
item = linked_list()
ch=0
while ch!=5:
print("\n1. Add product to the list")
print("2. Print all products in the LinkedList")
print("3. Print all products above certain price")
print("4. Print all low-stock products (Less than 20 pounds)")
print("5. Exit")
ch=int(input(""))
if(ch==1):
name=input("Enter name: ")
price=float(input("Enter Price: "))
stock=float(input("Enter Stock: "))
item.add_item_to_end(name,price,stock)
elif(ch==2):
item.print_all_products()
elif(ch==3):
certain_price=float(input("Enter price to print all product above this given price: "))
item.print_all_products_above_given_price(certain_price)
elif(ch==4):
item.print_all_low_products()
The screenshot of code is given below:
Output: