0% found this document useful (0 votes)
8 views

python3

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

python3

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

unit_price = {}

description = {}
stock = {}
cart = []
total_cost = 0

# Load inventory from file


try:
with open("stock.txt", "r") as file:
no_items = int(file.readline().strip())
for _ in range(no_items):
part, price = file.readline().strip().split("#")
unit_price[int(part)] = float(price)
for _ in range(no_items):
part, desc = file.readline().strip().split("#")
description[int(part)] = desc
for _ in range(no_items):
part, qty = file.readline().strip().split("#")
stock[int(part)] = int(qty)
except FileNotFoundError:
print("Stock file not found. Starting with an empty inventory.")

# Display menu
def show_menu():
print("\nOptions:")
print("A - Add an item")
print("E - Edit an item")
print("R - Remove an item")
print("L - List all items")
print("I - Inquire about an item")
print("P - Purchase")
print("C - Checkout")
print("S - Show cart")
print("Q - Quit\n")

# List all items


def list_items():
print("\nInventory:")
for part in unit_price:
print(f"Part {part}: {description[part]}, Price: ${unit_price[part]},
Stock: {stock[part]}")
print()

# Main program loop


show_menu()
while True:
command = input("Enter command: ").strip().lower()

if command == "q": # Quit


break

elif command == "a": # Add item


try:
part = int(input("Part number: "))
price = float(input("Price: "))
desc = input("Description: ").strip()
qty = max(0, int(input("Stock: ")))
unit_price[part] = price
description[part] = desc
stock[part] = qty
print("Item added successfully.")
except ValueError:
print("Invalid input. Try again.")

elif command == "e": # Edit item


try:
part = int(input("Part number to edit: "))
if part in unit_price:
price = float(input("New price: "))
desc = input("New description: ").strip()
qty = max(0, int(input("New stock: ")))
unit_price[part] = price
description[part] = desc
stock[part] = qty
print("Item updated successfully.")
else:
print("Part not found.")
except ValueError:
print("Invalid input. Try again.")

elif command == "r": # Remove item


try:
part = int(input("Part number to remove: "))
if part in unit_price:
del unit_price[part]
del description[part]
del stock[part]
print("Item removed successfully.")
else:
print("Part not found.")
except ValueError:
print("Invalid input. Try again.")

elif command == "l": # List items


list_items()

elif command == "i": # Inquire about an item


try:
part = int(input("Part number to inquire: "))
if part in unit_price:
print(f"Part {part}: {description[part]}, Price: $
{unit_price[part]}, Stock: {stock[part]}")
if stock[part] < 3:
print("Only a few left! Hurry!")
else:
print("Part not found.")
except ValueError:
print("Invalid input. Try again.")

elif command == "p": # Purchase


try:
part = int(input("Part number to purchase: "))
if part in unit_price and stock[part] > 0:
stock[part] -= 1
cart.append(part)
total_cost += unit_price[part]
print(f"{description[part]} added to cart for $
{unit_price[part]}.")
else:
print("Part not available or out of stock.")
except ValueError:
print("Invalid input. Try again.")

elif command == "c": # Checkout


print("\nCart:")
for part in cart:
print(f"Part {part}: {description[part]}, Price: ${unit_price[part]}")
print(f"Subtotal: ${total_cost:.2f}")
tax = total_cost * 0.13
print(f"Tax (13%): ${tax:.2f}")
print(f"Total: ${total_cost + tax:.2f}")
cart.clear()
total_cost = 0
print("Checkout complete.")

elif command == "s": # Show cart


print("\nCart:")
for part in cart:
print(f"Part {part}: {description[part]}, Price: ${unit_price[part]}")
print(f"Total: ${total_cost:.2f}")

else:
print("Invalid command. Type 'help' for options.")

# Save inventory to file


try:
with open("stock.txt", "w") as file:
file.write(f"{len(unit_price)}\n")
for part in unit_price:
file.write(f"{part}#{unit_price[part]}\n")
for part in description:
file.write(f"{part}#{description[part]}\n")
for part in stock:
file.write(f"{part}#{stock[part]}\n")
except Exception as e:
print("Error saving inventory:", e)

print("Thank you for using our system!")

You might also like