-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrafficLights.php
79 lines (70 loc) · 1.38 KB
/
trafficLights.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
<?php
/**
* Created by PhpStorm.
* User: Nikolay
* Date: 01.04.2020
* Time: 12:37
*/
class TrafficLights
{
/**
* @var int
*/
private $position;
/**
* @var int
*/
private $time;
/**
* @var int
*/
private $firstIsGreen;
/**
* TrafficLights constructor.
*
* @param $position
* @param $time
* @param bool $firstIsGreen
*/
public function __construct(int $position, int $time, $firstIsGreen = true)
{
$this->position = $position;
$this->time = $time;
$this->firstIsGreen = $firstIsGreen;
}
/**
* @return int
*/
public function getPosition(): int
{
return $this->position;
}
/**
* @param $position
*
* @return bool
*/
public function isTrafficLightsGreen($position): bool
{
if ((floor($position / $this->time) % 2) == 0) {
if ($this->firstIsGreen) {
return true;
}
} else {
if (!$this->firstIsGreen) {
return true;
}
}
return false;
}
/**
* @param $position
*
* @return bool
*/
public function isOnTrafficLights($position): bool
{
return $position >= $this->position
&& $position < ($this->position + $this->time);
}
}