Python - Basic POS System: Console and GUI Solutions
Basic POS System:
Develop a simple point-of-sale system for a small business.
From investopedia - A point of sale (POS) is a place where a customer executes the payment for goods or services and where sales taxes may become payable.
Input values:
1. User interacts with the POS system by entering product information (e.g., item name, quantity, price).
2. Users can also add items to the cart, remove items from the cart, and process payments.
Output value:
Visual representation of the POS interface displaying the current transaction details, including items in the cart, total price, and receipt.
Example:
Input values: 1. Add item to cart: - User scans or manually enters the item's barcode. - User specifies the item quantity. - User confirms the addition of the item to the cart. Output value: Visual representation of the POS interface displays the added item with its name, quantity, price per unit, and total price. It also updates the total price of the transaction. Input values: 2. Remove item from cart: - User selects an item from the cart to remove. Output value: Visual representation of the POS interface removes the selected item from the cart and updates the total price of the transaction accordingly. Input values: 3. Process payment: - User selects the payment method (e.g., cash, credit card). - User confirms the payment. Output value: Visual representation of the POS interface displays a receipt with the list of items purchased, their quantities, prices, total price, and payment method. It also provides any change due if the payment method is cash.
Solution 1: Basic Console-Based POS System
Code:
# Solution 1: A basic console-based POS system
# This solution provides a simple text-based POS system where the user can input item details and process a sale.
class Item:
def __init__(self, name, price):
self.name = name # Name of the item
self.price = price # Price of the item
class Cart:
def __init__(self):
self.items = [] # List to store items added to the cart
def add_item(self, item, quantity):
# Adds item to the cart with specified quantity
self.items.append((item, quantity))
def remove_item(self, item_name):
# Removes item from the cart based on item name
self.items = [item for item in self.items if item[0].name != item_name]
def calculate_total(self):
# Calculates the total price of all items in the cart
return sum(item.price * quantity for item, quantity in self.items) # Fixed item.price reference
def display_cart(self):
# Displays the cart details
print("\nItems in Cart:")
for item, quantity in self.items:
print(f"{item.name}: {quantity} x ${item.price:.2f} = ${item.price * quantity:.2f}")
print(f"Total: ${self.calculate_total():.2f}")
class POSSystem:
def __init__(self):
self.cart = Cart() # Initialize the cart
def add_item_to_cart(self):
# Allows user to add an item to the cart
name = input("Enter item name: ")
price = float(input(f"Enter price of {name}: $"))
quantity = int(input(f"Enter quantity of {name}: "))
item = Item(name, price)
self.cart.add_item(item, quantity)
print(f"Added {quantity} x {name} to cart.")
def remove_item_from_cart(self):
# Allows user to remove an item from the cart
item_name = input("Enter item name to remove: ")
self.cart.remove_item(item_name)
print(f"Removed {item_name} from cart.")
def process_payment(self):
# Handles payment processing and generates a receipt
self.cart.display_cart()
payment_method = input("Select payment method (cash/credit): ")
total_amount = self.cart.calculate_total()
print(f"Payment of ${total_amount:.2f} made using {payment_method}. Thank you for your purchase!")
def run(self):
# Main function to run the POS system
while True:
print("\nPOS System")
print("1. Add Item to Cart")
print("2. Remove Item from Cart")
print("3. Process Payment")
print("4. Exit")
choice = input("Choose an option: ")
if choice == '1':
self.add_item_to_cart()
elif choice == '2':
self.remove_item_from_cart()
elif choice == '3':
self.process_payment()
break
elif choice == '4':
print("Exiting POS System.")
break
else:
print("Invalid option. Try again.")
# Run the POS system
pos = POSSystem()
pos.run()
Output:
POS System 1. Add Item to Cart 2. Remove Item from Cart 3. Process Payment 4. Exit Choose an option: 1 Enter item name: Mobile Enter price of Mobile: $400 Enter quantity of Mobile: 1 Added 1 x Mobile to cart. POS System 1. Add Item to Cart 2. Remove Item from Cart 3. Process Payment 4. Exit Choose an option: 1 Enter item name: Laptop Enter price of Laptop: $1000 Enter quantity of Laptop: 1 Added 1 x Laptop to cart. POS System 1. Add Item to Cart 2. Remove Item from Cart 3. Process Payment 4. Exit Choose an option: 2 Enter item name to remove: Laptop Removed Laptop from cart. POS System 1. Add Item to Cart 2. Remove Item from Cart 3. Process Payment 4. Exit Choose an option: 3 Items in Cart: Mobile: 1 x $400.00 = $400.00 Total: $400.00 Select payment method (cash/credit): cash Payment of $400.00 made using cash. Thank you for your purchase!
Explanation:
- Text-based console interface.
- Users input item details manually.
- The system allows adding/removing items and calculating total prices.
- Payments can be processed through console input.
- Displays a simple receipt at the end.
Solution 2: POS System with Tkinter (GUI)
Code:
# Solution 2: A GUI-based POS system using Tkinter
# This solution provides a graphical user interface for the POS system.
import tkinter as tk
from tkinter import messagebox
class POSApp:
def __init__(self, root):
self.root = root
self.root.title("Basic POS System")
# Create cart to store items
self.cart = []
# Create label and entry for item name
self.label_name = tk.Label(root, text="Item Name:")
self.label_name.grid(row=0, column=0)
self.entry_name = tk.Entry(root)
self.entry_name.grid(row=0, column=1)
# Create label and entry for item price
self.label_price = tk.Label(root, text="Price:")
self.label_price.grid(row=1, column=0)
self.entry_price = tk.Entry(root)
self.entry_price.grid(row=1, column=1)
# Create label and entry for item quantity
self.label_quantity = tk.Label(root, text="Quantity:")
self.label_quantity.grid(row=2, column=0)
self.entry_quantity = tk.Entry(root)
self.entry_quantity.grid(row=2, column=1)
# Add item button
self.btn_add = tk.Button(root, text="Add to Cart", command=self.add_to_cart)
self.btn_add.grid(row=3, column=0, columnspan=2)
# Cart display area
self.label_cart = tk.Label(root, text="Cart:")
self.label_cart.grid(row=4, column=0)
self.cart_display = tk.Text(root, height=10, width=40)
self.cart_display.grid(row=5, column=0, columnspan=2)
# Process payment button
self.btn_payment = tk.Button(root, text="Process Payment", command=self.process_payment)
self.btn_payment.grid(row=6, column=0, columnspan=2)
def add_to_cart(self):
# Get item details
name = self.entry_name.get()
price = float(self.entry_price.get())
quantity = int(self.entry_quantity.get())
# Add item to cart
self.cart.append((name, price, quantity))
self.update_cart_display()
# Clear entry fields
self.entry_name.delete(0, tk.END)
self.entry_price.delete(0, tk.END)
self.entry_quantity.delete(0, tk.END)
def update_cart_display(self):
# Update the cart display with the current cart contents
self.cart_display.delete(1.0, tk.END)
total_price = 0
for item in self.cart:
name, price, quantity = item
item_total = price * quantity
self.cart_display.insert(tk.END, f"{name}: {quantity} x ${price:.2f} = ${item_total:.2f}\n")
total_price += item_total
self.cart_display.insert(tk.END, f"\nTotal: ${total_price:.2f}")
def process_payment(self):
# Display payment success message
messagebox.showinfo("Payment", "Payment processed successfully!")
self.cart.clear()
self.update_cart_display()
# Run the GUI application
root = tk.Tk()
app = POSApp(root)
root.mainloop()
Output:
Explanation:
- GUI-based interface using Tkinter.
- Users input item details through text fields.
- The cart is updated and displayed in the GUI dynamically.
- Payment is processed via a button in the interface.
- A message box confirms payment, and the cart is cleared after processing.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/projects/python/python-project-basic-pos-system-for-small-business.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics