Implementation
(Pages 12-15: Explain code sections. Include key snippets; full code in Appendix.)
Project Structure
The project is implemented in a single Python file (grocery_management.py) using object-oriented
programming (OOP). It includes a class GroceryManagement for encapsulation.
Key Components
• Database Connection: Uses mysql-connector-python for secure connectivity. Configured via
dictionary to handle credentials.
• GUI Design: Tkinter for windows, Entry widgets for inputs, Buttons for actions, Treeview for
tabular display.
• Input Validation: Checks for numeric values, positive quantities, and sufficient stock using
try-except and if-statements.
Code Explanation
1. Initialization (__init__): Sets up GUI and connects to DB. Loads initial product view.
2. Add Product:
• Validates inputs.
• Executes: INSERT INTO products (name, price, quantity) VALUES (%s, %s,
%s) (parameterized to prevent injection).
• Commits changes and refreshes display.
Snippet:
3. View Products:
• SELECT * FROM products to fetch and populate Treeview.
4. Record Sale:
• Checks stock: SELECT quantity FROM products WHERE item_id = %s.
• If sufficient: INSERT sale, UPDATE stock (quantity = quantity - %s).
• Uses [Link]() for sale_date.
Snippet:
5. Update/Delete: Similar to Add, with WHERE clause and confirmation dialog.
6. View Sales: SELECT with JOIN:
7. Error Handling: Try-except for MySQL errors; messagebox for user feedback.
8. Cleanup: __del__ closes connection.
Syllabus Mapping
• Python: Classes, Functions, Exception Handling (ValueError, Error), Libraries.
• Database: SQL Commands, Keys, Joins.
• Connectivity: Driver-based integration.