Advertisement
Guest User

Untitled

a guest
May 14th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.41 KB | None | 0 0
  1. <?php
  2.  
  3. class Cars
  4. {
  5.     /**
  6.      * @var string
  7.      */
  8.     private $model;
  9.     /**
  10.      * @var Engine
  11.      */
  12.     private $engine;
  13.     /**
  14.      * @var string
  15.      */
  16.     private $weight;
  17.     /**
  18.      * @var string
  19.      */
  20.     private $color;
  21.  
  22.     /**
  23.      * Cars constructor.
  24.      * @param string $model
  25.      * @param Engine $engine
  26.      * @param string $weight
  27.      * @param string $color
  28.      */
  29.     public function __construct(string $model, Engine $engine, string $weight, string $color)
  30.     {
  31.         $this->model = $model;
  32.         $this->engine = $engine;
  33.         $this->weight = $weight;
  34.         $this->color = $color;
  35.     }
  36.  
  37.     /**
  38.      * @return string
  39.      */
  40.     public function getModel(): string
  41.     {
  42.         return $this->model;
  43.     }
  44.  
  45.     /**
  46.      * @return Engine
  47.      */
  48.     public function getEngine(): Engine
  49.     {
  50.         return $this->engine;
  51.     }
  52.  
  53.     /**
  54.      * @return int
  55.      */
  56.     public function getWeight(): string
  57.     {
  58.         return $this->weight;
  59.     }
  60.  
  61.     /**
  62.      * @return string
  63.      */
  64.     public function getColor(): string
  65.     {
  66.         return $this->color;
  67.     }
  68. public function __toString()
  69. {
  70.     return "{$this->getModel()}:\n  {$this->getEngine()->getModel()}:\n    Power: {$this->getEngine()->getPower()}\n    Displacement: {$this->getEngine()->getDisplacement()}\n    Efficiency: {$this->getEngine()->getEfficiency()}\n  Weight: {$this->getWeight()}\n  Color: {$this->getColor()}\n";
  71. }
  72.  
  73. }
  74.  
  75. class Engine
  76. {
  77.     /**
  78.      * @var string
  79.      */
  80.     private $model;
  81.     /**
  82.      * @var int
  83.      */
  84.     private $power;
  85.     /**
  86.      * @var int
  87.      */
  88.     private $displacement;
  89.     /**
  90.      * @var string
  91.      */
  92.     private $efficiency;
  93.  
  94.     /**
  95.      * Engine constructor.
  96.      * @param string $model
  97.      * @param int $power
  98.      * @param string $displacement
  99.      * @param string $efficiency
  100.      */
  101.     public function __construct(string $model, int $power, string $displacement, string $efficiency)
  102.     {
  103.         $this->model = $model;
  104.         $this->power = $power;
  105.         $this->displacement = $displacement;
  106.         $this->efficiency = $efficiency;
  107.     }
  108.  
  109.     /**
  110.      * @return string
  111.      */
  112.     public function getModel(): string
  113.     {
  114.         return $this->model;
  115.     }
  116.  
  117.     /**
  118.      * @return int
  119.      */
  120.     public function getPower(): int
  121.     {
  122.         return $this->power;
  123.     }
  124.  
  125.     /**
  126.      * @return int
  127.      */
  128.     public function getDisplacement(): string
  129.     {
  130.         return $this->displacement;
  131.     }
  132.  
  133.     /**
  134.      * @return string
  135.      */
  136.     public function getEfficiency(): string
  137.     {
  138.         return $this->efficiency;
  139.     }
  140. }
  141.  
  142. $n = intval(readline());
  143. $engineList = [];
  144. for ($i = 0; $i < $n; $i++) {
  145.     $engineInfo = explode(" ", readline());
  146.     $model = $engineInfo[0];
  147.     $power = intval($engineInfo[1]);
  148.     if (count($engineInfo) == 4) {
  149.         $displacement = $engineInfo[2];
  150.         $efficiency = $engineInfo[3];
  151.     }
  152.     if (count($engineInfo) == 3) {
  153.         if(ctype_digit($engineInfo[2][0])){
  154.             $displacement = $engineInfo[2];
  155.             $efficiency = "n/a";
  156.         }elseif(ctype_alpha($engineInfo[2][0])){
  157.             $displacement = "n/a";
  158.             $efficiency = $engineInfo[2];
  159.         }
  160.     }
  161.     if (count($engineInfo) == 2) {
  162.         $displacement = "n/a";
  163.         $efficiency = "n/a";
  164.     }
  165.     $listEng = new Engine($model, $power, $displacement, $efficiency);
  166.     $engineList[] = $listEng;
  167. }
  168. $m = intval(readline());
  169. $listCar = [];
  170. for ($d = 0; $d < $m; $d++) {
  171.     $carInfo = explode(" ", readline());
  172.     $model = $carInfo[0];
  173.     $engineModel = $carInfo[1];
  174.     $engine = null;
  175.     foreach ($engineList as $item) {
  176.         if ($item->getModel() == $engineModel) {
  177.             $engine = $item;
  178.             break;
  179.         }
  180.     }
  181.     if (count($carInfo) == 4) {
  182.         $weight = $carInfo[2];
  183.         $color = $carInfo[3];
  184.     } elseif (count($carInfo) == 3) {
  185.         if (ctype_digit($carInfo[2][0])) {
  186.             $weight = $carInfo[2];
  187.             $color = "n/a";
  188.         } else {
  189.             $weight = "n/a";
  190.             $color = $carInfo[2];
  191.         }
  192.     }elseif (count($carInfo) == 2) {
  193.         $weight = "n/a";
  194.         $color = "n/a";
  195.     }
  196.     $list = new Cars($model, $engine, $weight, $color);
  197.     $listCar[] = $list;
  198. }
  199. foreach ($listCar as $itemCar){
  200.    echo $itemCar;
  201. }
  202. //print_r($listCar);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement