Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $app = new Control();
- $app->loadCages();
- $app->loadAnimals();
- $run = true;
- echo 'Welcome!' . PHP_EOL;
- while ($run !== false) {
- echo 'Press 1 to add cage' . PHP_EOL;
- echo 'Press 2 to add animal' . PHP_EOL;
- echo 'Press 3 to see cage info' . PHP_EOL;
- echo 'Press 4 to see animal info' . PHP_EOL;
- echo 'Press 0 to exit' . PHP_EOL;
- $input = readline('Your choice!') . PHP_EOL;
- switch ($input) {
- case 1 :
- $app->addCage();
- break;
- case 2:
- $name = readline('Enter name of the animal: ');
- $age = readline('Enter age of the animal: ') . PHP_EOL;
- $weight = readline('Enter weight of the animal: ') . PHP_EOL;
- $app->addAnimal($name, $age, $weight);
- break;
- case 3:
- $id = readline('Enter number of the cage: ') . PHP_EOL;
- echo $app->showCageInfo($id) . PHP_EOL;
- break;
- case 4:
- $name = readline('Enter name of the animal: ');
- echo $app->showAnimalInfo($name) . PHP_EOL;
- break;
- case 0:
- $run = false;
- break;
- }
- }
- class Control
- {
- private $cages;
- private $animals;
- public function __construct(array $cages = [], array $animals = [])
- {
- $this->cages = $cages;
- $this->animals = $animals;
- }
- public function getCages(): array
- {
- return $this->cages;
- }
- public function getAnimals(): array
- {
- return $this->animals;
- }
- public function loadCages(): void
- {
- $csv_data = array_map('str_getcsv', file('cages.csv'));
- $csv_header = $csv_data[0];
- unset($csv_data[0]);
- foreach ($csv_data as $row) {
- $row = array_combine($csv_header, $row);
- $this->cages[] = new Cage($row['id'], $row['occupied'], new Animal($row['animal'], 4, 25));
- }
- }
- public function loadAnimals(): void
- {
- $csv_data = array_map('str_getcsv', file('animals.csv'));
- $csv_header = $csv_data[0];
- unset($csv_data[0]);
- foreach ($csv_data as $row) {
- $row = array_combine($csv_header, $row);
- $this->animals[] = $row;
- }
- }
- public function addCage(): void
- {
- $cageCount = count($this->cages);
- $file = fopen("cages.csv", "a");
- $cage = new Cage($cageCount + 1);
- $this->cages[$cageCount] = $cage;
- $output = [$cage->getId(), $cage->isOccupied(), $cage->getAnimal()];
- fputcsv($file, $output);
- fclose($file);
- }
- public function occupyCage(): void
- {
- $array = $this->getCages();
- $file = fopen("cages.csv", "w");
- $header = ['id', 'occupied', 'animal'];
- fputcsv($file, $header);
- foreach ($array as $line) {
- fputcsv($file, [$line->getId(), $line->isOccupied(), $line->getAnimal()]);
- }
- fclose($file);
- }
- public function addAnimal(string $name, int $age, int $weight): void
- {
- $file = fopen("animals.csv", "a");
- $animal = new Animal($name, $age, $weight);
- $this->animals[$animal->getName()] = $animal;
- $output = [$animal->getName(), $animal->getAge(), $animal->getWeight()];
- foreach ($this->cages as $cage) {
- if ($cage->isOccupied() == false) {
- $cage->changeOccupied();
- $cage->setAnimal($animal);
- }
- }
- $this->occupyCage();
- fputcsv($file, $output);
- fclose($file);
- }
- public function showCageInfo(int $id): string
- {
- foreach ($this->getCages() as $cage) {
- if ($cage->getId() == $id) {
- if($cage->isOccupied() == 0){
- $occupied = 'nē';
- } else $occupied = 'jā';
- if($cage->getAnimal() == NULL){
- $animal = 'nav';
- } else $animal = $cage->getAnimal();
- return $cage->getId() . ' | Aizņemts: ' . $occupied . ' | Dzīvnieks: ' . $animal;
- }
- }
- return 'Error';
- }
- public function showAnimalInfo(string $name): string
- {
- foreach ($this->getAnimals() as $animal) {
- if ($animal['name'] == $name) {
- return $animal['name'] . ' | Vecums: ' . $animal['age'] . ' | Svars: ' . $animal['weight'];
- }
- }
- return 'Error';
- }
- }
- class Cage
- {
- private $id;
- private $occupied;
- private $animal;
- public function __construct(int $id, bool $occupied = false, Animal $animal = NULL)
- {
- $this->id = $id;
- $this->occupied = $occupied;
- $this->animal = $animal;
- }
- public function getId(): int
- {
- return $this->id;
- }
- public function isOccupied(): int
- {
- if ($this->occupied == true) {
- return 1;
- }
- return 0;
- }
- public function changeOccupied(): void
- {
- if ($this->occupied == false) {
- $this->occupied = true;
- } else $this->occupied = false;
- }
- public function getAnimal(): string
- {
- if ($this->animal !== NULL) {
- return $this->animal->getName();
- }
- return 'NULL';
- }
- public function setAnimal(Animal $animal): void
- {
- $this->animal = $animal;
- }
- }
- class Animal
- {
- private $name;
- private $age;
- private $weight;
- public function __construct(string $name, int $age, int $weight)
- {
- $this->name = $name;
- $this->age = $age;
- $this->weight = $weight;
- }
- public function getName(): string
- {
- return $this->name;
- }
- public function getAge(): int
- {
- return $this->age;
- }
- public function getWeight(): int
- {
- return $this->weight;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement