Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Marto;
- interface VehicleInterface {
- public function getYear();
- public function setYear($year);
- }
- interface ParkingInterface {
- public function fillSlot(Vehicle $vehicle);
- public function freeSlot();
- public function calculateFees();
- public function showParking();
- }
- interface VehicleFees {
- public function calculateFee();
- }
- class Parking implements ParkingInterface {
- private $parking = array();
- private $slots;
- function __construct($slots) {
- if(!is_int($slots) || $slots < 0)
- throw new \Exception("Invalid slots", 1);
- else
- $this->slots = $slots;
- }
- public function fillSlot(Vehicle $vehicle) {
- array_push($this->parking, $vehicle);
- }
- public function freeSlot() {
- array_pop($this->parking);
- }
- public function calculateFees() {
- $sum = 0;
- $counter = 0;
- foreach ($this->parking as $slot) {
- $sum += $slot->calculateFee();
- $counter++;
- }
- echo "<pre>Паркоместа: <b>{$counter}/{$this->slots}</b> | Сумирана такса: <b>{$sum}</b></pre>";
- }
- public function showParking() {
- foreach ($this->parking as $slot) {
- printf("<pre>Превозно средство: <b>%s</b> | %s | Такса: <b>%d</b> | Година на производство: <b>%d</b></pre>",
- $slot instanceof Car ? "Кола": "Автобус",
- $slot instanceof Car ? "Тип: <b> {$slot->getType()}</b>" : "Места: <b>{$slot->getSeats()}</b>",
- $slot->calculateFee(),
- $slot->getYear()
- );
- }
- }
- }
- abstract class Vehicle implements VehicleInterface {
- private $year;
- final public function getYear() {
- return $this->year ? $this->year : false;
- }
- final public function setYear($year) {
- $this->year = $year;
- }
- }
- class Car extends Vehicle implements VehicleFees {
- # 0 - Седан
- # 1 - Кабрио
- # 2 - Комби
- private $type;
- function __construct($type) {
- if(!is_int($type) || $type < 0)
- throw new \Exception("Invalid type", 1);
- else
- $this->type = $type;
- }
- public function calculateFee() {
- return $this->type === 0 || $this->type === 1 ? parent::getYear() * 2 : parent::getYear() * 3;
- }
- public function getType() {
- return ($this->type === 0 ? "Седан" : ($this->type === 1 ? "Кабрио" : ($this->type === 2 ? "Комби" : "Грешен тип!")));
- }
- }
- class Bus extends Vehicle implements VehicleFees {
- private $seats;
- function __construct($seats) {
- if(!is_int($seats) || $seats < 0)
- throw new \Exception("Invalid seats", 1);
- else
- $this->seats = $seats;
- }
- public function calculateFee() {
- return parent::getYear() * $this->seats;
- }
- public function getSeats() {
- return $this->seats;
- }
- }
- $parking = new Parking(20);
- $cars[0] = new Car(0);
- $cars[1] = new Car(1);
- $cars[2] = new Car(2);
- $cars[3] = new Car(4);
- foreach($cars as $car) {
- $car->setYear(1996 + rand(0, 9));
- $parking->fillSlot($car);
- }
- $buses[0] = new Bus(21);
- $buses[1] = new Bus(15);
- $buses[2] = new Bus(51);
- $buses[3] = new Bus(3);
- foreach($buses as $bus) {
- $bus->setYear(2007 + rand(0, 9));
- $parking->fillSlot($bus);
- }
- $parking->showParking();
- echo "<hr>";
- $parking->calculateFees();
- $parking->freeSlot();
- echo "<br><h2>След освобождаване на паркомясто:</h2><br>";
- $parking->ShowParking();
- echo "<hr>";
- $parking->calculateFees();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement