Advertisement
zukars3

index.php

Mar 30th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.96 KB | None | 0 0
  1. <?php
  2.  
  3. $app = new Control();
  4. $app->loadCages();
  5. $app->loadAnimals();
  6. $run = true;
  7.  
  8. echo 'Welcome!' . PHP_EOL;
  9.  
  10. while ($run !== false) {
  11.  
  12.     echo 'Press 1 to add cage' . PHP_EOL;
  13.     echo 'Press 2 to add animal' . PHP_EOL;
  14.     echo 'Press 3 to see cage info' . PHP_EOL;
  15.     echo 'Press 4 to see animal info' . PHP_EOL;
  16.     echo 'Press 0 to exit' . PHP_EOL;
  17.  
  18.     $input = readline('Your choice!') . PHP_EOL;
  19.     switch ($input) {
  20.         case 1 :
  21.             $app->addCage();
  22.             break;
  23.         case 2:
  24.             $name = readline('Enter name of the animal: ');
  25.             $age = readline('Enter age of the animal: ') . PHP_EOL;
  26.             $weight = readline('Enter weight of the animal: ') . PHP_EOL;
  27.             $app->addAnimal($name, $age, $weight);
  28.             break;
  29.         case 3:
  30.             $id = readline('Enter number of the cage: ') . PHP_EOL;
  31.             echo $app->showCageInfo($id) . PHP_EOL;
  32.             break;
  33.         case 4:
  34.             $name = readline('Enter name of the animal: ');
  35.             echo $app->showAnimalInfo($name) . PHP_EOL;
  36.             break;
  37.         case 0:
  38.             $run = false;
  39.             break;
  40.     }
  41. }
  42.  
  43. class Control
  44. {
  45.     private $cages;
  46.     private $animals;
  47.  
  48.     public function __construct(array $cages = [], array $animals = [])
  49.     {
  50.         $this->cages = $cages;
  51.         $this->animals = $animals;
  52.     }
  53.  
  54.     public function getCages(): array
  55.     {
  56.         return $this->cages;
  57.     }
  58.  
  59.     public function getAnimals(): array
  60.     {
  61.         return $this->animals;
  62.     }
  63.  
  64.     public function loadCages(): void
  65.     {
  66.         $csv_data = array_map('str_getcsv', file('cages.csv'));
  67.         $csv_header = $csv_data[0];
  68.         unset($csv_data[0]);
  69.         foreach ($csv_data as $row) {
  70.             $row = array_combine($csv_header, $row);
  71.             $this->cages[] = new Cage($row['id'], $row['occupied'], new Animal($row['animal'], 4, 25));
  72.         }
  73.     }
  74.  
  75.     public function loadAnimals(): void
  76.     {
  77.         $csv_data = array_map('str_getcsv', file('animals.csv'));
  78.         $csv_header = $csv_data[0];
  79.         unset($csv_data[0]);
  80.         foreach ($csv_data as $row) {
  81.             $row = array_combine($csv_header, $row);
  82.             $this->animals[] = $row;
  83.         }
  84.     }
  85.  
  86.     public function addCage(): void
  87.     {
  88.         $cageCount = count($this->cages);
  89.  
  90.         $file = fopen("cages.csv", "a");
  91.         $cage = new Cage($cageCount + 1);
  92.         $this->cages[$cageCount] = $cage;
  93.  
  94.         $output = [$cage->getId(), $cage->isOccupied(), $cage->getAnimal()];
  95.  
  96.         fputcsv($file, $output);
  97.         fclose($file);
  98.     }
  99.  
  100.     public function occupyCage(): void
  101.     {
  102.         $array = $this->getCages();
  103.  
  104.         $file = fopen("cages.csv", "w");
  105.  
  106.         $header = ['id', 'occupied', 'animal'];
  107.         fputcsv($file, $header);
  108.  
  109.         foreach ($array as $line) {
  110.             fputcsv($file, [$line->getId(), $line->isOccupied(), $line->getAnimal()]);
  111.         }
  112.  
  113.         fclose($file);
  114.  
  115.     }
  116.  
  117.     public function addAnimal(string $name, int $age, int $weight): void
  118.     {
  119.  
  120.         $file = fopen("animals.csv", "a");
  121.         $animal = new Animal($name, $age, $weight);
  122.         $this->animals[$animal->getName()] = $animal;
  123.  
  124.         $output = [$animal->getName(), $animal->getAge(), $animal->getWeight()];
  125.         foreach ($this->cages as $cage) {
  126.             if ($cage->isOccupied() == false) {
  127.                 $cage->changeOccupied();
  128.                 $cage->setAnimal($animal);
  129.             }
  130.         }
  131.  
  132.         $this->occupyCage();
  133.  
  134.         fputcsv($file, $output);
  135.         fclose($file);
  136.     }
  137.  
  138.     public function showCageInfo(int $id): string
  139.     {
  140.         foreach ($this->getCages() as $cage) {
  141.             if ($cage->getId() == $id) {
  142.  
  143.                 if($cage->isOccupied() == 0){
  144.                     $occupied = 'nē';
  145.                 } else $occupied = 'jā';
  146.  
  147.                 if($cage->getAnimal() == NULL){
  148.                     $animal = 'nav';
  149.                 } else $animal = $cage->getAnimal();
  150.  
  151.                 return $cage->getId() . ' | Aizņemts: ' . $occupied . ' | Dzīvnieks: ' . $animal;
  152.             }
  153.         }
  154.         return 'Error';
  155.     }
  156.  
  157.     public function showAnimalInfo(string $name): string
  158.     {
  159.         foreach ($this->getAnimals() as $animal) {
  160.             if ($animal['name'] == $name) {
  161.                 return $animal['name'] . ' | Vecums: ' . $animal['age'] . ' | Svars: ' . $animal['weight'];
  162.             }
  163.         }
  164.         return 'Error';
  165.     }
  166.  
  167. }
  168.  
  169. class Cage
  170. {
  171.     private $id;
  172.     private $occupied;
  173.     private $animal;
  174.  
  175.     public function __construct(int $id, bool $occupied = false, Animal $animal = NULL)
  176.     {
  177.         $this->id = $id;
  178.         $this->occupied = $occupied;
  179.         $this->animal = $animal;
  180.     }
  181.  
  182.     public function getId(): int
  183.     {
  184.         return $this->id;
  185.     }
  186.  
  187.     public function isOccupied(): int
  188.     {
  189.         if ($this->occupied == true) {
  190.             return 1;
  191.         }
  192.         return 0;
  193.     }
  194.  
  195.     public function changeOccupied(): void
  196.     {
  197.         if ($this->occupied == false) {
  198.             $this->occupied = true;
  199.         } else $this->occupied = false;
  200.     }
  201.  
  202.     public function getAnimal(): string
  203.     {
  204.         if ($this->animal !== NULL) {
  205.             return $this->animal->getName();
  206.         }
  207.         return 'NULL';
  208.     }
  209.  
  210.     public function setAnimal(Animal $animal): void
  211.     {
  212.         $this->animal = $animal;
  213.     }
  214.  
  215. }
  216.  
  217. class Animal
  218. {
  219.     private $name;
  220.     private $age;
  221.     private $weight;
  222.  
  223.     public function __construct(string $name, int $age, int $weight)
  224.     {
  225.         $this->name = $name;
  226.         $this->age = $age;
  227.         $this->weight = $weight;
  228.     }
  229.  
  230.     public function getName(): string
  231.     {
  232.         return $this->name;
  233.     }
  234.  
  235.     public function getAge(): int
  236.     {
  237.         return $this->age;
  238.     }
  239.  
  240.     public function getWeight(): int
  241.     {
  242.         return $this->weight;
  243.     }
  244.  
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement