ulvis.paste.net - pastebin

Paste Search Dynamic
Recent pastes
CarSalesman
  1. <?php
  2.  
  3. class Car
  4. {
  5.     private $model;
  6.     private $engine;
  7.     private $weight;
  8.     private $color;
  9.  
  10.     public function __construct(string $model, Engine $engine, string $weight, string $color)
  11.     {
  12.         $this->model = $model;
  13.         $this->engine = $engine;
  14.         $this->weight = $weight;
  15.         $this->color = $color;
  16.     }
  17.  
  18.     public function getModel(): string
  19.     {
  20.         return $this->model;
  21.     }
  22.  
  23.     public function getEngine(): Engine
  24.     {
  25.         return $this->engine;
  26.     }
  27.  
  28.     public function getWeight(): string
  29.     {
  30.         return $this->weight;
  31.     }
  32.  
  33.     public function getColor(): string
  34.     {
  35.         return $this->color;
  36.     }
  37.  
  38.     public function __toString(): string
  39.     {
  40.         $output = "";
  41.         $output .= $this->model . ":\n";
  42.         $output .= "  " . $this->engine->getModel() . ":\n";
  43.         $output .= "    Power: " . $this->engine->getPower() . "\n";
  44.         $output .= "    Displacement: " . $this->engine->getDisplacement() . "\n";
  45.         $output .= "    Efficiency: " . $this->engine->getEfficiency() . "\n";
  46.         $output .= "  Weight: " . $this->weight . "\n";
  47.         $output .= "  Color: " . $this->color . "\n";
  48.         return $output;
  49.     }
  50. }
  51.  
  52. class Engine
  53. {
  54.     private $model;
  55.     private $power;
  56.     private $displacement;
  57.     private $efficiency;
  58.  
  59.     public function __construct(string $model, int $power, string $displacement, string $efficiency)
  60.     {
  61.         $this->model = $model;
  62.         $this->power = $power;
  63.         $this->displacement = $displacement;
  64.         $this->efficiency = $efficiency;
  65.     }
  66.  
  67.     public function getModel(): string
  68.     {
  69.         return $this->model;
  70.     }
  71.  
  72.     public function getPower(): int
  73.     {
  74.         return $this->power;
  75.     }
  76.  
  77.     public function getDisplacement(): string
  78.     {
  79.         return $this->displacement;
  80.     }
  81.  
  82.     public function getEfficiency(): string
  83.     {
  84.         return $this->efficiency;
  85.     }
  86. }
  87.  
  88. $carList = [];
  89. $engineList = [];
  90. $n = intval(readline());
  91. while ($n-- > 0) {
  92.     $input = explode(" ", readline());
  93.     $model = $input[0];
  94.     $power = intval($input[1]);
  95.     if (count($input) == 2) {
  96.         $displacement = "n/a";
  97.         $efficency = "n/a";
  98.     } else if (count($input) == 3) {
  99.         if (is_numeric($input[2])) {
  100.             $displacement = $input[2];
  101.             $efficency = "n/a";
  102.         } else {
  103.             $displacement = "n/a";
  104.             $efficency = $input[2];
  105.         }
  106.     } else if (count($input) == 4) {
  107.         $displacement = $input[2];
  108.         $efficency = $input[3];
  109.     }
  110.     $engine = new Engine($model, $power, $displacement, $efficency);
  111.     $engineList[$model] = $engine;
  112. }
  113. $m = intval(readline());
  114. while ($m-- > 0) {
  115.     $input = explode(" ", readline());
  116.     $model = $input[0];
  117.     $engineName = $input[1];
  118.     if (count($input) == 2) {
  119.         $weight = "n/a";
  120.         $color = "n/a";
  121.     } else if (count($input) == 3) {
  122.         if (is_numeric($input[2])) {
  123.             $weight = $input[2];
  124.             $color = "n/a";
  125.         } else {
  126.             $weight = "n/a";
  127.             $color = $input[2];
  128.         }
  129.     } else if (count($input) == 4) {
  130.         $weight = $input[2];
  131.         $color = $input[3];
  132.     }
  133.     if (key_exists($engineName, $engineList)) {
  134.         $currentEngine = $engineList[$engineName];
  135.     }
  136.     $carsCurrent = new Car($model, $currentEngine, $weight, $color);
  137.     $carList[] = $carsCurrent;
  138. }
  139.  
  140. foreach ($carList as $car) {
  141.     // var_dump($car);
  142.     echo $car->__toString();
  143. }
  144. ?>
Parsed in 0.066 seconds