-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW 5.py
133 lines (104 loc) · 4.06 KB
/
HW 5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class Node:
def __init__(self, name, price, stock):
self.name = name
self.price = price
self.stock = stock
self.ref = None
class LinkedList:
# ---------------Create an empty linked list -------------
def __init__(self):
self.start_node = None
# ------------- Insert at start ----------------------
def insert_at_start(self, name, price, stock):
new_node = Node(name, price, stock)
new_node.ref = self.start_node
self.start_node = new_node
# --------------- Traverse the linked list ----------------------
def traverse_list(self):
print("Here is the Product List")
if self.start_node is None:
print("List is empty")
return
else:
n = self.start_node
while n is not None:
print(f"Name: {n.name} | Price: {n.price} | stock: {n.stock}")
n = n.ref
print(" ")
# --------------- Print lowstock items ----------------------
def lowStock(self):
print("Here are the items with less than 20 pounds left")
if self.start_node is None:
print("List is empty")
return
else:
n = self.start_node
while n is not None:
if n.stock < 20:
print(f"Name: {n.name} | Price: {n.price} | stock: {n.stock}")
n = n.ref
print(" ")
# --------------- print out prices above a certin price ----------------------
def findPriceOfStuff(self,x):
print(f"Here is everything above ${x}")
if self.start_node is None:
print("List is empty")
return
else:
n = self.start_node
while n is not None:
if n.price > x:
print(f"Name: {n.name} | Price: {n.price} | stock: {n.stock}")
n = n.ref
print(" ")
# ___________ USER COMMANDS ______________________
"""
My test inputs
avacado 30 40
orange 40 20
banna 10 30
"""
productList = LinkedList()
loop = "yes"
while loop == "yes":
print("""Type 'insert info' to add information to the linked list
Type 'print info' to print all information in the linked list
Type 'find prices' to print items above a certian price
Type 'print stock' to print items that have less than 20 pounds of stock left""")
choice = input()
choice = choice.lower()
if choice == "insert info":
loop = "no"
ok = "no"
while ok == "no":
try:
name, price, stock = input("Please give the name the price and the stock in that order on a single line with spaces seperating them: ").split()
price = float(price)
stock = float(stock)
ok = "yes"
except:
print("Invalid input returning to previous section, please don't put any '$' or other additional symbols into price or stock")
ok = "no"
productList.insert_at_start(name, price, stock)
ok = "no"
loop = "yes"
print(f"Name: {name} | Price: {price} | Stock: {stock} | Inserted into the list")
elif choice == "print info":
productList.traverse_list()
elif choice == "find prices":
ok2 = "no"
while ok2 == "no":
try:
pricesInput = input("Please give the lowest price: ")
pricesInput = float(pricesInput)
print(pricesInput)
productList.findPriceOfStuff(pricesInput)
ok2 = "yes"
except:
print("Invalid input, please only put a number with no symbols")
ok2 = "no"
elif choice == "print stock":
productList.lowStock()
else:
print("Invalid input returning to main menu")
loop = "yes"