Advertisement
Guest User

Untitled

a guest
Jun 18th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.09 KB | None | 0 0
  1. <?php
  2.  
  3. interface VehiclesIsnterface
  4. {
  5. public function Drive(float $distance): void;
  6.  
  7. public function reFuel(float $liters): void;
  8. }
  9.  
  10. interface VehiclesFactoryInterface
  11. {
  12. public static function create(
  13. string $type, float $fuelQuantity, float $litersPerKm, float $capacity
  14. ): Vehicle;
  15. }
  16.  
  17. class VehiclesFactory implements VehiclesFactoryInterface
  18. {
  19.  
  20. public static function create(string $type, float $fuelQuantity, float $litersPerKm, float $capacity): Vehicle
  21. {
  22. if (class_exists($type)) {
  23. return new $type($type, $fuelQuantity, $litersPerKm, $capacity);
  24. }
  25. }
  26. }
  27.  
  28. abstract class Vehicle implements VehiclesIsnterface
  29. {
  30. /**
  31. * @var string
  32. */
  33. private $type;
  34. /**
  35. * @var float
  36. */
  37. private $fuelQuantity;
  38. /**
  39. * @var float
  40. */
  41. private $litersPerKm;
  42. /**
  43. * @var float
  44. */
  45. private $capacity;
  46.  
  47. /**
  48. * Vehicle constructor.
  49. * @param string $type
  50. * @param float $fuelQuantity
  51. * @param float $litersPerKm
  52. * @param float $capacity
  53. */
  54. protected function __construct(string $type, float $fuelQuantity, float $litersPerKm, float $capacity)
  55. {
  56. $this->setType($type);
  57. $this->setFuelQuantity($fuelQuantity);
  58. $this->setLitersPerKm($litersPerKm);
  59. $this->setCapacity($capacity);
  60. }
  61.  
  62. /**
  63. * @return string
  64. */
  65. public function getType(): string
  66. {
  67. return $this->type;
  68. }
  69.  
  70. /**
  71. * @param string $type
  72. */
  73. protected function setType(string $type): void
  74. {
  75. $this->type = $type;
  76. }
  77.  
  78. /**
  79. * @return float
  80. */
  81. public function getFuelQuantity(): float
  82. {
  83. return $this->fuelQuantity;
  84. }
  85.  
  86. /**
  87. * @param float $fuelQuantity
  88. */
  89. protected function setFuelQuantity(float $fuelQuantity): void
  90. {
  91. $this->fuelQuantity = $fuelQuantity;
  92. }
  93.  
  94. /**
  95. * @return float
  96. */
  97. public function getLitersPerKm(): float
  98. {
  99. return $this->litersPerKm;
  100. }
  101.  
  102. /**
  103. * @param float $litersPerKm
  104. */
  105. protected function setLitersPerKm(float $litersPerKm): void
  106. {
  107. $this->litersPerKm = $litersPerKm;
  108. }
  109.  
  110. /**
  111. * @return float
  112. */
  113. public function getCapacity(): float
  114. {
  115. return $this->capacity;
  116. }
  117.  
  118. /**
  119. * @param float $capacity
  120. */
  121. protected function setCapacity(float $capacity): void
  122. {
  123. $this->capacity = $capacity;
  124. }
  125.  
  126. }
  127.  
  128. class Car extends Vehicle
  129. {
  130. /**
  131. * @throws Exception
  132. */
  133. /**
  134. * Car constructor.
  135. * @param string $type
  136. * @param float $fuelQuantity
  137. * @param float $litersPerKm
  138. * @param float $capacity
  139. */
  140. public function __construct(string $type, float $fuelQuantity, float $litersPerKm, float $capacity)
  141. {
  142. parent::__construct($type, $fuelQuantity, $litersPerKm, $capacity);
  143. }
  144.  
  145. /**
  146. * @param float $distance
  147. * @throws Exception
  148. */
  149. public function Drive(float $distance): void
  150. {
  151. $consumption = ($this->getLitersPerKm() + 0.9) * $distance;
  152. if ($consumption <= $this->getFuelQuantity()) {
  153. $this->setFuelQuantity($this->getFuelQuantity() - $consumption);
  154. throw new Exception("Car travelled $distance km\n");
  155. }
  156. elseif ($this->getFuelQuantity() < 0) {
  157. throw new Exception("Fuel must be a positive number\n");
  158. }
  159. throw new Exception("Car needs refueling\n");
  160. }
  161.  
  162. /**
  163. * @param float $fuel
  164. * @throws Exception
  165. */
  166. public function reFuel(float $fuel): void
  167. {
  168. if ($this->getFuelQuantity() + $fuel > $this->getCapacity()) {
  169. throw new Exception("Cannot fit fuel in tank\n");
  170. }
  171. $this->setFuelQuantity($this->getFuelQuantity() + $fuel);
  172. }
  173.  
  174. public function __toString()
  175. {
  176. return "{$this->getType()}: " . number_format($this->getFuelQuantity(), 2, ".", "") . "\n";
  177. }
  178. }
  179.  
  180. class Truck extends Vehicle
  181. {
  182. public function __construct(string $type, float $fuelQuantity, float $litersPerKm, float $capacity)
  183. {
  184. parent::__construct($type, $fuelQuantity, $litersPerKm, $capacity);
  185. }
  186.  
  187. /**
  188. * @param float $distance
  189. * @throws Exception
  190. */
  191. public function Drive(float $distance): void
  192. {
  193. $consumption = ($this->getLitersPerKm() + 1.6) * $distance;
  194. if ($consumption <= $this->getFuelQuantity()) {
  195. $this->setFuelQuantity($this->getFuelQuantity() - $consumption);
  196. throw new Exception("Truck travelled $distance km\n");
  197. }
  198. elseif ($this->getFuelQuantity() < 0) {
  199. throw new Exception("Fuel must be a positive number\n");
  200. }
  201. throw new Exception("Truck needs refueling\n");
  202.  
  203. }
  204.  
  205. /**
  206. * @param float $fuel
  207. * @throws Exception
  208. */
  209. public function reFuel(float $fuel): void
  210. {
  211. $consumption = $this->getFuelQuantity() + ($fuel * 0.95);
  212. $this->setFuelQuantity($consumption);
  213. }
  214.  
  215. public function __toString()
  216. {
  217. return "{$this->getType()}: " . number_format($this->getFuelQuantity(), 2, ".", "") . "\n";
  218. }
  219. }
  220.  
  221. class Bus extends Vehicle
  222. {
  223. public function __construct(string $type, float $fuelQuantity, float $litersPerKm, float $capacity)
  224. {
  225. parent::__construct($type, $fuelQuantity, $litersPerKm, $capacity);
  226. }
  227.  
  228. /**
  229. * @param float $distance
  230. * @throws Exception
  231. */
  232. public function DriveEmpty(float $distance): void
  233. {
  234. $consumption = ($this->getLitersPerKm()) * $distance;
  235. if ($consumption <= $this->getFuelQuantity()) {
  236. $this->setFuelQuantity($this->getFuelQuantity() - $consumption);
  237. throw new Exception("Bus travelled $distance km\n");
  238. }
  239. elseif ($this->getFuelQuantity() < 0) {
  240. echo "{$this->getFuelQuantity()}\n";
  241. throw new Exception("Fuel must be a positive number\n");
  242. }
  243. throw new Exception("Bus needs refueling\n");
  244. }
  245.  
  246. /**
  247. * @param float $distance
  248. * @throws Exception
  249. */
  250. public function Drive(float $distance): void
  251. {
  252. $consumption = ($this->getLitersPerKm() + 1.4) * $distance;
  253. if ($consumption <= $this->getFuelQuantity()) {
  254. $this->setFuelQuantity($this->getFuelQuantity() - $consumption);
  255. throw new Exception("Bus travelled $distance km\n");
  256. }
  257. elseif ($this->getFuelQuantity() < 0) {
  258. throw new Exception("Fuel must be a positive number\n");
  259. }
  260. throw new Exception("Bus needs refueling\n");
  261. }
  262.  
  263. /**
  264. * @param float $fuel
  265. * @throws Exception
  266. */
  267. public function reFuel(float $fuel): void
  268. {
  269. if ($this->getFuelQuantity() + $fuel > $this->getCapacity()) {
  270. throw new Exception("Cannot fit fuel in tank\n");
  271. }
  272. $this->setFuelQuantity($this->getFuelQuantity() + $fuel);
  273. }
  274.  
  275. public function __toString()
  276. {
  277. return "{$this->getType()}: " . number_format($this->getFuelQuantity(), 2, ".", "");
  278. }
  279. }
  280.  
  281. class Main
  282. {
  283. public $car;
  284. private $truck;
  285. private $bus;
  286. const PATTERN = '/\s+/';
  287.  
  288. public function run()
  289. {
  290. $this->readData();
  291. }
  292.  
  293.  
  294. public function readData()
  295. {
  296. /**
  297. * @var Car $car
  298. * @var Bus $bus
  299. * @var Truck $truck
  300. */
  301. list($typeCar, $fuelQuantityCar, $litersPerKmCar, $capacityCar) = preg_split(self::PATTERN, readline(), -1, PREG_SPLIT_NO_EMPTY);
  302. $car = VehiclesFactory::create($typeCar, $fuelQuantityCar, $litersPerKmCar, $capacityCar);
  303.  
  304. list($typeTruck, $fuelQuantityTruck, $litersPerKmTruck, $capacityTruck) = preg_split(self::PATTERN, readline(), -1, PREG_SPLIT_NO_EMPTY);
  305. $truck = VehiclesFactory::create($typeTruck, $fuelQuantityTruck, $litersPerKmTruck, $capacityTruck);
  306.  
  307. list($typeBus, $fuelQuantityBus, $litersPerKmBus, $capacityBus) = preg_split(self::PATTERN, readline(), -1, PREG_SPLIT_NO_EMPTY);
  308. $bus = VehiclesFactory::create($typeBus, $fuelQuantityBus, $litersPerKmBus, $capacityBus);
  309.  
  310. $num = intval(readline());
  311. for ($i = 0; $i < $num; $i++) {
  312. $line = preg_split(self::PATTERN, readline(), -1, PREG_SPLIT_NO_EMPTY);
  313. $command = $line[0];
  314. switch ($command) {
  315. case"Drive":
  316. switch ($line[1]) {
  317. case"Car":
  318. try {
  319. $car->Drive($line[2]);
  320. } catch (Exception $e) {
  321. echo $e->getMessage();
  322. }
  323. break;
  324. case"Bus":
  325. try {
  326. $bus->Drive($line[2]);
  327. } catch (Exception $e) {
  328. echo $e->getMessage();
  329. }
  330. break;
  331. case"Truck":
  332. try {
  333. $truck->Drive($line[2]);
  334. } catch (Exception $e) {
  335. echo $e->getMessage();
  336. }
  337. break;
  338. }
  339. break;
  340. case"Refuel":
  341. switch ($line[1]) {
  342. case"Car":
  343. try {
  344. $car->reFuel($line[2]);
  345. } catch (Exception $e) {
  346. echo $e->getMessage();
  347. }
  348. break;
  349. case"Bus":
  350. try {
  351. $bus->reFuel($line[2]);
  352. } catch (Exception $e) {
  353. echo $e->getMessage();
  354. }
  355. break;
  356. case"Truck":
  357. try {
  358. $truck->reFuel($line[2]);
  359. } catch (Exception $e) {
  360. echo $e->getMessage();
  361. }
  362. break;
  363. }
  364. break;
  365. case"DriveEmpty":
  366. switch ($line[1]) {
  367. case"Bus":
  368. try {
  369. $bus->DriveEmpty($line[2]);
  370. } catch (Exception $e) {
  371. echo $e->getMessage();
  372. }
  373. break;
  374. }
  375. break;
  376. }
  377. }
  378. echo $car;
  379. echo $truck;
  380. echo $bus;
  381. }
  382.  
  383.  
  384. }
  385.  
  386. $main = new Main();
  387. $main->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement