Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Car
- {
- private $model;
- private $fuelAmount;//налично гориво
- private $fuelCostOneKilometer;
- private $amountOfKilometers;//вход колко км искаме да измине
- private $distanceTraveled;//пропътувани км
- public function __construct(string $model, float $fuelAmount, float $fuelCostOneKilometer, float $distanceTraveled = 0,
- float $amountOfKilometers = 0)
- {
- $this->model = $model;
- $this->fuelAmount = $fuelAmount;
- $this->fuelCostOneKilometer = $fuelCostOneKilometer;
- $this->distanceTraveled = $distanceTraveled;
- $this->amountOfKilometers = $amountOfKilometers;
- }
- public function getModel(): string
- {
- return $this->model;
- }
- public function getFuelAmount(): float
- {
- return $this->fuelAmount;
- }
- public function getDistanceTraveled(): int
- {
- return $this->distanceTraveled;
- }
- public function setAmountOfKilometers($amountOfKilometers): void
- {
- $this->amountOfKilometers = $amountOfKilometers;
- }
- public function Cost()
- {
- $fuel = $this->fuelAmount - ($this->amountOfKilometers * $this->fuelCostOneKilometer);
- if ($this->amountOfKilometers * $this->fuelCostOneKilometer <= $this->fuelAmount) {
- $this->distanceTraveled += $this->amountOfKilometers;
- $this->fuelAmount = $fuel;
- return $fuel;
- } else {
- return "Insufficient fuel for the drive" . PHP_EOL;
- }
- }
- }
- $n = intval(readline());
- //$n = 1;
- $cars = [];//hold all cars
- while ($n-- > 0) {
- $input = explode(" ", readline());
- // $input = explode(" ", "BMW-M2 45 0.42");
- $model = $input[0];
- $fuelAmount = $input[1];
- $fuelCostOneKilometer = $input[2];
- $car = new Car($model, $fuelAmount, $fuelCostOneKilometer);
- array_push($cars, $car);
- }
- $input = explode(" ", readline());
- //$input = explode(" ", "Drive BMW-M2 56");
- while ($input[0] != "End") {
- $carModel = $input[1];
- $amountOfKm = $input[2];
- for ($i = 0; $i < count($cars); $i++) {
- $currentCar = $cars[$i];
- if ($carModel === $currentCar->getModel()) {
- $currentCar->setAmountOfKilometers($amountOfKm);
- if (is_numeric($currentCar->Cost())) {
- continue;
- } else {
- $str = $currentCar->Cost();
- echo $str;
- }
- }
- }
- $input = explode(" ", readline());
- // $input = explode(" ", "End");
- }
- foreach ($cars as $car) {
- echo $car->getModel() . " " . number_format($car->getFuelAmount(), 2) . " " . $car->getDistanceTraveled() . PHP_EOL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement