Question

In: Computer Science

USING IDLE PYTHON: A greengrocer would like to maintain a linked lists about his products. For...

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

Solutions

Expert Solution

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:


Related Solutions

Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list...
Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list for employees, the data stored is ·An employee number (a positive integer) ·A yearly salary (a float). ·Number of dependents (a short positive integer) The employer would like you as the programmer to design and implement a linked list using classes. For each class two files are needed, one to define the class, the other to implement the methods. In addition, the client uses...
Please include comments on what you are doing.   Using linked lists, write a Python program that...
Please include comments on what you are doing.   Using linked lists, write a Python program that performs the following tasks: store the records for each college found in the input file - colleges.csv - into a linked list. (File includes name and state data fields) allow the user to search the linked list for a college’s name; display a message indicating whether or not the college’s name was in the database allow the user to enter a state's name and...
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
Python linked lists ● Insert: this method takes a value as a parameter, and adds a...
Python linked lists ● Insert: this method takes a value as a parameter, and adds a node which contains the value to the end of the linked list ● Delete: this method deletes a node from the linked list. If an index is passed as a parameter, then the method should delete the node at this index. If no index is passed, then delete the first item in the list ● Find: this method takes a value as a parameter,...
In python i want to create a function. The function will take in two linked lists...
In python i want to create a function. The function will take in two linked lists as the parameters. If one is shorter than the other then the shorter will be the length. I want to take the values from both linked lists and turn them into tuples. I then want these tuples to be put into a new linked list. I want to return that linked list. I want to do this using recursion and no helper functions or...
How to do this in Python (using Lists): Create a python program that allows a user...
How to do this in Python (using Lists): Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird. You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions: 1. Display all U.S. States in Alphabetical order along with Capital and Bird 2. Search for a specific state and display...
The goal of this assignment is to implement a set container using linked lists. Use the...
The goal of this assignment is to implement a set container using linked lists. Use the authors bag3.h and bag3.cpp as a basis for implementing your set container using linked lists. The authors bag3.h and bag3.cpp can be found here https://www.cs.colorado.edu/~main/chapter5/ Since you are using the authors bag3.h and bag3.cpp for your Set container implementation, make sure that you change the name of the class and constructors to reflect the set class. Additionally you will need to implement the follow...
PYTHON PROGRAM Buzz Lightyear is teaching his friends about money. At the moment, they are using...
PYTHON PROGRAM Buzz Lightyear is teaching his friends about money. At the moment, they are using fictional $ 5, 10, and 20 Monopoly game bills. Buzz hands out the bills to his friends so that they each have the same amount of money. Then he moves the bills from one friend's pile and puts it on another's pile. Next, the toys have to figure out which one has more money and which one has less. Sometimes Buzz doesn't move a...
Can you write a program for the card game WAR using linked lists in c++!
Can you write a program for the card game WAR using linked lists in c++!
Using Python In this assignment we will try to add tuples, lists, if statements and strings...
Using Python In this assignment we will try to add tuples, lists, if statements and strings to our program. For example, you can ask for a user for a couple items, their name and age – and depending on their age, print out a predefined list. You could ask for some string input and decide to do something with the output. You could ask for three items, 1) age, 2) are you a male or female and 3) are your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT