Skip to content

Instantly share code, notes, and snippets.

@PiyarNET
Forked from clarade/Bicycle.php
Created June 22, 2022 11:45
Show Gist options
  • Save PiyarNET/1d2e7844a6d07c99a6fb99227a81d68c to your computer and use it in GitHub Desktop.
Save PiyarNET/1d2e7844a6d07c99a6fb99227a81d68c to your computer and use it in GitHub Desktop.
POO exercise about classes
<?php
class Bicycle
{
private $color;
private $currentSpeed;
private $name;
private $nbSeats;
private $nbWheels;
public function __construct(string $name, string $color)
{
$this->color = $color;
$this->name = $name;
$this->currentSpeed = 0;
}
public function forward()
{
$this->currentSpeed = 15;
return "Go !";
}
public function brake(): string
{
$sentence = "";
while ($this->currentSpeed > 0) {
$this->currentSpeed--;
$sentence .= "Brake !!!";
}
$sentence .= "I'm stopped !";
return $sentence;
}
public function getColor(): string
{
return $this->color;
}
public function getName(): string
{
return $this->name;
}
public function getCurrentSpeed(): int
{
return $this->currentSpeed;
}
public function setCurrentSpeed(int $currentSpeed): void
{
if ($currentSpeed >= 0) {
$this->currentSpeed = $currentSpeed;
}
}
}
<?php
class Car
{
private string $brandName;
private int $nbWheels;
private int $currentSpeed;
private string $color;
private int $nbSeats;
private string $energyType;
private int $energyLevel;
public function __construct(string $brandName, string $color, int $nbSeats, string $energy)
{
$this->currentSpeed= 0;
$this->brandName = $brandName;
$this->color = $color;
$this->nbSeats = $nbSeats;
$this->energy = $energy;
}
public function forward()
{
$this->currentSpeed++;
}
public function brake()
{
$this->currentSpeed--;
}
public function start() : Car
{
$this->currentSpeed = 15;
return $this;
}
public function getNbWheels()
{
return $this->nbWheels;
}
public function getCurrentSpeed()
{
return $this->currentSpeed;
}
public function getColor()
{
return $this->color;
}
public function getNbSeats()
{
return $this->nbSeats;
}
public function getEnergy()
{
return $this->energy;
}
public function getEnergyLevel()
{
return $this->energyLevel;
}
/**
* Get the value of brandName
*
* @return string
*/
public function getBrandName()
{
return $this->brandName;
}
/**
* Set the value of brandName
*
* @param string $brandName
*
* @return self
*/
public function setBrandName(string $brandName)
{
$this->brandName = $brandName;
return $this;
}
}
<?php
require_once '../src/Car.php';
require_once '../src/Bicycle.php';
$ford = new Car('Ford', 'blue', 4, 'ethanol');
$mini = new Car('Mini', 'red', 4, 'electric');
$rover = new Car('Rover', 'green', 6, 'gas');
$tesla = new Car('Tesla', 'yellow', 2, 'electric');
$firstBicycle = new Bicycle('rockrider', 'orange');
$secondBicycle = new Bicycle('b-tween', 'black');
echo " Le véhicule " . $ford->getBrandName() . " de couleur " . $ford->getColor() . " avance à une allure de ";
echo "</br>";
$ford->start();
echo $ford->getCurrentSpeed();
echo " | ";
while ($ford->getCurrentSpeed() < 50) {
$ford->forward();
echo $ford->getCurrentSpeed();
echo " | ";
}
echo " <strong> Mega speed nitrooo </strong> ";
echo "</br>";
echo " <strong> Ca suffit les conneries, c'est l'heure de freiner </strong> ";
while ($ford->getCurrentSpeed() > 0) {
$ford->brake();
echo $ford->getCurrentSpeed();
echo " | ";
}
echo "</br>";
echo " Le vélo " . $firstBicycle->getName() . " de couleur " . $firstBicycle->getColor() . " avance à une allure de ";
echo "</br>";
echo $firstBicycle->getCurrentSpeed();
echo " | ";
echo $firstBicycle->forward();
echo $firstBicycle->getCurrentSpeed();
echo " | ";
echo " <strong> Mega roue arrière!</strong> ";
echo "</br>";
echo $firstBicycle->brake();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment