Skip to content

Instantly share code, notes, and snippets.

@korchasa
Created October 11, 2018 10:59
Show Gist options
  • Save korchasa/24fa04ec6e198914920bedc469c8e89b to your computer and use it in GitHub Desktop.
Save korchasa/24fa04ec6e198914920bedc469c8e89b to your computer and use it in GitHub Desktop.
BeverageMaker test
<?php
class BeverageMaker
{
const TYPE_COFFEE = 'coffee';
const TYPE_TEA = 'tea';
const STATUS_NO_WATER = "no water";
const STATUS_NO_SOLID = "no solid";
const STATUS_READY = "I'm ready";
const STATUS_BROKEN = "broken";
const STATUS_BREWING = 'brewing';
const PLANNED_OBSOLESCENCE = '1000';
public $beverageType;
public $waterCapacity;
public $solidCapacity;
public $currentWater;
public $currentSolid;
public $waterPerCup;
public $solidPerCup;
public $cupsCounter;
public $stopFile;
public function __construct($beverage_type, $water_capacity, $solid_capacity)
{
$this->waterCapacity = $water_capacity;
$this->solidCapacity = $solid_capacity;
$this->beverageType = $beverage_type;
$this->cupsCounter = 0;
$this->stopFile = 'stop'.rand(1, 10000);
switch ($beverage_type) {
case self::TYPE_TEA:
$this->liquidPerCup = '200';
$this->solidPerCup = '10';
break;
case self::TYPE_COFFEE:
default:
$this->liquidPerCup = '100';
$this->solidPerCup = '25';
break;
}
$this->status = self::STATUS_NO_WATER;
}
public function brew($numberOfCups = 1)
{
if ($this->cupsCounter < self::PLANNED_OBSOLESCENCE) {
if ($this->currentWater >= $this->liquidPerCup * $numberOfCups) {
if ($this->currentSolid >= $this->solidPerCup * $numberOfCups) {
$this->status = self::STATUS_BREWING;
for ($i = 1; $i <= $numberOfCups; $i++) {
$stopFile = fopen($this->stopFile, 'r+');
$stop = fgets($stopFile);
if ($stop != 'yes') {
if ($this->beverageType == self::TYPE_COFFEE) {
echo 'Grinding';
}
echo 'Brewing';
$this->currentWater = $this->currentWater - $this->liquidPerCup;
$this->cupsCounter = $this->cupsCounter + 1;
echo 'Enjoy your '.$this->beverageType.'\n';
} else {
fwrite($stopFile, 'no');
fclose($stopFile);
return;
}
}
$this->status = self::STATUS_READY;
} else {
echo 'I need more '.$this->beverageType;
}
} else {
echo 'I need more water';
}
} else {
echo 'Error';
}
}
public static function stop(BeverageMaker $beverageMaker)
{
$stopFile = fopen($beverageMaker->stopFile, 'w');
fwrite($stopFile, 'yes');
}
public function addWater($water)
{
if ($water + $this->currentWater <= $this->waterCapacity) {
$this->currentWater = $water + $this->currentWater;
} else {
if ($water + $this->currentWater > $this->waterCapacity) {
$this->currentWater = $this->waterCapacity;
return $water + $this->currentWater - $this->waterCapacity;
}
}
return true;
}
public function addSolid($solid)
{
if ($solid + $this->currentSolid <= $this->solidCapacity) {
$this->currentSolid = $solid + $this->currentWater;
} else {
if ($solid + $this->currentSolid > $this->solidCapacity) {
$this->currentSolid = $this->solidCapacity;
return $solid + $this->currentSolid - $this->solidCapacity;
}
}
return true;
}
public function clean()
{
$this->currentWater = $this->currentWater - 50;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment