![]() |
|
Inventory System - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: Inventory System (/thread-35121.html) |
Inventory System - mcmxl22 - Sep-30-2021 A command-line inventory system I wrote. The dependencies are here. #!/usr/bin/env python3
"""
Inventory.py
Requires: numli.py, clear_screen.py
Python 3.7
"""
import json
from os import path
from clear_screen import clear_screen
from numli import add_numbers
class Inventory_file:
def create_inventory_file():
"""Create file"""
name = "inventory.json"
with open(name, "w+"):
if path.exists(name):
print(f"{name} created!")
else:
return
def check_inventory_file():
"""Check if file exists"""
if path.exists("inventory.json"):
pass
else:
Inventory_file.create_inventory_file()
def get_data() -> dict:
"""Get data from file."""
with open("inventory.json", "r+") as file:
try:
existing_items = json.load(file)
except ValueError:
existing_items = {}
json.dump(existing_items, file, indent=4)
return existing_items
class Menu:
def list_choices() -> list:
"""Give user a choice of actions."""
inventory_actions = ["Add", "Take", "View", "Delete", "Exit"]
add_numbers(inventory_actions)
action_choice = input("What do you want to do? ")
return action_choice
class Inventory:
def add_inventory() -> str:
"""Add items to inventory."""
item = get_data()
try:
add_item = input("Enter item: ")
quantity = int(input("Enter quantity: "))
if add_item in item:
item[add_item] += quantity
else:
item[add_item] = quantity
except ValueError as e:
print(e)
return item
def delete_item():
"""Delete items from inventory."""
clear_screen()
item = get_data()
try:
if item == {}:
print("Nothing to delete.")
pass
else:
delete = input("Enter item to delete: ")
item.pop(delete)
print(f"{delete} deleted!\n")
with open("inventory.json", "w") as file:
json.dump(item, file, indent=4)
except KeyError as e:
print(e)
return item
def take_items():
"""Take items from inventory."""
item = get_data()
if item == {}:
print("No inventory available!\n")
else:
try:
take = input("What did you take? ")
deduct = int(input(f"How many {take}? "))
if take in item:
item[take] -= deduct
print(f"{item[take]} {take} left!\n")
else:
print(f"{take} doesn't exist.\n")
except ValueError as e:
print(e)
return item
def view_items():
"""View items from inventory."""
clear_screen()
view_items = get_data()
if view_items == {}:
print("No inventory available!\n")
else:
for item, view in view_items.items():
if view < 1:
print("No inventory available!\n")
else:
print(item, "=", view)
def main():
"""main"""
while True:
Inventory_file.check_inventory_file()
choice = Menu.list_choices()
# Process user's choice.
if choice in "1":
# Add items to inventory.
clear_screen()
with open("inventory.json", "r+") as file:
add = Inventory.add_inventory()
json.dump(add, file, indent=4)
elif choice in "2":
# Take items from inventory.
clear_screen()
taken_items = Inventory.take_items()
with open("inventory.json", "w") as file:
json.dump(taken_items, file, indent=4)
elif choice in "3":
Inventory.view_items()
elif choice in "4":
Inventory.delete_item()
else:
clear_screen()
exit(0)
if __name__ == "__main__":
clear_screen()
exit(main())
RE: Inventory System - Yoriz - Sep-30-2021 Where did you learn your unique use of classes? RE: Inventory System - mcmxl22 - Sep-30-2021 (Sep-30-2021, 09:16 PM)Yoriz Wrote: Where did you learn your unique use of classes?I'm still trying to wrap my head around how to use them. This was the best I could come up with for this program. RE: Inventory System - mcmxl22 - Oct-01-2021 An updated version with no dependencies. #!/usr/bin/env python3
"""
Inventory.py
Python 3.7
"""
import json
from os import path, system
from sys import platform
def clear_screen():
"""Clear the screen."""
if platform in "win32":
system("cls")
else:
system("clear")
def get_data() -> dict:
"""Get Json data from file."""
with open("inventory.json", "r+") as file:
try:
existing_items = json.load(file)
except ValueError:
existing_items = {}
json.dump(existing_items, file, indent=4)
return existing_items
class Inventory_file:
"""Check and/or create inventory file."""
def create_inventory_file():
"""Create file"""
with open("inventory.json", "w+"):
if path.exists("inventory.json"):
print(f"inventory.json created!")
else:
return
def check_inventory_file():
"""Check if file exists"""
if path.exists("inventory.json"):
pass
else:
Inventory_file.create_inventory_file()
class Menu:
"""Prepare a menu."""
def add_numbers(num):
"""Add numbers to menu list."""
for c, value in enumerate(num, 1):
print(c, value)
def list_choices() -> list:
"""Give user a choice of actions."""
inventory_actions = ["Add", "Take", "View", "Delete", "Exit"]
Menu.add_numbers(inventory_actions)
action_choice = input("What do you want to do? ")
return action_choice
class Inventory:
"""Add, remove and view inventory."""
def add_inventory() -> str:
"""Add items to inventory."""
item = get_data()
try:
add_item = input("Enter item: ")
quantity = int(input("Enter quantity: "))
if add_item in item:
item[add_item] += quantity
else:
item[add_item] = quantity
except ValueError as e:
print(e)
clear_screen()
return item
def delete_item():
"""Delete items from inventory."""
clear_screen()
item = get_data()
try:
if item == {}:
print("Nothing to delete.")
pass
else:
delete = input("Enter item to delete: ")
item.pop(delete)
print(f"{delete} deleted!\n")
with open("inventory.json", "w") as file:
json.dump(item, file, indent=4)
except KeyError as e:
print(e)
return item
def take_items():
"""Take items from inventory."""
item = get_data()
if item == {}:
print("No inventory available!\n")
else:
try:
take = input("What did you take? ")
deduct = int(input(f"How many {take}? "))
if take in item:
item[take] -= deduct
print(f"{item[take]} {take} left!\n")
else:
print(f"{take} doesn't exist.\n")
except ValueError as e:
print(e)
return item
def view_items():
"""View items from inventory."""
clear_screen()
view_items = get_data()
if view_items == {}:
print("No inventory available!\n")
else:
for item, view in view_items.items():
if view < 1:
print("No inventory available!\n")
else:
print(item, "=", view)
print("\n")
def main():
"""main"""
while True:
Inventory_file.check_inventory_file()
choice = Menu.list_choices()
# Process user's choice.
if choice in "1":
# Add items to inventory.
clear_screen()
with open("inventory.json", "r+") as file:
add = Inventory.add_inventory()
json.dump(add, file, indent=4)
elif choice in "2":
# Take items from inventory.
clear_screen()
taken_items = Inventory.take_items()
with open("inventory.json", "w") as file:
json.dump(taken_items, file, indent=4)
elif choice in "3":
Inventory.view_items()
elif choice in "4":
Inventory.delete_item()
else:
clear_screen()
exit(0)
if __name__ == "__main__":
clear_screen()
exit(main())
|