Files
inf1103-labs/persistent_auditor.py
T

85 lines
2.3 KiB
Python

def get_valid_input():
global failed_attempts
while True:
user_input = input("Enter stock quantity: ").strip()
if user_input.lower() == "quit":
return "quit"
if not user_input.isdigit():
print("Error: Invalid input. Please enter a non-negative integer.\n")
failed_attempts += 1
continue
quantity = int(user_input)
if quantity < 0:
print("Error: Negative numbers are not allowed.\n")
failed_attempts += 1
continue
return quantity
def process_delivery(current_total, new_value):
return current_total + new_value
def calculate_tax(amount):
return amount * 0.10
def generate_report(total_units, failed_attempts):
print("\n--- Audit Report ---")
print(f"Total Units Processed: {total_units}")
print(f"Failed/Rejected Entries: {failed_attempts}")
def load_inventory():
try:
with open("inventory.txt", "r") as inventory_file:
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:
return 0, []
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
failed_attempts = 0
print("=== Stock Auditor ===")
print("Type 'quit' to exit.\n")
while True:
quantity = get_valid_input()
if quantity == "quit":
save_inventory(inventory, transaction_history)
break
tax = calculate_tax(quantity)
transaction_history.append(quantity)
inventory = process_delivery(inventory, quantity)
total_units = process_delivery(total_units, quantity)
print(f"Stock added. Current inventory: {inventory}")
print(f"Tax on this delivery: ${tax:.2f}\n")
if inventory > 500:
print(f"ALERT: Overstock detected! Inventory ({inventory}) exceeds 500 units.")
break
generate_report(total_units, failed_attempts)