Created
October 27, 2019 14:57
-
-
Save Orianne0605/cc069d3bf127c2113c7ae1f6c7c37a80 to your computer and use it in GitHub Desktop.
POO 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'Vehicle.php'; | |
class Cars extends Vehicle | |
{ | |
const ALLOWED_ENERGIES = [ | |
'fuel', | |
'electric', | |
]; | |
/** | |
* @var string | |
*/ | |
private $energy; | |
/** | |
* @var int | |
*/ | |
private $energyLevel; | |
public function __construct(string $color, int $nbSeats, string $energy) | |
{ | |
parent::__construct($color, $nbSeats); | |
$this->setEnergy($energy); | |
} | |
public function getEnergy(): string | |
{ | |
return $this->energy; | |
} | |
public function setEnergy(string $energy): Cars | |
{ | |
if (in_array($energy, self::ALLOWED_ENERGIES)) { | |
$this->energy = $energy; | |
} | |
return $this; | |
} | |
public function getEnergyLevel(): int | |
{ | |
return $this->energyLevel; | |
} | |
public function setEnergyLevel(int $energyLevel): void | |
{ | |
$this->energyLevel = $energyLevel; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'Cars.php'; | |
require_once 'Truck.php'; | |
require_once 'Vehicle.php'; | |
var_dump(Truck::LOADING); | |
echo "<br>"; | |
echo "<br>"; | |
//Instanciation d'un objet $truck | |
$truck = new Truck('purple', 2, 'gazole', 150, 150); | |
var_dump($truck); | |
echo "<br>"; | |
echo "<br>"; | |
//Instanciation d'un objet $truck | |
$truck = new Truck('pink', 2, 'gazole', 150, 80); | |
var_dump($truck); | |
echo "<br>"; | |
echo "<br>"; | |
var_dump(Cars::ALLOWED_ENERGIES); | |
echo "<br>"; | |
echo "<br>"; | |
//Instanciation d'un objet $car | |
$car = new Cars('green', 4, 'electric'); | |
var_dump($car); | |
echo "<br>"; | |
echo "<br>"; | |
// Moving car | |
echo $car->forward(); | |
echo '<br> Vitesse de la voiture : ' . $car->getCurrentSpeed(). ' km/h' . '<br>'; | |
echo $car->brake(); | |
echo '<br> Vitesse de la voiture : ' . $car->getCurrentSpeed() . ' km/h' . '<br>'; | |
echo $car->brake(); | |
echo '<br>'; | |
// Moving truck | |
echo $truck->forward(); | |
echo '<br> Vitesse du camion : ' . $truck->getCurrentSpeed(). ' km/h' . '<br>'; | |
echo $truck->brake(); | |
echo '<br> Vitesse du camion : ' . $truck->getCurrentSpeed() . ' km/h' . '<br>'; | |
echo $truck->brake(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'Vehicle.php'; | |
class Truck extends Vehicle | |
{ | |
const LOADING = [ | |
'in filling', | |
'full', | |
]; | |
/** | |
* @var string | |
*/ | |
private $energy; | |
/** | |
* @var int | |
*/ | |
private $energyLevel; | |
/** | |
* @var int | |
*/ | |
private $storageCapacity; | |
/** | |
* @var int | |
*/ | |
private $loading; | |
public function __construct(string $color, int $nbSeats, string $energy, int $storageCapacity, int $loading) | |
{ | |
parent::__construct($color, $nbSeats); | |
$this->energy = $energy; | |
$this->storageCapacity = $storageCapacity; | |
$this->loading = $loading; | |
} | |
public function getEnergy(): string | |
{ | |
return $this->energy; | |
} | |
public function setEnergy(string $energy): void | |
{ | |
$this->energy = $energy; | |
} | |
public function getEnergyLevel(): int | |
{ | |
return $this->energyLevel; | |
} | |
public function setEnergyLevel(int $energyLevel): void | |
{ | |
$this->energyLevel = $energyLevel; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Vehicle.php | |
class Vehicle | |
{ | |
/** | |
* @var string | |
*/ | |
protected $color; | |
/** | |
* @var integer | |
*/ | |
protected $currentSpeed; | |
/** | |
* @var integer | |
*/ | |
protected $nbSeats; | |
/** | |
* @var integer | |
*/ | |
protected $nbWheels; | |
public function __construct(string $color, int $nbSeats) | |
{ | |
$this->color = $color; | |
$this->nbSeats = $nbSeats; | |
} | |
public function forward(): string | |
{ | |
$this->currentSpeed = 5; | |
return "Go !"; | |
} | |
public function brake(): string | |
{ | |
$sentence = ""; | |
while ($this->currentSpeed > 0) { | |
$this->currentSpeed--; | |
$sentence .= "Brake !!!"; | |
} | |
$sentence .= "I'm stopped !"; | |
return $sentence; | |
} | |
public function getCurrentSpeed(): int | |
{ | |
return $this->currentSpeed; | |
} | |
public function setCurrentSpeed(int $currentSpeed): void | |
{ | |
if($currentSpeed >= 0){ | |
$this->currentSpeed = $currentSpeed; | |
} | |
} | |
public function getColor(): string | |
{ | |
return $this->color; | |
} | |
public function setColor(string $color): void | |
{ | |
$this->color = $color; | |
} | |
public function getNbSeats(): int | |
{ | |
return $this->nbSeats; | |
} | |
public function setNbSeats(int $nbSeats): void | |
{ | |
$this->nbSeats = $nbSeats; | |
} | |
public function getNbWheels(): int | |
{ | |
return $this->nbWheels; | |
} | |
public function setNbWheels(int $nbWheels): void | |
{ | |
$this->nbWheels = $nbWheels; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment