-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
executable file
·197 lines (164 loc) · 7.62 KB
/
db.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python3
import json
import os
import argparse
# JSON file to store the inventory data
class InventoryManager:
def __init__(self, file_name="inventory.json"):
self.file_name = file_name
self.inventory = self.load_inventory()
def load_inventory(self):
"""Load the inventory from the JSON file, or initialize an empty dictionary if the file doesn't exist."""
if os.path.exists(self.file_name):
with open(self.file_name, "r") as file:
return json.load(file)
return {}
def save_inventory(self):
"""Save the inventory to the JSON file."""
with open(self.file_name, "w") as file:
json.dump(self.inventory, file)
def add_item(self):
"""Add or update an item in the inventory."""
item = input("Enter the item name to add/update: ")
quantity = input(f"Enter the quantity for '{item}': ")
if not quantity.isdigit() or int(quantity) <= 0:
print("Invalid quantity. Please enter a positive number.")
return
quantity = int(quantity)
if item in self.inventory:
print(f"Current price for '{item}' is {self.inventory[item]['price']:.2f}.")
price = input(f"Enter the price for '{item}' (or press Enter to keep the current price): ")
if price == "":
price = self.inventory[item]['price']
elif not price.replace('.', '', 1).isdigit() or float(price) <= 0:
print("Invalid price. Please enter a positive number.")
return
else:
price = float(price)
self.inventory[item]["quantity"] += quantity
self.inventory[item]["price"] = price
print(f"Updated '{item}': quantity {self.inventory[item]['quantity']}, price {self.inventory[item]['price']:.2f}.")
else:
price = input(f"Enter the price for '{item}': ")
if not price.replace('.', '', 1).isdigit() or float(price) <= 0:
print("Invalid price. Please enter a positive number.")
return
price = float(price)
self.inventory[item] = {"quantity": quantity, "price": price}
print(f"Added '{item}' with quantity {quantity} and price {price:.2f}.")
self.save_inventory()
def adjust_price(self):
"""Adjust the price of an item in the inventory."""
item = input("Enter the item name to adjust the price: ")
if item in self.inventory:
print(f"Current price for '{item}' is {self.inventory[item]['price']:.2f}.")
new_price = input(f"Enter the new price for '{item}': ")
if not new_price.replace('.', '', 1).isdigit() or float(new_price) <= 0:
print("Invalid price. Please enter a positive number.")
return
new_price = float(new_price)
self.inventory[item]["price"] = new_price
print(f"Updated price for '{item}' is now {new_price:.2f}.")
self.save_inventory()
else:
print(f"'{item}' is not in the inventory.")
def deduct_quantity(self):
"""Deduct a specified quantity from an item in the inventory."""
while True:
item = input("Enter the item name to deduct from (or enter '?' to view the inventory): ")
if item == "?":
self.print_inventory()
else:
if item in self.inventory:
quantity = input(f"Enter the quantity to deduct from '{item}': ")
if not quantity.isdigit() or int(quantity) <= 0:
print("Invalid quantity. Please enter a positive number.")
return
quantity = int(quantity)
if quantity > self.inventory[item]["quantity"]:
print(f"Cannot deduct {quantity}. '{item}' only has {self.inventory[item]['quantity']} in stock.")
else:
self.inventory[item]["quantity"] -= quantity
print(f"Deducted {quantity} from '{item}'. Remaining quantity: {self.inventory[item]['quantity']}.")
if self.inventory[item]["quantity"] == 0:
print(f"'{item}' has been removed from the inventory as its quantity is now 0.")
del self.inventory[item]
self.save_inventory()
break
else:
print(f"'{item}' is not in the inventory.")
def remove_item(self):
"""Remove an item from the inventory, with a warning and confirmation."""
item = input("Enter the item name to remove: ")
if item in self.inventory:
print(f"Warning: You are about to remove '{item}' from the inventory.")
confirm = input("Are you sure? (yes/no): ").strip().lower()
if confirm == "yes":
del self.inventory[item]
self.save_inventory()
print(f"'{item}' has been removed from the inventory.")
else:
print(f"'{item}' was not removed.")
else:
print(f"'{item}' is not in the inventory.")
def print_inventory(self):
"""Print the current inventory in a table format, sorted by item name."""
if self.inventory:
print(f"\n{'Item':<20}{'Quantity':<10}{'Price':<10}{'Total':<10}")
print("-" * 50)
for item, details in sorted(self.inventory.items()):
total = details["quantity"] * details["price"]
print(f"{item:<20}{details['quantity']:<10}{details['price']:<10.2f}{total:<10.2f}")
print("-" * 50)
else:
print("The inventory is empty.")
def find_item(self):
"""Find an item in the inventory and return its quantity, price, and total value."""
item = input("Enter the item name to find: ")
if item in self.inventory:
quantity = self.inventory[item]["quantity"]
price = self.inventory[item]["price"]
total = quantity * price
print(f"'{item}' has quantity {quantity}, price {price:.2f}, and total value {total:.2f}.")
else:
print(f"'{item}' is not in the inventory.")
def display_menu():
"""Display the menu options."""
print("\nMenu:")
print("1. Add an item")
print("2. Remove an item")
print("3. Print the inventory")
print("4. Find an item's details")
print("5. Deduct quantity from an item")
print("6. Adjust the price of an item")
print("7. Exit")
def main():
"""Main function to handle the menu and user choices."""
parser = argparse.ArgumentParser(description="Inventory Management System")
parser.add_argument("-f", "--file", default="inventory.json", help="Path to the JSON inventory file")
args = parser.parse_args()
file_name = args.file
manager = InventoryManager(file_name)
while True:
display_menu()
choice = input("Enter your choice (1-7): ")
if choice == "1":
manager.add_item()
elif choice == "2":
manager.remove_item()
elif choice == "3":
manager.print_inventory()
elif choice == "4":
manager.find_item()
elif choice == "5":
manager.deduct_quantity()
elif choice == "6":
manager.adjust_price()
elif choice == "7":
print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice. Please enter a number between 1 and 7.")
# Run the program
if __name__ == "__main__":
main()