From 6a7cab9ba3d393714a9f188b802702d6c65ed62e Mon Sep 17 00:00:00 2001 From: dhyanikindasus Date: Sat, 7 Jun 2025 12:54:45 +0530 Subject: [PATCH] Create test_total_bill.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR improves the Total_Bill() method in the café management system by: Using a price dictionary to reduce repetitive code. Simplifying cost calculations and GUI updates. Adding error handling for invalid or empty inputs. Renaming variables to avoid conflicts and improve clarity. These changes make the billing logic cleaner, safer, and easier to maintain. --- Cafe Management System/test_total_bill.py | 62 +++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Cafe Management System/test_total_bill.py diff --git a/Cafe Management System/test_total_bill.py b/Cafe Management System/test_total_bill.py new file mode 100644 index 0000000..2636909 --- /dev/null +++ b/Cafe Management System/test_total_bill.py @@ -0,0 +1,62 @@ +# ======== A cleaned-up and working version of Total_Bill(), and a checklist of improvements and missing elements: ======= + +# Key Fixes and Recommendations: +# Use try...except instead of checking if get() is an empty string. +# Avoid assigning self.Total_Bill as both a method and a variable (naming conflict). +# Combine repetitive cost calculations using a loop or helper function. +# Ensure all UI elements (self.tea_item, self.items_cost, etc.) are defined before calling Total_Bill(). + + +def Total_Bill(self): + # Item prices + prices = { + "tea": 10, + "coffee": 20, + "sandwich": 50, + "cake": 100, + "burger": 50, + "pizza": 150, + "fries": 80, + "pepsi": 80 + } + + # Calculate individual item costs + try: + self.tea_cost = prices["tea"] * int(self.tea_item.get() or 0) + self.coffee_cost = prices["coffee"] * int(self.coffee_item.get() or 0) + self.sandwich_cost = prices["sandwich"] * int(self.sandwitch_item.get() or 0) + self.cake_cost = prices["cake"] * int(self.cake_item.get() or 0) + self.burger_cost = prices["burger"] * int(self.burger_item.get() or 0) + self.pizza_cost = prices["pizza"] * int(self.pizza_item.get() or 0) + self.fries_cost = prices["fries"] * int(self.fries_item.get() or 0) + self.pepsi_cost = prices["pepsi"] * int(self.pepsi_item.get() or 0) + + total_items_cost = ( + self.tea_cost + self.coffee_cost + self.sandwich_cost + + self.cake_cost + self.burger_cost + self.pizza_cost + + self.fries_cost + self.pepsi_cost + ) + + service_charge = 10.0 + subtotal = total_items_cost + service_charge + tax = subtotal * 0.08 + total = subtotal + tax + + # Update GUI fields + self.items_cost.delete(0, END) + self.items_cost.insert(END, total_items_cost) + + self.service_cost.delete(0, END) + self.service_cost.insert(END, service_charge) + + self.sub_cost.delete(0, END) + self.sub_cost.insert(END, subtotal) + + self.paid_tax.delete(0, END) + self.paid_tax.insert(END, round(tax, 2)) + + self.total_bill.delete(0, END) + self.total_bill.insert(END, round(total, 2)) + + except Exception as e: + messagebox.showerror("Error", f"Something went wrong:\n{e}")