Created
October 27, 2019 15:23
-
-
Save Orianne0605/277854887a83f4210d8649be95e83915 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'; | |
//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 | |
$truck2 = new Truck('pink', 2, 'gazole', 150, 15); | |
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(); | |
echo '<br>'; | |
echo $truck->loading(); | |
echo '<br>'; | |
// Moving truck | |
echo $truck2->forward(); | |
echo '<br> Vitesse du camion : ' . $truck2->getCurrentSpeed(). ' km/h' . '<br>'; | |
echo $truck2->brake(); | |
echo '<br> Vitesse du camion : ' . $truck2->getCurrentSpeed() . ' km/h' . '<br>'; | |
echo $truck2->brake(); | |
echo '<br>'; | |
echo $truck2->loading(); |
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 | |
{ | |
/** | |
* @var string | |
*/ | |
private $energy; | |
/** | |
* @var int | |
*/ | |
private $energyLevel; | |
/** | |
* @var int | |
*/ | |
private $storageCapacity; | |
/** | |
* @var int | |
*/ | |
private $loading = 0; | |
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; | |
} | |
public function loading(): string | |
{ | |
if ($this->loading < $this->storageCapacity) { | |
return 'in filling'; | |
} elseif ($this->loading == $this->storageCapacity) { | |
return 'full'; | |
} | |
} | |
public | |
function getStorageCapacity(): int | |
{ | |
return $this->storageCapacity; | |
} | |
public | |
function setStorageCapacity(int $storageCapacity): void | |
{ | |
$this->storageCapacity = $storageCapacity; | |
} | |
public | |
function getLoading(): int | |
{ | |
return $this->loading; | |
} | |
public | |
function setLoading(int $loading): void | |
{ | |
$this->loading = $loading; | |
} | |
} |
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