Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- interface VehiclesIsnterface
- {
- public function Drive(float $distance): void;
- public function reFuel(float $liters): void;
- }
- interface VehiclesFactoryInterface
- {
- public static function create(
- string $type, float $fuelQuantity, float $litersPerKm, float $capacity
- ): Vehicle;
- }
- class VehiclesFactory implements VehiclesFactoryInterface
- {
- public static function create(string $type, float $fuelQuantity, float $litersPerKm, float $capacity): Vehicle
- {
- if (class_exists($type)) {
- return new $type($type, $fuelQuantity, $litersPerKm, $capacity);
- }
- }
- }
- abstract class Vehicle implements VehiclesIsnterface
- {
- /**
- * @var string
- */
- private $type;
- /**
- * @var float
- */
- private $fuelQuantity;
- /**
- * @var float
- */
- private $litersPerKm;
- /**
- * @var float
- */
- private $capacity;
- /**
- * Vehicle constructor.
- * @param string $type
- * @param float $fuelQuantity
- * @param float $litersPerKm
- * @param float $capacity
- */
- protected function __construct(string $type, float $fuelQuantity, float $litersPerKm, float $capacity)
- {
- $this->setType($type);
- $this->setFuelQuantity($fuelQuantity);
- $this->setLitersPerKm($litersPerKm);
- $this->setCapacity($capacity);
- }
- /**
- * @return string
- */
- public function getType(): string
- {
- return $this->type;
- }
- /**
- * @param string $type
- */
- protected function setType(string $type): void
- {
- $this->type = $type;
- }
- /**
- * @return float
- */
- public function getFuelQuantity(): float
- {
- return $this->fuelQuantity;
- }
- /**
- * @param float $fuelQuantity
- */
- protected function setFuelQuantity(float $fuelQuantity): void
- {
- $this->fuelQuantity = $fuelQuantity;
- }
- /**
- * @return float
- */
- public function getLitersPerKm(): float
- {
- return $this->litersPerKm;
- }
- /**
- * @param float $litersPerKm
- */
- protected function setLitersPerKm(float $litersPerKm): void
- {
- $this->litersPerKm = $litersPerKm;
- }
- /**
- * @return float
- */
- public function getCapacity(): float
- {
- return $this->capacity;
- }
- /**
- * @param float $capacity
- */
- protected function setCapacity(float $capacity): void
- {
- $this->capacity = $capacity;
- }
- }
- class Car extends Vehicle
- {
- /**
- * @throws Exception
- */
- /**
- * Car constructor.
- * @param string $type
- * @param float $fuelQuantity
- * @param float $litersPerKm
- * @param float $capacity
- */
- public function __construct(string $type, float $fuelQuantity, float $litersPerKm, float $capacity)
- {
- parent::__construct($type, $fuelQuantity, $litersPerKm, $capacity);
- }
- /**
- * @param float $distance
- * @throws Exception
- */
- public function Drive(float $distance): void
- {
- $consumption = ($this->getLitersPerKm() + 0.9) * $distance;
- if ($consumption <= $this->getFuelQuantity()) {
- $this->setFuelQuantity($this->getFuelQuantity() - $consumption);
- throw new Exception("Car travelled $distance km\n");
- }
- elseif ($this->getFuelQuantity() < 0) {
- throw new Exception("Fuel must be a positive number\n");
- }
- throw new Exception("Car needs refueling\n");
- }
- /**
- * @param float $fuel
- * @throws Exception
- */
- public function reFuel(float $fuel): void
- {
- if ($this->getFuelQuantity() + $fuel > $this->getCapacity()) {
- throw new Exception("Cannot fit fuel in tank\n");
- }
- $this->setFuelQuantity($this->getFuelQuantity() + $fuel);
- }
- public function __toString()
- {
- return "{$this->getType()}: " . number_format($this->getFuelQuantity(), 2, ".", "") . "\n";
- }
- }
- class Truck extends Vehicle
- {
- public function __construct(string $type, float $fuelQuantity, float $litersPerKm, float $capacity)
- {
- parent::__construct($type, $fuelQuantity, $litersPerKm, $capacity);
- }
- /**
- * @param float $distance
- * @throws Exception
- */
- public function Drive(float $distance): void
- {
- $consumption = ($this->getLitersPerKm() + 1.6) * $distance;
- if ($consumption <= $this->getFuelQuantity()) {
- $this->setFuelQuantity($this->getFuelQuantity() - $consumption);
- throw new Exception("Truck travelled $distance km\n");
- }
- elseif ($this->getFuelQuantity() < 0) {
- throw new Exception("Fuel must be a positive number\n");
- }
- throw new Exception("Truck needs refueling\n");
- }
- /**
- * @param float $fuel
- * @throws Exception
- */
- public function reFuel(float $fuel): void
- {
- $consumption = $this->getFuelQuantity() + ($fuel * 0.95);
- $this->setFuelQuantity($consumption);
- }
- public function __toString()
- {
- return "{$this->getType()}: " . number_format($this->getFuelQuantity(), 2, ".", "") . "\n";
- }
- }
- class Bus extends Vehicle
- {
- public function __construct(string $type, float $fuelQuantity, float $litersPerKm, float $capacity)
- {
- parent::__construct($type, $fuelQuantity, $litersPerKm, $capacity);
- }
- /**
- * @param float $distance
- * @throws Exception
- */
- public function DriveEmpty(float $distance): void
- {
- $consumption = ($this->getLitersPerKm()) * $distance;
- if ($consumption <= $this->getFuelQuantity()) {
- $this->setFuelQuantity($this->getFuelQuantity() - $consumption);
- throw new Exception("Bus travelled $distance km\n");
- }
- elseif ($this->getFuelQuantity() < 0) {
- echo "{$this->getFuelQuantity()}\n";
- throw new Exception("Fuel must be a positive number\n");
- }
- throw new Exception("Bus needs refueling\n");
- }
- /**
- * @param float $distance
- * @throws Exception
- */
- public function Drive(float $distance): void
- {
- $consumption = ($this->getLitersPerKm() + 1.4) * $distance;
- if ($consumption <= $this->getFuelQuantity()) {
- $this->setFuelQuantity($this->getFuelQuantity() - $consumption);
- throw new Exception("Bus travelled $distance km\n");
- }
- elseif ($this->getFuelQuantity() < 0) {
- throw new Exception("Fuel must be a positive number\n");
- }
- throw new Exception("Bus needs refueling\n");
- }
- /**
- * @param float $fuel
- * @throws Exception
- */
- public function reFuel(float $fuel): void
- {
- if ($this->getFuelQuantity() + $fuel > $this->getCapacity()) {
- throw new Exception("Cannot fit fuel in tank\n");
- }
- $this->setFuelQuantity($this->getFuelQuantity() + $fuel);
- }
- public function __toString()
- {
- return "{$this->getType()}: " . number_format($this->getFuelQuantity(), 2, ".", "");
- }
- }
- class Main
- {
- public $car;
- private $truck;
- private $bus;
- const PATTERN = '/\s+/';
- public function run()
- {
- $this->readData();
- }
- public function readData()
- {
- /**
- * @var Car $car
- * @var Bus $bus
- * @var Truck $truck
- */
- list($typeCar, $fuelQuantityCar, $litersPerKmCar, $capacityCar) = preg_split(self::PATTERN, readline(), -1, PREG_SPLIT_NO_EMPTY);
- $car = VehiclesFactory::create($typeCar, $fuelQuantityCar, $litersPerKmCar, $capacityCar);
- list($typeTruck, $fuelQuantityTruck, $litersPerKmTruck, $capacityTruck) = preg_split(self::PATTERN, readline(), -1, PREG_SPLIT_NO_EMPTY);
- $truck = VehiclesFactory::create($typeTruck, $fuelQuantityTruck, $litersPerKmTruck, $capacityTruck);
- list($typeBus, $fuelQuantityBus, $litersPerKmBus, $capacityBus) = preg_split(self::PATTERN, readline(), -1, PREG_SPLIT_NO_EMPTY);
- $bus = VehiclesFactory::create($typeBus, $fuelQuantityBus, $litersPerKmBus, $capacityBus);
- $num = intval(readline());
- for ($i = 0; $i < $num; $i++) {
- $line = preg_split(self::PATTERN, readline(), -1, PREG_SPLIT_NO_EMPTY);
- $command = $line[0];
- switch ($command) {
- case"Drive":
- switch ($line[1]) {
- case"Car":
- try {
- $car->Drive($line[2]);
- } catch (Exception $e) {
- echo $e->getMessage();
- }
- break;
- case"Bus":
- try {
- $bus->Drive($line[2]);
- } catch (Exception $e) {
- echo $e->getMessage();
- }
- break;
- case"Truck":
- try {
- $truck->Drive($line[2]);
- } catch (Exception $e) {
- echo $e->getMessage();
- }
- break;
- }
- break;
- case"Refuel":
- switch ($line[1]) {
- case"Car":
- try {
- $car->reFuel($line[2]);
- } catch (Exception $e) {
- echo $e->getMessage();
- }
- break;
- case"Bus":
- try {
- $bus->reFuel($line[2]);
- } catch (Exception $e) {
- echo $e->getMessage();
- }
- break;
- case"Truck":
- try {
- $truck->reFuel($line[2]);
- } catch (Exception $e) {
- echo $e->getMessage();
- }
- break;
- }
- break;
- case"DriveEmpty":
- switch ($line[1]) {
- case"Bus":
- try {
- $bus->DriveEmpty($line[2]);
- } catch (Exception $e) {
- echo $e->getMessage();
- }
- break;
- }
- break;
- }
- }
- echo $car;
- echo $truck;
- echo $bus;
- }
- }
- $main = new Main();
- $main->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement