-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStock-inventory-system.py
More file actions
148 lines (119 loc) · 6.21 KB
/
Stock-inventory-system.py
File metadata and controls
148 lines (119 loc) · 6.21 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
This application implements software to record stock and inventory of a computer store.
This stock inventory will record manufacturer, quantity of devices, unique serial number and the price of the
device.This inventory will not allow any negative number in the quantity of item or the price. The price will have
2 decimal float value, and the quantity will be a whole number only. Application is designed for the user's ease,
hence initial of each choice will work as well (eg: instead of 'add' you can just type 'a'). All data will be
non-case sensitive, so it can be searched or typed in any case.
Please note: random is used create unique serial numbers, and tabulate had been used with list of lists to display
the inventory in a table format.
"""
#importing random - unique serial numbers & tabulate to create a neat table format
import random
from tabulate import tabulate
#defining the function to add the item details and serial_num
def add_item (stock_inventory):
# prompting user to provide item name
item_name = input("Enter item name:\t")
item_name=item_name.capitalize()
#promting for quantity of the item
while True:
item_qty = input("Enter item quantity:\t")
#convert the item_qty input string to integer
#this loop will ensure no negative or non-real numbers are added to the quantity
try:
item_qty=int(item_qty)
if item_qty<=0:
print("Invalid input. Negative or zero values are not acceptable, please type whole number.")
else:
break
except ValueError:
print("Invalid input. Decimal values are not acceptable. Please enter whole number.")
#promting user for price of the item
while True:
item_price_str=input("Enter item price:\t$")
#convert the price to 2 decimal floating value
try:
item_price_flt=float(item_price_str) #conveting string to float
if item_price_flt<=0:
print("Invalid input. Negative or zero values are not acceptable, please type whole number.")
else:
#allow float to use only 2 decimals
item_price= f"${item_price_flt:.2f}"
break
except ValueError:
print("Invalid value. Please enter a valid price.")
# generate random serial number with initial of the item_name + 4 digit int
while True:
random_serial_value=random.randint(1111,9999)
serial_num=item_name[0].upper()+str(random_serial_value)
serial_num=unique_serial_num(stock_inventory, serial_num)
if serial_num != "":
new_item = [item_name, serial_num, item_qty, item_price]
stock_inventory.append(new_item) # Append the new item as a list
display_header = ["Item Name", "Serial Number", "Quantity", "Price"]
display_inventory = tabulate([new_item], headers=display_header, tablefmt="fancy_grid")
print("\nBelow item is now added to the inventory:")
print(display_inventory+"\n")
return stock_inventory
#verifying if the serial_num generated is unique or not
def unique_serial_num(stock_inventory, serial_num):
if stock_inventory=="":
return serial_num
for each_item in stock_inventory:
if each_item[1]==serial_num:
return""
return serial_num
def application():
stock_inventory=[]
hash_break="#"*100
main_menu=[
["Add:","add item in stock"],
["Display:","display all items from stock"],
["Search:","search item by name in stock"],
["End:","end application"]
]
while True:
#printing main header as per your requirement
print(hash_break)
print(tabulate(main_menu,tablefmt="plain")) #printing the menu using tabulate
print(hash_break)
select_option=(input("\nEnter your choice:\t")) #get user's input from main_menu options
#add option will call the function add_item
if select_option.upper()=="ADD" or select_option.upper()=="A":
stock_inventory=add_item(stock_inventory)
#display all the items in the inventory stock
elif select_option.upper()=="DISPLAY" or select_option.upper()=="D":
#if inventory is empty, print display_error message
if len(stock_inventory)==0:
print("There are no items in stock, please try later...\n")
#if items are present in inventory, display the items in a table format using tabulate
else:
display_header= ["Item Name", "Serial Number", "Quantity","Price"]
display_inventory = tabulate(stock_inventory, headers=display_header, tablefmt="fancy_grid")
print(display_inventory)
#Search functionality based on name of the item
elif select_option.upper()=="SEARCH" or select_option.upper()=="S":
#if inventory is empty, print display_search_error message
if len(stock_inventory)==0:
print("""There are no items in stock \nplease try again, later...\n""")
else:
print("Search item by name......")
search_by_name= input("Enter name of the item to search:\t") #input variable to search by item_name
search_by_name=search_by_name.upper() #convert all input to upper case to remove case-insensitivity
found_item = [each_item for each_item in stock_inventory if search_by_name in each_item[0].upper()]
if found_item:
print("\nSearch results are as below:")
display_header=["Item Name", "Serial Number", "Quantity","Price"]
print(tabulate(found_item, headers=display_header, tablefmt="fancy_grid"))
else:
#serach error to indicate if the item searched was invalid
print(f"The stock does not have {search_by_name}, please try again later...")
#End the application if "End" selected
elif select_option.upper()=="END" or select_option.upper()=="E":
print("Application ending now...\n")
break
else:
print("Enter valid choice.\n")
application()
#***********Code Ends here*********