Advertisement
Garey

Marto_Homework_PHP

Apr 24th, 2018
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.66 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Marto;
  4.  
  5. interface VehicleInterface {
  6.     public function getYear();
  7.     public function setYear($year);
  8. }
  9.  
  10. interface ParkingInterface {
  11.     public function fillSlot(Vehicle $vehicle);
  12.     public function freeSlot();
  13.     public function calculateFees();
  14.     public function showParking();
  15. }
  16.  
  17. interface VehicleFees {
  18.     public function calculateFee();
  19. }
  20.  
  21. class Parking implements ParkingInterface {
  22.     private $parking = array();
  23.     private $slots;
  24.  
  25.     function __construct($slots) {
  26.       if(!is_int($slots) || $slots < 0)
  27.         throw new \Exception("Invalid slots", 1);
  28.       else
  29.         $this->slots = $slots;
  30.     }
  31.  
  32.     public function fillSlot(Vehicle $vehicle) {
  33.         array_push($this->parking, $vehicle);
  34.     }
  35.  
  36.     public function freeSlot() {
  37.       array_pop($this->parking);
  38.     }
  39.  
  40.     public function calculateFees() {
  41.       $sum = 0;
  42.       $counter = 0;
  43.  
  44.       foreach ($this->parking as $slot) {
  45.         $sum += $slot->calculateFee();
  46.         $counter++;
  47.       }
  48.         echo "<pre>Паркоместа: <b>{$counter}/{$this->slots}</b> | Сумирана такса: <b>{$sum}</b></pre>";
  49.     }
  50.  
  51.     public function showParking() {
  52.       foreach ($this->parking as $slot) {
  53.         printf("<pre>Превозно средство: <b>%s</b> | %s | Такса: <b>%d</b> | Година на производство: <b>%d</b></pre>",
  54.           $slot instanceof Car ? "Кола": "Автобус",
  55.           $slot instanceof Car ? "Тип: <b> {$slot->getType()}</b>" : "Места: <b>{$slot->getSeats()}</b>",
  56.           $slot->calculateFee(),
  57.           $slot->getYear()
  58.         );
  59.       }
  60.     }
  61. }
  62.  
  63. abstract class Vehicle implements VehicleInterface {
  64.     private $year;
  65.  
  66.     final public function getYear() {
  67.       return $this->year ? $this->year : false;
  68.     }
  69.  
  70.     final public function setYear($year) {
  71.       $this->year = $year;
  72.     }
  73. }
  74.  
  75. class Car extends Vehicle implements VehicleFees {
  76.     # 0 - Седан
  77.    # 1 - Кабрио
  78.    # 2 - Комби
  79.    private $type;
  80.  
  81.     function __construct($type) {
  82.         if(!is_int($type) || $type < 0)
  83.           throw new \Exception("Invalid type", 1);
  84.         else
  85.           $this->type = $type;
  86.     }
  87.  
  88.     public function calculateFee() {
  89.         return $this->type === 0 || $this->type === 1 ? parent::getYear() * 2 : parent::getYear() * 3;
  90.     }
  91.  
  92.     public function getType() {
  93.      return ($this->type === 0 ? "Седан" : ($this->type === 1 ? "Кабрио" : ($this->type === 2 ? "Комби" : "Грешен тип!")));
  94.     }
  95. }
  96.  
  97. class Bus extends Vehicle implements VehicleFees {
  98.     private $seats;
  99.  
  100.     function __construct($seats) {
  101.         if(!is_int($seats) || $seats < 0)
  102.           throw new \Exception("Invalid seats", 1);
  103.         else
  104.           $this->seats = $seats;
  105.     }
  106.  
  107.     public function calculateFee() {
  108.         return parent::getYear() * $this->seats;
  109.     }
  110.  
  111.     public function getSeats() {
  112.       return $this->seats;
  113.     }
  114.  
  115. }
  116.  
  117. $parking = new Parking(20);
  118.  
  119. $cars[0] = new Car(0);
  120. $cars[1] = new Car(1);
  121. $cars[2] = new Car(2);
  122. $cars[3] = new Car(4);
  123.  
  124. foreach($cars as $car) {
  125.   $car->setYear(1996 + rand(0, 9));
  126.   $parking->fillSlot($car);
  127. }
  128.  
  129. $buses[0] = new Bus(21);
  130. $buses[1] = new Bus(15);
  131. $buses[2] = new Bus(51);
  132. $buses[3] = new Bus(3);
  133.  
  134. foreach($buses as $bus) {
  135.   $bus->setYear(2007 + rand(0, 9));
  136.   $parking->fillSlot($bus);
  137. }
  138.  
  139. $parking->showParking();
  140. echo "<hr>";
  141. $parking->calculateFees();
  142. $parking->freeSlot();
  143.  
  144. echo "<br><h2>След освобождаване на паркомясто:</h2><br>";
  145.  
  146. $parking->ShowParking();
  147. echo "<hr>";
  148. $parking->calculateFees();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement