From cbe5bead13fbcbf8ca3b7b5aaf2611c4df916d8a Mon Sep 17 00:00:00 2001 From: AgarwalSugandha <81156180+AgarwalSugandha@users.noreply.github.com> Date: Thu, 27 Feb 2025 16:28:26 +0100 Subject: [PATCH 1/2] Create multiply_service.py --- multiply_service.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 multiply_service.py diff --git a/multiply_service.py b/multiply_service.py new file mode 100644 index 0000000..bea6a94 --- /dev/null +++ b/multiply_service.py @@ -0,0 +1,4 @@ +def multiply(string): + arguments = string.split("*") + result = int(arguments[0]) * int(arguments[1]) + return result From 2f7a65031f4d9004840719ea428431c1db99867d Mon Sep 17 00:00:00 2001 From: AgarwalSugandha <81156180+AgarwalSugandha@users.noreply.github.com> Date: Thu, 27 Feb 2025 16:29:36 +0100 Subject: [PATCH 2/2] Update calculator_application.py --- src/calculator_application.py | 77 +++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 4 deletions(-) diff --git a/src/calculator_application.py b/src/calculator_application.py index c7f944a..c8e4576 100644 --- a/src/calculator_application.py +++ b/src/calculator_application.py @@ -1,6 +1,75 @@ -"""This module starts the application.""" -import calculator_view as calculator_view +"""Module for the CalculationService class""" +from tkinter import StringVar +import divide_service as divide_service +import multiply_service as multiply_service -if __name__ == "__main__": - calculator_view.initialize_calculator() +class CalculationService: + """""Class for keeping track of, and evaluating calculations""""" + + # Constructor for the class + # Initializes a String expression and a StringVar equation + # StringVar is used because it is required for the tkinter text box + def __init__(self): + print("initialized") + self.expression = "" + self.equation = StringVar() + + def press(self, num): + """Function to update the expression in the text entry box""" + # appends the new number to the existing expression + self.expression = self.expression + str(num) + + # update the expression by using set method + self.equation.set(self.expression) + + def equalpress(self): + """Function to evaluate the final expression""" + + # using a try/except in order to catch errors + try: + + # evaluate the expression/calculation + if "/" in self.expression: + total = str(divide_service.divide(self.expression)) + if "*" in self.expression: + total = str(multiply_service.multiply(self.expression)) + else: + total = str(eval(self.expression)) + + + if float(total) > 10: + total = total + " <:o)" + + self.equation.set(total) + + # reset the expression to the empty string + self.expression = "" + + # show error on the screen after an exception is caught + except: + self.equation.set(" error ") + self.expression = "" + + + + def clear(self): + """Function to clear the contents of text entry box""" + self.expression = "" + self.equation.set("") + + def get_expression(self): + """get expression""" + return self.expression + + def set_expression(self, newExpression): + """set expression""" + self.expression = newExpression + + def get_equation(self): + """get equation""" + return self.equation + + def set_equation(self, new_equation): + """set equation""" + self.equation.set(new_equation)