-
Notifications
You must be signed in to change notification settings - Fork 0
/
o.php
82 lines (62 loc) · 1.66 KB
/
o.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
final class Rectangle
{
public int $a;
public int $b;
public function __construct(int $a, int $b)
{
$this->a = $a;
$this->b = $b;
}
}
final class Triangle
{
public int $a;
public int $b;
public int $c;
public function __construct(int $a, int $b, int $c)
{
$this->a = $a;
$this->b = $b;
$this->c = $c;
}
}
final class Circle
{
public int $r;
public function __construct(int $r)
{
$this->r = $r;
}
}
final class AreaCalculator
{
public function calculate($shapes): float
{
$area = [];
foreach ($shapes as $shape) {
if (is_a($shape, Rectangle::class)) {
$area[] = $shape->a * $shape->b;
}
if (is_a($shape, Triangle::class)) {
$p = ($shape->a + $shape->b + $shape->c)/2;
$area[] = sqrt($p * ($p - $shape->a) * ($p - $shape->b) * ($p - $shape->c));
}
if (is_a($shape, Circle::class)) {
$area[] = pi() * pow($shape->r, 2);
}
}
return array_sum($area);
}
}
$shapes = [
new Rectangle(2, 3),
new Triangle(2, 3, 4),
new Circle(2),
];
$area = (new AreaCalculator)->calculate($shapes);
/**
* Необходимо изменить весь код таким образом, что при добавлении новой фигуры функция area и код уже используемых
* классов фигур не изенялись
* Классы фигур можно дополнять кодом но нельзя удалять существующий код
*/