commit c1006502f280cf08783138042708c1a7fd9eae60 Author: nugroho Date: Fri Apr 10 13:35:37 2026 +0700 First commit diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..016a3dd --- /dev/null +++ b/README.MD @@ -0,0 +1,13 @@ +# Discount Calculator Project + +This project was created as part of a recruitment task for Koding Next. + +It is a python-based GUI app that calculates final price after applying discount. + +The purpose of this project is to demonstrate practical skills in programming, problem solving, and basic web development. + +# Notes +Since the task mentionied quantity as a possible input, but the example showed no such input, I created two versions of the app. +- `discalc.py` includes all three mentioned fields (price, quantity, discount). +- `discalc-noqty.py` is the one without quantity field. +Thank you for your understanding \ No newline at end of file diff --git a/discalc-noqty.py b/discalc-noqty.py new file mode 100644 index 0000000..16b568a --- /dev/null +++ b/discalc-noqty.py @@ -0,0 +1,47 @@ +import tkinter as tk +from tkinter import messagebox + +def calculate(): + try: + price = float(entry_price.get()) + #quantity = int(entry_quantity.get()) + rate = float(entry_rate.get()) + + if price < 0 or rate < 0: #price < 0 or quantity < 0 or rate < 0: + messagebox.showerror("Error", "Invalid input.", detail="Value cannot be negative.") + return + + if rate > 100: + messagebox.showerror("Error", "Invalid input.", detail="Discount rate cannot exceed 100%.") + return + + #subtotal = price * quantity + discount = price * rate / 100 #subtotal * rate / 100 + total = price - discount #subtotal - discount + + result_var.set(f"Total: {total:,.2f}") + + except ValueError: + messagebox.showerror("Error", "Invalid input.", detail="Please enter numeric values. Fractions are allowed for price and discount rate.") + + +root = tk.Tk() +root.title("Discount Calculator") +root.geometry("280x250") +tk.Label(root, text="Price").pack(pady=5) +entry_price = tk.Entry(root, width=20) +entry_price.pack(pady=5) + +# tk.Label(root, text="Quantity").pack(pady=5) +# entry_quantity = tk.Entry(root, width=20) +# entry_quantity.pack(pady=5) +tk.Label(root, text="Discount (%)").pack(pady=5) +entry_rate = tk.Entry(root, width=20) +entry_rate.pack(pady=5) + +tk.Button(root, text="Calculate", command=calculate).pack(pady=10) + +result_var = tk.StringVar() +tk.Label(root, textvariable=result_var).pack(pady=10) + +root.mainloop() diff --git a/discalc.py b/discalc.py new file mode 100755 index 0000000..2eff369 --- /dev/null +++ b/discalc.py @@ -0,0 +1,47 @@ +import tkinter as tk +from tkinter import messagebox + +def calculate(): + try: + price = float(entry_price.get()) + quantity = int(entry_quantity.get()) + rate = float(entry_rate.get()) + + if price < 0 or quantity < 0 or rate < 0: + messagebox.showerror("Error", "Invalid input.", detail="Value cannot be negative.") + return + + if rate > 100: + messagebox.showerror("Error", "Invalid input.", detail="Discount rate cannot exceed 100%.") + return + + subtotal = price * quantity + discount = subtotal * rate / 100 + total = subtotal - discount + + result_var.set(f"Total: {total:,.2f}") + + except ValueError: + messagebox.showerror("Error", "Invalid input.", detail="Please enter numeric values. Fractions are allowed for price and discount rate.") + + +root = tk.Tk() +root.title("Discount Calculator") +root.geometry("280x300") +tk.Label(root, text="Price").pack(pady=5) +entry_price = tk.Entry(root, width=20) +entry_price.pack(pady=5) + +tk.Label(root, text="Quantity").pack(pady=5) +entry_quantity = tk.Entry(root, width=20) +entry_quantity.pack(pady=5) +tk.Label(root, text="Discount (%)").pack(pady=5) +entry_rate = tk.Entry(root, width=20) +entry_rate.pack(pady=5) + +tk.Button(root, text="Calculate", command=calculate).pack(pady=10) + +result_var = tk.StringVar() +tk.Label(root, textvariable=result_var).pack(pady=10) + +root.mainloop()