Refactor functions into Calculator class
- Add Calculator class with add and multiply methods - Maintain original functions for compatibility - Update test code to use class interface
This commit is contained in:
27
test.py
27
test.py
@@ -1,3 +1,14 @@
|
|||||||
|
class Calculator:
|
||||||
|
"""A calculator class that provides basic arithmetic operations."""
|
||||||
|
|
||||||
|
def add(self, a, b):
|
||||||
|
"""Return the sum of two numbers."""
|
||||||
|
return a + b
|
||||||
|
|
||||||
|
def multiply(self, a, b):
|
||||||
|
"""Return the product of two numbers."""
|
||||||
|
return a * b
|
||||||
|
|
||||||
def add_two_numbers(a, b):
|
def add_two_numbers(a, b):
|
||||||
"""Return the sum of two numbers."""
|
"""Return the sum of two numbers."""
|
||||||
return a + b
|
return a + b
|
||||||
@@ -7,9 +18,17 @@ def multiply_two_numbers(a, b):
|
|||||||
return a * b
|
return a * b
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Example usage
|
# Example usage with the Calculator class
|
||||||
sum_result = add_two_numbers(5, 3)
|
calc = Calculator()
|
||||||
|
|
||||||
|
# Test addition using Calculator class
|
||||||
|
sum_result = calc.add(5, 3)
|
||||||
print(f"The sum is: {sum_result}")
|
print(f"The sum is: {sum_result}")
|
||||||
|
|
||||||
product_result = multiply_two_numbers(5, 3)
|
# Test multiplication using Calculator class
|
||||||
print(f"The product is: {product_result}")
|
product_result = calc.multiply(5, 3)
|
||||||
|
print(f"The product is: {product_result}")
|
||||||
|
|
||||||
|
# Also test the original functions for compatibility
|
||||||
|
print(f"Function add result: {add_two_numbers(5, 3)}")
|
||||||
|
print(f"Function multiply result: {multiply_two_numbers(5, 3)}")
|
||||||
Reference in New Issue
Block a user