Skip to content

Instantly share code, notes, and snippets.

@tkouleris
Created December 15, 2016 18:57
Show Gist options
  • Select an option

  • Save tkouleris/0523ed92f2d14e411dca67296ba0b081 to your computer and use it in GitHub Desktop.

Select an option

Save tkouleris/0523ed92f2d14e411dca67296ba0b081 to your computer and use it in GitHub Desktop.
<?php
interface IDiscount{
function calculateDiscount($bill);
}
class NoDiscount implements IDiscount{
public function calculateDiscount($bill){
return $bill;
}
}
class LowDiscount implements IDiscount{
public function calculateDiscount($bill){
return $bill * 0.9;
}
}
class HighDiscount implements IDiscount{
public function calculateDiscount($bill){
return $bill * 0.7;
}
}
class BuyGames{
private $totalcost = 0;
public $discountInstance = null;
public function buyGame($name, $cost){
echo $name . ": " . $cost ." Euro \r\n";
$this->totalcost = $this->totalcost + $cost;
echo "Total Cost: ". $this->totalcost . " Euro \r\n";
return $this->totalcost;
}
public function discount(IDiscount $discountType){
$this->discountInstance = $discountType;
return $this->discountInstance->calculateDiscount($this->totalcost);
}
}
//Client
$bgames = new BuyGames();
$bgames->buyGame("Zork", 24);
$bgames->buyGame("Super Mario", 30);
$bgames->buyGame("Sonic", 40);
echo "Total with 30% discount: " . $bgames->discount( new HighDiscount() ) . "\r\n";
echo "Total with 10% discount: " . $bgames->discount( new LowDiscount() ) . "\r\n";
echo "Total with no discount: " . $bgames->discount( new NoDiscount() ) . "\r\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment