In: Computer Science
This is a python program. Put comments explaining the code, please.
Suppose you have been tasked with writing a Python program (using linked lists) to keep track of computer equipment. For each piece of equipment, we track its name, purchase date, purchase amount, and quantity on hand. Write a program that completes the following tasks:
allow the user to add a piece of equipment to the front of the list;
allow the user to update the quantity of a piece of equipment;
calculate and display (to the screen) the total value (in dollars) of ALL inventory
Please Refer to the following code:-
# Node class class Node: # Constructor to initialize the node object def __init__(self, name, purchase_date, amount, qty): # contains data self.name = name self.purchase_date = purchase_date self.amount = amount self.qty = qty # contains next node self.next = None # This class contains link list operations class LinkedList: # Function to initialize head def __init__(self): # initializing head to none self.head = None def insert(self, new_data): # Creating object for node new_node = Node(*new_data) # Pointing next of new node to current head new_node.next = self.head # shifting head to new node self.head = new_node def update_qty(self, name, qty): # storing head in temp temp = self.head # looping while (temp): # checking for name if temp.name == name: # updating quantity temp.qty = qty # breaking loop break # going to next node temp = temp.next else: # if not found print("Part Does not exist") def calculate(self): # intializing sum variable s = 0 # storing current head in temp temp = self.head # looping in linked list while (temp): # calculating sum s += int(temp.amount) * int(temp.qty) # going to next node temp = temp.next # returning sum return s # initializing linked list object ll = LinkedList() # loop while True: # printing menu print("1. Add a equipment") print("2. update quantity") print("3. Calculate value") print("Q. quit") # taking Choice input choice = input("Choose an option:") if choice == '1': # Taking input of necessary data name = input("Enter name of part:") date = input("Enter date of part:") amount = input("Enter amount:") qty = input("Enter Qty:") # inserting into linked list ll.insert([name, date, amount, qty]) print("Succesfully inserted") elif choice == '2': # Taking name and quantity as input name = input("Enter name of part:") qty = input("Enter Qty:") # updating quantity ll.update_qty(name, qty) print("Succefully Updated") elif choice == '3': # calculating total value and printing print("Total value is $", ll.calculate()) elif choice.upper() == 'Q': # quitting print("Thank You,Bye!") break else: # wrong choice print('Try Again')
==================================
Output:-
1. Add a equipment
2. update quantity
3. Calculate value
Q. quit
Choose an option:1
Enter name of part:a
Enter date of part:12/01/19
Enter amount:10
Enter Qty:4
Succesfully inserted
1. Add a equipment
2. update quantity
3. Calculate value
Q. quit
Choose an option:2
Enter name of part:a
Enter Qty:5
Succefully Updated
1. Add a equipment
2. update quantity
3. Calculate value
Q. quit
Choose an option:3
Total value is $ 50
1. Add a equipment
2. update quantity
3. Calculate value
Q. quit
Choose an option:q
Thank You,Bye!
===============================
Please Refer to the following images
===============================================
Please Upvote
===============================================