DARIMI27

13.Speed Racing-php

Sep 4th, 2019
154
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class Car
  4. {
  5.     private $model;
  6.     private $fuelAmount;//налично гориво
  7.     private $fuelCostOneKilometer;
  8.     private $amountOfKilometers;//вход колко км искаме да измине
  9.     private $distanceTraveled;//пропътувани км
  10.  
  11.     public function __construct(string $model, float $fuelAmount, float $fuelCostOneKilometer, float $distanceTraveled = 0,
  12.                                 float $amountOfKilometers = 0)
  13.     {
  14.         $this->model = $model;
  15.         $this->fuelAmount = $fuelAmount;
  16.         $this->fuelCostOneKilometer = $fuelCostOneKilometer;
  17.         $this->distanceTraveled = $distanceTraveled;
  18.         $this->amountOfKilometers = $amountOfKilometers;
  19.     }
  20.  
  21.     public function getModel(): string
  22.     {
  23.         return $this->model;
  24.     }
  25.  
  26.     public function getFuelAmount(): float
  27.     {
  28.         return $this->fuelAmount;
  29.     }
  30.  
  31.     public function getDistanceTraveled(): int
  32.     {
  33.         return $this->distanceTraveled;
  34.     }
  35.  
  36.     public function setAmountOfKilometers($amountOfKilometers): void
  37.     {
  38.         $this->amountOfKilometers = $amountOfKilometers;
  39.     }
  40.  
  41.     public function Cost()
  42.     {
  43.         $fuel = $this->fuelAmount - ($this->amountOfKilometers * $this->fuelCostOneKilometer);
  44.         if ($this->amountOfKilometers * $this->fuelCostOneKilometer <= $this->fuelAmount) {
  45.             $this->distanceTraveled += $this->amountOfKilometers;
  46.             $this->fuelAmount = $fuel;
  47.             return $fuel;
  48.         } else {
  49.             return "Insufficient fuel for the drive" . PHP_EOL;
  50.         }
  51.  
  52.     }
  53. }
  54.  
  55. $n = intval(readline());
  56. //$n = 1;
  57. $cars = [];//hold all cars
  58. while ($n-- > 0) {
  59.     $input = explode(" ", readline());
  60. //    $input = explode(" ", "BMW-M2 45 0.42");
  61.     $model = $input[0];
  62.     $fuelAmount = $input[1];
  63.     $fuelCostOneKilometer = $input[2];
  64.     $car = new Car($model, $fuelAmount, $fuelCostOneKilometer);
  65.     array_push($cars, $car);
  66. }
  67. $input = explode(" ", readline());
  68. //$input = explode(" ", "Drive BMW-M2 56");
  69. while ($input[0] != "End") {
  70.     $carModel = $input[1];
  71.     $amountOfKm = $input[2];
  72.     for ($i = 0; $i < count($cars); $i++) {
  73.         $currentCar = $cars[$i];
  74.         if ($carModel === $currentCar->getModel()) {
  75.             $currentCar->setAmountOfKilometers($amountOfKm);
  76.             if (is_numeric($currentCar->Cost())) {
  77.                 continue;
  78.             } else {
  79.                 $str = $currentCar->Cost();
  80.                 echo $str;
  81.             }
  82.         }
  83.     }
  84.     $input = explode(" ", readline());
  85. //    $input = explode(" ", "End");
  86. }
  87. foreach ($cars as $car) {
  88.     echo $car->getModel() . " " . number_format($car->getFuelAmount(), 2) . " " . $car->getDistanceTraveled() . PHP_EOL;
  89. }
  90. }
RAW Paste Data