In: Computer Science
Program Specification
Design an inventory class that stores the following members:
The class should have appropriate setter and getter functions.
Next, design a stack class that can hold objects of the class described above. If you wish, you may use the linked list from program 5 as a basis for designing your stack.
Finally, design a program that uses the stack class described above. The program should have a loop that asks the user if he or she wishes to add a part to inventory, or take a part from inventory. The loop should repeat until the user is finished. If the user wishes to add a part to inventory, the program should ask for the serial number, date of manufacture, and lot number. The data should be stored in an inventory object, and the object should be pushed onto the stack. If the user wishes to take a part from inventory, the program should pop the top-most part from the stack. When the user finishes the program, it should display the contents of the member values of all the objects that remain on the stack.
All i need in the display function not the whole program.In python.
To create a stack class that will keep inventory objects in the stack, We first have to create an inventory class.
I have coded everything to make a proper functional stack that will add to inventory, take from inventory and additionally show the items from the inventory and at last exit when the user wants.
import datetime
class inventory(object):
def __init__(self,serialNum=None,lotNum=None,dom=None):
self.serialNum = serialNum
self.lotNum = lotNum
self.dom = dom
# getter method
def get_serialNum(self):
return self.serialNum
# setter method
def set_serialNum(self, x):
self.serialNum = x
# getter method
def get_lotNum(self):
return self.lotNum
# setter method
def set_lotNum(self, x):
self.lotNum = x
# getter method
def get_dom(self):
return self.dom
# setter method
def set_dom(self, x):
self.dom = x
Below is the stack used :
class stack:
def __init__(self):
self.stack = []
def push(self,inv_obj):
self.stack.insert(0,inv_obj)
def pop(self):
del self.stack[0]
Additional Functions:
def get_date():
dom = input("Enter date of manufacture [dd/mm/yyyy]: ")
try:
dom = datetime.datetime.strptime(dom,"%d/%m/%Y").date()
return dom
except:
print("Invalid Date Format, Please enter mentioned date format:")
return get_date()
def create_inventory():
serialNum = input("Enter Serial Num: ")
dom = get_date()
lotNum = input("Enter Lot Num: ")
inv = inventory()
inv.set_lotNum(lotNum)
inv.set_dom(dom)
inv.set_serialNum(serialNum)
st.push(inv)
def pop_inventory():
st.pop()
def show_inventory():
print("\n Inventory Items:")
for item in st.stack:
print("Serial Num: ",item.get_serialNum())
print("Date of manufacture: ",item.get_dom())
print("Lot Num: ",item.get_lotNum())
print(20*"--")
Now the one last Display function which will combine all:
def display():
while(1):
print(20*"==")
print("1. Add to inventory.")
print("2. Take from inventory.")
print("3. Show inventory.")
print("4. Stop.")
val = input("Choose from above options: ")
if(val=='1'):
create_inventory()
elif(val=='2'):
pop_inventory()
elif(val=='3'):
show_inventory()
elif(val=='4'):
break
else:
print("Invalid Entry, Please select from [1,2,3,4]: ")
Now, let's make a stack object and call the display function:
st = stack()
display()
With this we completed the coding part and now let's see some sample outputs when we call display function.
With this, I hope you got the idea to go ahead with your problem.
Thanks and have a nice day!!
Below, I have attached all coding screenshots to avoid any indentation issues: