diff --git a/src/calculator.py b/src/calculator.py index 5eb03460..59db8c09 100644 --- a/src/calculator.py +++ b/src/calculator.py @@ -10,3 +10,52 @@ # Multiplication: 8 # Division: 2 # +import enum + + +class OperationType(enum.Enum): + SUM = 1 + DIFFERENCE = 2 + MULTIPLICATION = 3 + DIVISION = 4 + + +def sum(a: int | float, b: int | float) -> int | float: + return a + b + + +def difference(a: int | float, b: int | float) -> int | float: + return a - b + + +def multiplication(a: int | float, b: int | float) -> int | float: + return a * b + + +def division(a: int | float, b: int | float) -> int | float: + if b != 0: + return a / b + raise ValueError("Error: Division by zero") + + +def operation(a: int | float, b: int | float, op: OperationType) -> int | float: + if op == OperationType.SUM: + return sum(a, b) + if op == OperationType.DIFFERENCE: + return difference(a, b) + if op == OperationType.MULTIPLICATION: + return multiplication(a, b) + if op == OperationType.DIVISION: + return division(a, b) + + raise ValueError("Invalid operation") + + +if __name__ == "__main__": # pragma: no cover + num1 = float(input("Insert first number: ")) + num2 = float(input("Insert second number: ")) + + print(f"SUM: {operation(num1, num2, OperationType.SUM)}") + print(f"Difference: {operation(num1, num2, OperationType.DIFFERENCE)}") + print(f"Multiplication: {operation(num1, num2, OperationType.MULTIPLICATION)}") + print(f"Division: {operation(num1, num2, OperationType.DIVISION)}") diff --git a/tests/test_calculator.py b/tests/test_calculator.py new file mode 100644 index 00000000..7107e8c6 --- /dev/null +++ b/tests/test_calculator.py @@ -0,0 +1,13 @@ +from src.calculator import operation, OperationType + + +def test_calculator_operations() -> None: + assert operation(4, 2, op=OperationType.SUM) == 6 # SUM + assert operation(4, 2, op=OperationType.DIFFERENCE) == 2 # DIFFERENCE + assert operation(4, 2, op=OperationType.MULTIPLICATION) == 8 # MULTIPLICATION + assert operation(4, 2, op=OperationType.DIVISION) == 2 # DIVISION + try: + operation(4, 0, op=OperationType.DIVISION) + assert False + except ValueError as _: + assert True