Advertisement
Guest User

?

a guest
Mar 10th, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. <?php
  2.  
  3.     class Car
  4.     {
  5.         private $speed;
  6.         private $fuel;
  7.         private $economy;
  8.         private $time = 0;
  9.         private $distance = 0;
  10.  
  11.         function __construct($speed, $fuel, $economy)
  12.         {
  13.             $this->fuel = $fuel;
  14.             $this->speed = $speed;
  15.             $this->economy = $economy;
  16.         }
  17.  
  18.         public function getFuel()
  19.         {
  20.             return "Fuel left: " . number_format($this->fuel, 1) . " liters" . PHP_EOL;
  21.         }
  22.  
  23.         public function getDistance()
  24.         {
  25.             return "Total Distance: " . number_format($this->distance, 1) . PHP_EOL;
  26.         }
  27.  
  28.         public function getTime()
  29.         {
  30.             if ($this->time < 60) {
  31.                 $hours = 0;
  32.                 $minutes = $this->time % 60;
  33.             } else {
  34.                 $hours = $this->time / 60;
  35.                 $minutes = ($this->time - ($hours * 60));
  36.             }
  37.             return "Total Time: " . ($hours) . " hours and " . $minutes . " minutes" . PHP_EOL;
  38.         }
  39.  
  40.         public function travel($distance)
  41.         {
  42.             if ((($distance / 100) * $this->economy) <= $this->fuel) {
  43.                 $fuel = ($distance / 100) * $this->economy;
  44.                 $time = ($distance / $this->speed) * 60;
  45.  
  46.                 $this->fuel -= $fuel;
  47.                 $this->time += $time;
  48.                 $this->distance += $distance;
  49.             } else {
  50.                 $difference = (($distance / 100) * $this->economy) - $this->fuel;
  51.                 //??
  52.             }
  53.         }
  54.  
  55.         public function refuel($value)
  56.         {
  57.             $this->fuel += $value;
  58.         }
  59.     }
  60.  
  61.     $input = explode(' ', trim(fgets(STDIN)));
  62.     $car = new Car($input[0], $input[1], $input[2]);
  63.     $input = explode(' ', trim(fgets(STDIN)));
  64.  
  65.     while ($input[0] != "END") {
  66.         if ($input[0] == "Travel") {
  67.             $car->travel($input[1]);
  68.         } else if ($input[0] == "Refuel") {
  69.             $car->refuel($input[1]);
  70.         } else if ($input[0] == "Distance") {
  71.             echo $car->getDistance();
  72.         } else if ($input[0] == "Time") {
  73.             echo $car->getTime();
  74.         } else if ($input[0] == "Fuel") {
  75.             echo $car->getFuel();
  76.         }
  77.  
  78.         $input = explode(' ', trim(fgets(STDIN)));
  79.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement