Advertisement
Guest User

CarSalesman

a guest
May 14th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.44 KB | None | 0 0
  1. <?php
  2.  
  3. class Car
  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.      * Car 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. class Engine
  75. {
  76.     /**
  77.      * @var string
  78.      */
  79.     private $model;
  80.     /**
  81.      * @var int
  82.      */
  83.     private $power;
  84.     /**
  85.      * @var string
  86.      */
  87.     private $displacement;
  88.     /**
  89.      * @var string
  90.      */
  91.     private $efficiency;
  92.  
  93.     /**
  94.      * Engine constructor.
  95.      * @param string $model
  96.      * @param string $power
  97.      * @param string $displacement
  98.      * @param string $efficiency
  99.      */
  100.     public function __construct(string $model, string $power, string $displacement, string $efficiency)
  101.     {
  102.         $this->model = $model;
  103.         $this->power = $power;
  104.         $this->displacement = $displacement;
  105.         $this->efficiency = $efficiency;
  106.     }
  107.  
  108.     /**
  109.      * @return string
  110.      */
  111.     public function getModel(): string
  112.     {
  113.         return $this->model;
  114.     }
  115.  
  116.     /**
  117.      * @return int
  118.      */
  119.     public function getPower(): string
  120.     {
  121.         return $this->power;
  122.     }
  123.  
  124.     /**
  125.      * @return int
  126.      */
  127.     public function getDisplacement(): string
  128.     {
  129.         return $this->displacement;
  130.     }
  131.  
  132.     /**
  133.      * @return string
  134.      */
  135.     public function getEfficiency(): string
  136.     {
  137.         return $this->efficiency;
  138.     }
  139. }
  140.  
  141. $n = intval(readline());
  142. $engineList = [];
  143. for ($i = 0; $i < $n; $i++) {
  144.     $engineInfo = explode(" ", readline());
  145.     $model = $engineInfo[0];
  146.     $power = $engineInfo[1];
  147.     if (count($engineInfo) == 4) {
  148.         $displacement = $engineInfo[2];
  149.         $efficiency = $engineInfo[3];
  150.     }
  151.     if (count($engineInfo) == 3) {
  152.         if (ctype_digit($engineInfo[2][0])) {
  153.             $displacement = $engineInfo[2];
  154.             $efficiency = "n/a";
  155.         } elseif (ctype_alpha($engineInfo[2][0])) {
  156.             $displacement = "n/a";
  157.             $efficiency = $engineInfo[2];
  158.         }
  159.     }
  160.     if (count($engineInfo) == 2) {
  161.         $displacement = "n/a";
  162.         $efficiency = "n/a";
  163.     }
  164.     $listEng = new Engine($model, $power, $displacement, $efficiency);
  165.     $engineList[] = $listEng;
  166. }
  167. $m = intval(readline());
  168. $listCar = [];
  169. for ($d = 0; $d < $m; $d++) {
  170.     $carInfo = explode(" ", readline());
  171.     $model = $carInfo[0];
  172.     $engineModel = $carInfo[1];
  173.     $engine = null;
  174.     foreach ($engineList as $item) {
  175.         if ($item->getModel() == $engineModel) {
  176.             $engine = $item;
  177.             break;
  178.         }
  179.     }
  180.     if (count($carInfo) == 4) {
  181.         $weight = $carInfo[2];
  182.         $color = $carInfo[3];
  183.     } elseif (count($carInfo) == 3) {
  184.         if (ctype_digit($carInfo[2][0])) {
  185.             $weight = $carInfo[2];
  186.             $color = "n/a";
  187.         } else {
  188.             $weight = "n/a";
  189.             $color = $carInfo[2];
  190.         }
  191.     } elseif (count($carInfo) == 2) {
  192.         $weight = "n/a";
  193.         $color = "n/a";
  194.     }
  195.     $list = new Car($model, $engine, $weight, $color);
  196.     $listCar[] = $list;
  197. }
  198. foreach ($listCar as $itemCar) {
  199.     echo $itemCar;
  200. }
  201. //print_r($listCar);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement