Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Car
- {
- private $speed;
- private $fuel;
- private $consumption;
- private $totalDistance;
- public function __construct(float $speed, float $fuel, float $consumption)
- {
- $this->speed = $speed;
- $this->fuel = $fuel;
- $this->consumption = $consumption;
- $this->totalDistance = 0;
- }
- public function getDistance(): float
- {
- return $this->totalDistance;
- }
- public function getFuel(): float
- {
- return $this->fuel;
- }
- public function getTime(): string
- {
- $time = $this->totalDistance / $this->speed;
- $hours = floor($time);
- $minutes = floor(($time - $hours) * 60);
- return "$hours hours and $minutes minutes";
- }
- public function refuel(float $liters) //: void
- {
- $this->fuel += $liters;
- }
- public function travel(float $distance) //: void
- {
- $canTravelDistance = $this->fuel * 100 / $this->consumption;
- if ($distance >= $canTravelDistance) {
- $this->totalDistance += $canTravelDistance;
- $this->fuel = 0;
- } else {
- $this->totalDistance += $distance;
- $fuelSpend = $distance * $this->consumption / 100;
- $this->fuel -= $fuelSpend;
- }
- }
- }
- list($speed, $fuel, $consumption) = array_map('floatval', explode(" ", readline()));
- $car = new Car($speed, $fuel, $consumption);
- $line = readline();
- while ($line != "END") {
- $command = explode(" ", $line);
- switch ($command[0]) {
- case "Travel":
- $distance = floatval($command[1]);
- $car->travel($distance);
- break;
- case "Refuel":
- $liters = floatval($command[1]);
- $car->refuel($liters);
- break;
- case "Distance":
- echo "Total Distance: " . number_format($car->getDistance(), 1, '.', "") . PHP_EOL;
- break;
- case "Time":
- echo "Total Time: " . $car->getTime() . PHP_EOL;
- break;
- case "Fuel":
- echo "Fuel left: " . number_format($car->getFuel(), 1, '.', "") . " liters" . PHP_EOL;
- break;
- }
- $line = readline();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement