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; }
public static function obterMoeda(Moeda $moeda) { $locale = $moeda->getLocale(); if ($locale == 'pt_BR') { $moeda->setMoeda('BRL'); $moeda->setNome('Real brasileiro'); $moeda->setSimbolo('R$'); $moeda->setSeparadorDaParteInteira(','); $moeda->setPrecisao(2); } if ($locale == 'en_US') { $moeda->setMoeda('USD'); $moeda->setNome('US Dollar'); $moeda->setSimbolo('$'); $moeda->setSeparadorDaParteInteira('.'); $moeda->setPrecisao(2); } if ($locale == 'de_DE') { $moeda->setMoeda('EUR'); $moeda->setNome('Euro'); $moeda->setSimbolo('?'); $moeda->setSeparadorDaParteInteira(','); $moeda->setPrecisao(2); } }