Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Prison
- {
- private $prisoners;
- /**
- * Prisoner constructor.
- * @param array $prisoners
- */
- public function __construct($prisoners = [])
- {
- $this->prisoners = $prisoners;
- }
- /**
- * Voegt een nieuwe prisoner toe aan de lijst.
- * @param Prisoner $prisoner
- */
- public function addPrisoner(Prisoner $prisoner)
- {
- // TODO: Check of de prisoner al in de lijst zit.
- $this->prisoners[] = $prisoner;
- }
- /**
- * Geeft de lijst met prisoners terug.
- * @return array
- */
- public function getAllPrisoners()
- {
- return $this->prisoners;
- }
- /**
- * Geeft een lijst met prisoners terug die ouder zijn dan de opgegeven leeftijd.
- * @param int $age
- * @return mixed
- */
- public function getAllPrisonersOlderThan(int $age)
- {
- return array_filter($this->prisoners, function(Prisoner $prisoner) use ($age) {
- return $prisoner->getAge() > $age;
- });
- }
- /**
- * Geeft een lijst met prisoners terug waarvan de crime hetzelfde is als de opgegeven parameter.
- * @param string $crime
- * @return array
- */
- public function getAllPrisonersByCrime(string $crime)
- {
- return array_filter($this->prisoners, function(Prisoner $prisoner) use($crime) {
- return strtolower($prisoner->getCrime()) === strtolower($crime);
- });
- }
- /**
- * Geeft een lijst terug met prisoners die in solitary zijn/ zitten.
- */
- public function getAllPrisonersInSolitary()
- {
- return array_filter($this->prisoners, function(Prisoner $prisoner) {
- return $prisoner->isInSolitary();
- });
- }
- /**
- * Geeft een lijst terug met alle prisoners ouder dan de opgegeven leeftijd in solitary.
- * @param int $age
- * @return array
- */
- public function getAllPrisonersInSolitaryOlderThan(int $age)
- {
- return array_filter($this->prisoners, function(Prisoner $prisoner) use($age) {
- return $prisoner->isInSolitary() && $prisoner->getAge() > $age;
- });
- }
- /**
- * Geeft de gemiddelde leeftijd van alle prisoners terug.
- * @return int
- */
- public function getAveragePrisonerAge()
- {
- // Verplaats alle array values met de waarde voor de age van een prisoner.
- $prisonerAges = array_map(function (Prisoner $prisoner) {
- return $prisoner->getAge();
- }, $this->prisoners);
- $addedAges = array_sum($prisonerAges);
- $totalPrisonerCount = array_count_values($this->prisoners);
- return ($totalPrisonerCount / $addedAges);
- }
- /**
- * Voegt prisoners toe aan de lijst met prisoners via een array.
- * @param array $prisoners
- */
- public function addPrisonersFromArray(array $prisoners)
- {
- foreach ($prisoners as $prisoner)
- {
- $this->addPrisoner($prisoner);
- }
- }
- /**
- * Verwijdert een prisoner uit de lijst gebaseerd op de naam.
- * @param $prisonerName
- * @return mixed|null
- */
- public function removePrisonerByName(string $prisonerName)
- {
- $foundPrisoner = null;
- foreach ($this->prisoners as $index => $prisoner)
- {
- if ($prisoner->getName() === $prisonerName) {
- $foundPrisoner = $prisoner;
- // Verwijder de prisoner uit de lijst.
- unset($this->prisoners[$index]);
- break;
- }
- }
- return $foundPrisoner;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement