save persistent inventory

This commit is contained in:
2026-09-25 15:10:53 +08:00
parent 4e93e266c1
commit 6eda77cc88
+14 -4
View File
@@ -39,15 +39,24 @@ def generate_report(total_units, failed_attempts):
def load_inventory(): def load_inventory():
try: try:
with open("inventory.txt", "r") as inventory_file: with open("inventory.txt", "r") as inventory_file:
return int(inventory_file.readline().strip()) inventory = int(inventory_file.readline().strip())
history_line = inventory_file.readline().strip()
history = [] if not history_line else [int(value) for value in history_line.split(",")]
return inventory, history
except FileNotFoundError: except FileNotFoundError:
return 0 return 0, []
inventory = load_inventory() def save_inventory(inventory, transaction_history):
with open("inventory.txt", "w") as inventory_file:
inventory_file.write(f"{inventory}\n")
inventory_file.write(",".join(str(quantity) for quantity in transaction_history))
inventory_file.write("\n")
inventory, transaction_history = load_inventory()
total_units = 0 total_units = 0
failed_attempts = 0 failed_attempts = 0
transaction_history = []
print("=== Stock Auditor ===") print("=== Stock Auditor ===")
print("Type 'quit' to exit.\n") print("Type 'quit' to exit.\n")
@@ -56,6 +65,7 @@ while True:
quantity = get_valid_input() quantity = get_valid_input()
if quantity == "quit": if quantity == "quit":
save_inventory(inventory, transaction_history)
break break
tax = calculate_tax(quantity) tax = calculate_tax(quantity)