Skip to content

Instantly share code, notes, and snippets.

@jeremiaspereira
Last active August 29, 2015 14:25
Show Gist options
  • Save jeremiaspereira/a2d0bf6dfd096a87dfa7 to your computer and use it in GitHub Desktop.
Save jeremiaspereira/a2d0bf6dfd096a87dfa7 to your computer and use it in GitHub Desktop.
Classe de caixa eletônico, sacar um valor usando o menor número de notas.
<?php
class CaixaEletronico {
public $qtdNotas;
public $notas;
public $retorno;
function __construct() {
$this->qtdNotas = array(0, 0, 0, 0);
$this->notas = array(100, 50, 20, 10);
}
public function sacar($valor = ""){
if($valor > 0){
for ($i= 0; $i < sizeof($this->notas); $i++) {
if($valor >= $this->notas[$i]){
do {
$valor = $valor - $this->notas[$i];
$this->qtdNotas[$i]++;
$this->retorno[$this->notas[$i]] = $this->qtdNotas[$i];
} while ($valor >= $this->notas[$i]);
}
}
if($valor != 0){
$this->retorno = array('error' => 'O valor não pode ser sacado');
}
}
else if ($valor < 0) {
$this->retorno = array('error' => 'Valor de entrada não aceito');
}
else{
$this->retorno = array("0" => 0);
}
echo (json_encode($this->retorno, JSON_FORCE_OBJECT));
exit;
}
}
$req = $_SERVER['REQUEST_METHOD'];
if($req == "GET"){
$valor = end(explode("/", $_SERVER['REQUEST_URI']));
$objCaixaEletronico = new CaixaEletronico();
$objCaixaEletronico->sacar($valor);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment