public static function cambiar() { $m1 = new Moeda("pt_BR"); $m2 = new Moeda("en_US"); $m1->setValor(5.6); $m2->setValor(2.4); $m1->adicionarServicoDeCambio(new ServicoDeCambio()); try { $m3 = $m1->somar($m2); echo "\n soma de " . $m1 . ' com ' . $m2 . ' eh igual a ' . $m3 . "\n"; } catch (Exception $e) { echo $e->getMessage(); } }
public function somar($m2) { $m3 = new Moeda($this->getLocale()); if ($this->getLocale() == $m2->getLocale()) { $m3->setValor($this->getValor() + $m2->getValor()); return $m3; } else { if ($this->servicoDeCambio == null) { throw new Exception('Não tem serviço de câmbio!'); } else { $taxa = $this->servicoDeCambio->obterTaxa($m2->getMoeda(), $this->getMoeda()); $m3->setValor($this->getValor() + $m2->getValor() * $taxa); } } return $m3; }