-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,3 +129,4 @@ dmypy.json | |
.pyre/ | ||
|
||
.idea/ | ||
requirements.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import math | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class Figure(ABC): | ||
@abstractmethod | ||
def get_perimeter(self): | ||
pass | ||
|
||
@abstractmethod | ||
def get_area(self): | ||
pass | ||
|
||
@abstractmethod | ||
def add_area(self, figure): | ||
pass | ||
|
||
|
||
class Triangle(Figure): | ||
name = 'Triangle' | ||
angles = 3 | ||
|
||
def __init__(self, a, b, c): | ||
self.a = a | ||
self.b = b | ||
self.c = c | ||
self.perimeter = self.get_perimeter() | ||
|
||
def get_perimeter(self): | ||
self.perimeter = self.a + self.b + self.c | ||
print('Периметр треугольника') | ||
return self.perimeter | ||
|
||
def get_area(self): | ||
print('Площадь треугольника') | ||
return math.sqrt( | ||
self.perimeter / 2 * (self.perimeter / 2 - self.a) * (self.perimeter / 2 - self.b) * ( | ||
self.perimeter / 2 - self.c)) | ||
|
||
def add_area(self, figure): | ||
if isinstance(figure, (Rectangle, Square, Circle)): | ||
return self.get_area() + figure.get_area() | ||
else: | ||
print('Передан неправильный класс') | ||
|
||
|
||
class Rectangle(Figure): | ||
name = 'Rectangle' | ||
angles = 4 | ||
|
||
def __init__(self, a, b): | ||
self.a = a | ||
self.b = b | ||
|
||
def get_perimeter(self): | ||
print('Периметр прямоугольника') | ||
return (self.a + self.b) * 2 | ||
|
||
def get_area(self): | ||
print('Площадь прямоугольника') | ||
return self.a * self.b | ||
|
||
def add_area(self, figure): | ||
if isinstance(figure, (Triangle, Square, Circle)): | ||
return self.get_area() + figure.get_area() | ||
else: | ||
print('Передан неправильный класс') | ||
|
||
|
||
class Square(Figure): | ||
name = 'Square' | ||
angles = 4 | ||
|
||
def __init__(self, a): | ||
self.a = a | ||
|
||
def get_perimeter(self): | ||
print('Периметр квадрата') | ||
return self.a * 4 | ||
|
||
def get_area(self): | ||
print('Площадь квадрата') | ||
return self.a ** 2 | ||
|
||
def add_area(self, figure): | ||
if isinstance(figure, (Triangle, Rectangle, Circle)): | ||
return self.get_area() + figure.get_area() | ||
else: | ||
print('Передан неправильный класс') | ||
|
||
|
||
class Circle(Figure): | ||
name = 'Circle' | ||
angles = 0 | ||
|
||
def __init__(self, r): | ||
self.r = r | ||
|
||
def get_perimeter(self): | ||
print('Периметр круга') | ||
return 2 * math.pi * self.r | ||
|
||
def get_area(self): | ||
print('Площадь круга') | ||
return math.pi * (self.r ** 2) | ||
|
||
def add_area(self, figure): | ||
if isinstance(figure, (Triangle, Rectangle, Square)): | ||
return self.get_area() + figure.get_area() | ||
else: | ||
print('Передан неправильный класс') | ||
|
||
|
||
|
||
Triangle_1 = Triangle(3, 4, 5) | ||
Rectangle_1 = Rectangle(5, 4) | ||
Square_1 = Square(4) | ||
Circle_1 = Circle(20) | ||
|
||
# abc = Figure() | ||
# print(abc) | ||
# print(Triangle_1.get_area()) | ||
# print(Rectangle_1.get_area()) | ||
# print(Square_1.get_area()) | ||
# print(Circle_1.get_area()) | ||
# | ||
# print(Triangle_1.get_perimeter()) | ||
# print(Rectangle_1.get_perimeter()) | ||
# print(Square_1.get_perimeter()) | ||
# print(Circle_1.get_perimeter()) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import pytest | ||
|
||
import OOP | ||
|
||
|
||
@pytest.fixture | ||
def triangle(): | ||
return OOP.Triangle(3, 4, 5) | ||
|
||
|
||
@pytest.fixture | ||
def rectangle(): | ||
return OOP.Rectangle(5, 4) | ||
|
||
|
||
@pytest.fixture | ||
def square(): | ||
return OOP.Square(4) | ||
|
||
|
||
@pytest.fixture | ||
def circle(): | ||
return OOP.Circle(20) | ||
|
||
|
||
# Тестовый класс проверки инстансов классов | ||
class Testclass_instanse: | ||
def test_inst_triangle(self, triangle): | ||
assert isinstance(triangle, OOP.Figure) | ||
|
||
def test_inst_rectangle(self, rectangle): | ||
assert isinstance(rectangle, OOP.Figure) | ||
|
||
def test_inst_square(self, square): | ||
assert isinstance(square, OOP.Figure) | ||
|
||
def test_inst_circle(self, circle): | ||
assert isinstance(circle, OOP.Figure) | ||
|
||
|
||
# Тестовый класс проверки рассчета периметра фигур | ||
class Testclass_perimeter: | ||
def test_triangle_perimeter(self, triangle): | ||
assert int(triangle.get_perimeter()) == 12 | ||
|
||
def test_rectangle_perimeter(self, rectangle): | ||
assert rectangle.get_perimeter() == 18 | ||
|
||
def test_square_perimeter(self, square): | ||
assert square.get_perimeter() == 16 | ||
|
||
def test_circle_perimeter(self, circle): | ||
assert int(circle.get_perimeter()) == 125 | ||
|
||
|
||
# Тестовый класс проверки рассчета площадей фигур | ||
class Testclass_area: | ||
def test_triangle_area(self, triangle): | ||
assert int(triangle.get_area()) == 6 | ||
|
||
def test_rectangle_area(self, rectangle): | ||
assert rectangle.get_area() == 20 | ||
|
||
def test_square_area(self, square): | ||
assert square.get_area() == 16 | ||
|
||
def test_circle_area(self, circle): | ||
assert int(circle.get_area()) == 1256 | ||
|
||
|
||
# Тестовый класс проверки суммирования площадей фигур | ||
class Testclass_add_area: | ||
def test_triangle_plus_rectangle(self, triangle, rectangle): | ||
assert triangle.add_area(rectangle) == 26 | ||
|
||
def test_triangle_plus_square(self, triangle, square): | ||
assert triangle.add_area(square) == 22 | ||
|
||
def test_triangle_plus_circle(self, triangle, circle): | ||
assert triangle.add_area(circle) == 1262.6370614359173 | ||
|
||
def test_rectangle_plus_triangle(self, rectangle, triangle): | ||
assert rectangle.add_area(triangle) == 26 | ||
|
||
def test_rectangle_plus_square(self, rectangle, square): | ||
assert rectangle.add_area(square) == 36 | ||
|
||
def test_rectangle_plus_circle(self, rectangle, circle): | ||
assert rectangle.add_area(circle) == 1276.6370614359173 | ||
|
||
def test_square_plus_triangle(self, square, triangle): | ||
assert square.add_area(triangle) == 22 | ||
|
||
def test_square_plus_rectangle(self, square, rectangle): | ||
assert square.add_area(rectangle) == 36 | ||
|
||
def test_square_plus_circle(self, square, circle): | ||
assert square.add_area(circle) == 1272.6370614359173 | ||
|
||
def test_circle_plus_triangle(self, circle, triangle): | ||
assert circle.add_area(triangle) == 1262.6370614359173 | ||
|
||
def test_circle_plus_rectangle(self, circle, rectangle): | ||
assert circle.add_area(rectangle) == 1276.6370614359173 | ||
|
||
def test_circle_plus_square(self, circle, square): | ||
assert circle.add_area(square) == 1272.6370614359173 |