diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..3989e56 Binary files /dev/null and b/.DS_Store differ diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..d327c04 --- /dev/null +++ b/circle.py @@ -0,0 +1,25 @@ +from math import pi + +#Defining Circle class +class Circle: + def __init__(self, radius: float): + self.radius = radius + + #Calculating perimeter of circle + def perimeter(self): + return 2 * pi * self.radius + + #Calculating area of circle + def area(self): + return pi * self.radius ** 2 + + #Defining calculation function based on selected operation + def perform_cir_calc(self, operation: str) -> float: + if operation == 'perimeter': + return f'This circle has a perimeter of {round(self.perimeter(), 2)} cm.' + elif operation == 'area': + return f'This circle has an area of {round(self.area(), 2)} cm.' + +#Converting radius input into float +def convert_radius_to_float(radius: str) -> float: + return float(radius) diff --git a/flask_app.py b/flask_app.py index 8897fa2..1f9afc7 100644 --- a/flask_app.py +++ b/flask_app.py @@ -1,11 +1,13 @@ from flask import Flask, render_template, request - from helper import perform_calculation, convert_to_float +from circle import Circle, convert_radius_to_float app = Flask(__name__) # create the instance of the flask class @app.route('/') + + @app.route('/home') def home(): return render_template('home.html') @@ -38,3 +40,25 @@ def calculate(): return render_template('calculator.html', printed_result="You cannot divide by zero") return render_template('calculator.html') + + +@app.route('/circle', methods=['GET', 'POST']) +def circle(): + if request.method == 'POST': + radius = request.form['radius'] + operation = request.form['operation'] + + try: + radius = convert_radius_to_float(radius=radius) + except ValueError: + return render_template('circle.html', printed_result="Radius must be a number") + + if radius <= 0: + return render_template('circle.html', printed_result="Radius must be a positive number") + + circle_instance = Circle(radius) + result = circle_instance.perform_cir_calc(operation=operation) + + return render_template('circle.html', printed_result=result) + + return render_template('circle.html') diff --git a/templates/circle.html b/templates/circle.html new file mode 100644 index 0000000..2b11445 --- /dev/null +++ b/templates/circle.html @@ -0,0 +1,20 @@ +{% extends 'layout.html' %} +{% block content %} +

Circle Calculator

+
+ + + + + + +
+ +
+ + {{ printed_result }} + +{% endblock %} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..23b0193 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -12,6 +12,7 @@

Home

diff --git a/test_circle.py b/test_circle.py new file mode 100644 index 0000000..97d0382 --- /dev/null +++ b/test_circle.py @@ -0,0 +1,24 @@ + +#Testing both methods in circle.py +import pytest +from math import pi +from circle import Circle + +# Tests for perimeter calculation with positive float input +def test_perimeter(): + + #creating instance for radius = 2 + circle_instance = Circle(radius = 2) + expected_perim_result = f'This circle has a perimeter of {round(2 * pi * 2, 2)} cm.' + actual_perim_result = circle_instance.perform_cir_calc('perimeter') + assert actual_perim_result == expected_perim_result, "Perimeter calculation test failed!" + + +# Tests for area calculation +def test_area(): + + #creating instance for radius = 2 + circle_instance = Circle(radius = 2) + expected_area_result = f'This circle has an area of {round(pi * 2 ** 2, 2)} cm.' + actual_area_result = circle_instance.perform_cir_calc('area') + assert actual_area_result == expected_area_result, "Area calculation test failed!" \ No newline at end of file