Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Car
- {
- private $speed;
- private $fuel;
- private $economy;
- private $time = 0;
- private $distance = 0;
- function __construct($speed, $fuel, $economy)
- {
- $this->fuel = $fuel;
- $this->speed = $speed;
- $this->economy = $economy;
- }
- public function getFuel()
- {
- return "Fuel left: " . number_format($this->fuel, 1) . " liters" . PHP_EOL;
- }
- public function getDistance()
- {
- return "Total Distance: " . number_format($this->distance, 1) . PHP_EOL;
- }
- public function getTime()
- {
- if ($this->time < 60) {
- $hours = 0;
- $minutes = $this->time % 60;
- } else {
- $hours = $this->time / 60;
- $minutes = ($this->time - ($hours * 60));
- }
- return "Total Time: " . ($hours) . " hours and " . $minutes . " minutes" . PHP_EOL;
- }
- public function travel($distance)
- {
- if ((($distance / 100) * $this->economy) <= $this->fuel) {
- $fuel = ($distance / 100) * $this->economy;
- $time = ($distance / $this->speed) * 60;
- $this->fuel -= $fuel;
- $this->time += $time;
- $this->distance += $distance;
- } else {
- $difference = (($distance / 100) * $this->economy) - $this->fuel;
- //??
- }
- }
- public function refuel($value)
- {
- $this->fuel += $value;
- }
- }
- $input = explode(' ', trim(fgets(STDIN)));
- $car = new Car($input[0], $input[1], $input[2]);
- $input = explode(' ', trim(fgets(STDIN)));
- while ($input[0] != "END") {
- if ($input[0] == "Travel") {
- $car->travel($input[1]);
- } else if ($input[0] == "Refuel") {
- $car->refuel($input[1]);
- } else if ($input[0] == "Distance") {
- echo $car->getDistance();
- } else if ($input[0] == "Time") {
- echo $car->getTime();
- } else if ($input[0] == "Fuel") {
- echo $car->getFuel();
- }
- $input = explode(' ', trim(fgets(STDIN)));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement