ciao ho un problema con i float. Guardate cosa fa il metodo makeEspresso all'iterazione 100!
dovrebbe portare a 0 la quantita' di caffe' e invece fa fuori di testa e stampa un numero con notazione scientifica. I float si comportano strani detro le classi?? ma che roba e'?
sono su ubuntu per la cronaca. grazie raga'
Codice PHP:
class EspressoMachine {
// store here the default values the machine can handle
const TOTAL_WATER = 5;
const TOTAL_BEANS = 80;
const ESPRESSO_WATER = 0.05;
const D_ESPRESSO_WATER = 0.10;
const ESPRESSO_BEANS = 1;
const D_ESPRESSO_BEANS = 2;
// UI messages
const MESS_NOWATER = "Add water";
const MESS_NOBEANS = "Add beans";
private $espressoSoFar = 0; // number of liters of espresso erogated
private $beans; // number of spoons of beans - refilled completely at instantiation time
private $water; // number of liters - refilled completely at instantiation time
public function __construct() {
$this->water = self::TOTAL_WATER;
$this->beans = self::TOTAL_BEANS;
}
private function updateLitersOfEspressoSoFar($quantity) {
// BEGIN debug area
echo "espressoSoFar : " . $this->espressoSoFar . "
";
echo "mod " . fmod($this->espressoSoFar, 5);
if ($this->espressoSoFar >= 5.0) {
echo "### maggiore/uguale a 5
";
}
if (!fmod($this->espressoSoFar, 5)) {
echo "### divisibile per 5
";
}
// END debug area
if (($this->espressoSoFar >= 5) && (fmod($this->espressoSoFar, 5.0) == 0)) {
//$this->descale();
}
$this->espressoSoFar += $quantity;
return $this->espressoSoFar;
}
private function printOnDisplay($str) {
echo $str . "
";
}
public function getBeans() {
if ($this->beans > 0) {
return $this->beans;
} else {
throw new NoBeansException(self::MESS_NOBEANS);
}
}
public function addBeans($beans) {;
$this->beans += $beans;
}
public function getWater() {
if ($this->water > 0) {
return $this->water;
} else {
throw new NoWaterException(self::MESS_NOWATER);
}
}
public function addWater($water) {
$this->water += $water;
}
public function descale() {
try {
$this->water = ($this->getWater() - 1 >= 0) ? ($this->getWater() - 1) : 0;
} catch (Exception $e) {
$this->printOnDisplay($e->getMessage());
}
}
public function makeEspresso() {
try {
$w = $this->getWater();
$b = $this->getBeans();
echo "--- prima di fare il caffe
";
echo "water left : " . $w . "
";
echo "beans left : " . $b . "
";
// no exceptions so the espresso can be prepared
echo "--- caffe fatto
";
$this->water = $w - self::ESPRESSO_WATER;
$this->beans = $b - self::ESPRESSO_BEANS;
echo "water left : " . $this->water . "
";
echo "beans left : " . $this->beans . "
";
return $this->updateLitersOfEspressoSoFar(self::ESPRESSO_WATER);
} catch (NoWaterException $e) {
$this->printOnDisplay($e->getMessage());
$this->printOnDisplay("autorefill process ...");
$this->addWater(self::TOTAL_WATER);
} catch (NoBeansException $e) {
$this->printOnDisplay($e->getMessage());
$this->printOnDisplay("autorefill process ...");
$this->addBeans(self::TOTAL_BEANS);
}
}
}
// client code
$o = new EspressoMachine;
for ($i = 0; $i < 1000; $i++) {
echo "N. " . $i . "
";
$o->makeEspresso();
echo "<hr />";
}