Inventory Management System

Chnadrashekhar, once run this code in ur machine and ping me for any error rectifications.
mail@pastecode.io avatar
unknown
python
a month ago
6.4 kB
5
Indexable
Never
import tkinter as tk
from tkinter import messagebox

class Inventory:
    def __init__(self):
        self.items = {}
        self.cart = {}

    def add_item(self, item_name, quantity, price):
        if item_name in self.items:
            self.items[item_name]['quantity'] += quantity
            self.items[item_name]['price'] = price
        else:
            self.items[item_name] = {'quantity': quantity, 'price': price}

    def update_item(self, item_name, quantity, price):
        if item_name in self.items:
            self.items[item_name]['quantity'] = quantity
            self.items[item_name]['price'] = price
        else:
            raise ValueError(f"Item '{item_name}' not found in inventory.")

    def get_inventory(self):
        return self.items

    def add_to_cart(self, item_name, quantity):
        if item_name in self.items and self.items[item_name]['quantity'] >= quantity:
            if item_name in self.cart:
                self.cart[item_name] += quantity
            else:
                self.cart[item_name] = quantity
            self.items[item_name]['quantity'] -= quantity
        else:
            raise ValueError(f"Insufficient quantity of '{item_name}' in inventory.")

    def get_cart(self):
        return self.cart

    def purchase(self):
        total = 0
        for item_name, quantity in self.cart.items():
            total += quantity * self.items[item_name]['price']
        self.cart.clear()
        return total

class InventoryGUI:
    def __init__(self, root, inventory):
        self.root = root
        self.inventory = inventory

        self.root.title("Inventory Management System")

        self.item_name_label = tk.Label(root, text="Item Name")
        self.item_name_label.grid(row=0, column=0)

        self.item_name_entry = tk.Entry(root)
        self.item_name_entry.grid(row=0, column=1)

        self.quantity_label = tk.Label(root, text="Quantity")
        self.quantity_label.grid(row=1, column=0)

        self.quantity_entry = tk.Entry(root)
        self.quantity_entry.grid(row=1, column=1)

        self.price_label = tk.Label(root, text="Price")
        self.price_label.grid(row=2, column=0)

        self.price_entry = tk.Entry(root)
        self.price_entry.grid(row=2, column=1)

        self.add_button = tk.Button(root, text="Add Item", command=self.add_item)
        self.add_button.grid(row=3, column=0)

        self.update_button = tk.Button(root, text="Update Item", command=self.update_item)
        self.update_button.grid(row=3, column=1)

        self.view_button = tk.Button(root, text="View Inventory", command=self.view_inventory)
        self.view_button.grid(row=4, column=0, columnspan=2)

        self.cart_button = tk.Button(root, text="Open Cart", command=self.open_cart_window)
        self.cart_button.grid(row=5, column=0, columnspan=2)

        self.output_text = tk.Text(root, height=10, width=50)
        self.output_text.grid(row=6, column=0, columnspan=2)

    def add_item(self):
        item_name = self.item_name_entry.get()
        quantity = int(self.quantity_entry.get())
        price = float(self.price_entry.get())

        self.inventory.add_item(item_name, quantity, price)
        messagebox.showinfo("Success", "Item added successfully!")
        self.view_inventory()

    def update_item(self):
        item_name = self.item_name_entry.get()
        quantity = int(self.quantity_entry.get())
        price = float(self.price_entry.get())

        try:
            self.inventory.update_item(item_name, quantity, price)
            messagebox.showinfo("Success", "Item updated successfully!")
        except ValueError as e:
            messagebox.showerror("Error", str(e))
        self.view_inventory()

    def view_inventory(self):
        self.output_text.delete(1.0, tk.END)
        inventory = self.inventory.get_inventory()
        for item_name, details in inventory.items():
            self.output_text.insert(tk.END, f"Item: {item_name}, Quantity: {details['quantity']}, Price: {details['price']}\n")

    def open_cart_window(self):
        cart_window = tk.Toplevel(self.root)
        cart_window.title("Shopping Cart")
        CartGUI(cart_window, self.inventory)

class CartGUI:
    def __init__(self, root, inventory):
        self.root = root
        self.inventory = inventory

        self.item_name_label = tk.Label(root, text="Item Name")
        self.item_name_label.grid(row=0, column=0)

        self.item_name_entry = tk.Entry(root)
        self.item_name_entry.grid(row=0, column=1)

        self.quantity_label = tk.Label(root, text="Quantity")
        self.quantity_label.grid(row=1, column=0)

        self.quantity_entry = tk.Entry(root)
        self.quantity_entry.grid(row=1, column=1)

        self.add_to_cart_button = tk.Button(root, text="Add to Cart", command=self.add_to_cart)
        self.add_to_cart_button.grid(row=2, column=0)

        self.view_cart_button = tk.Button(root, text="View Cart", command=self.view_cart)
        self.view_cart_button.grid(row=2, column=1)

        self.purchase_button = tk.Button(root, text="Purchase", command=self.purchase)
        self.purchase_button.grid(row=3, column=0, columnspan=2)

        self.output_text = tk.Text(root, height=10, width=50)
        self.output_text.grid(row=4, column=0, columnspan=2)

    def add_to_cart(self):
        item_name = self.item_name_entry.get()
        quantity = int(self.quantity_entry.get())

        try:
            self.inventory.add_to_cart(item_name, quantity)
            messagebox.showinfo("Success", "Item added to cart!")
        except ValueError as e:
            messagebox.showerror("Error", str(e))
        self.view_cart()

    def view_cart(self):
        self.output_text.delete(1.0, tk.END)
        cart = self.inventory.get_cart()
        for item_name, quantity in cart.items():
            self.output_text.insert(tk.END, f"Item: {item_name}, Quantity: {quantity}\n")

    def purchase(self):
        total = self.inventory.purchase()
        messagebox.showinfo("Purchase Complete", f"Total amount payable: ${total:.2f}")
        self.view_cart()

if __name__ == "__main__":
    root = tk.Tk()
    inventory = Inventory()
    app = InventoryGUI(root, inventory)
    root.mainloop()