-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPark.php
More file actions
75 lines (66 loc) · 1.95 KB
/
Park.php
File metadata and controls
75 lines (66 loc) · 1.95 KB
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
<?php
//Класс Парк
class Park
{
private $vehicles = array(); //массив ТС
private int $capacity = 0; //вместительность парка
//конструктор с параметрами
function __construct(int $capacity) {
$this->capacity=$capacity;
}
//присвоение значений полям класса
public function __set($property, $value)
{
try {
$this->$property = $value;
}
catch (Exception $e) {
echo 'Исключение: ', $e->getMessage(), "\n";
}
}
//добавить ТС в парк
public function addVehicle(Vehicle $v): bool {
if (count($this->vehicles)<$this->capacity){
$this->vehicles[]=$v;
return true;
}
else {
return false;
}
}
//получить самое дорогое ТС
public function getExpensive(): Vehicle {
$mostExp = $this->vehicles[0];
foreach ($this->vehicles as $vehicle)
{
if ($vehicle->getPrice()>$mostExp->getPrice())
$mostExp = $vehicle;
}
return $mostExp;
}
//получить суммарную стоимость ТС
public function getSumCost(): float {
$sum = 0;
foreach ($this->vehicles as $vehicle)
{
$sum += $vehicle->getPrice();
}
return $sum;
}
//получить список авто в парке
public function getAllCars(): array {
$cars = array();
foreach ($this->vehicles as $vehicle)
{
if (get_class($vehicle)=="Car")
$cars[]=$vehicle;
}
return $cars;
}
//получить среднюю стоимость ТС
public function getMidCost(): float {
$midCost = $this->getSumCost()/count($this->vehicles);
return $midCost;
}
}
?>