1
resposta

Resposta das atividades

  1. Crie uma enum em PHP com tipos de contas bancárias e implemente um método informando se a conta possui taxas. Contas correntes e de investimento possuem taxas, enquanto contas poupança e universitárias não;
<?php 

 enum AccountTypes {
    
    case CurrentAccount;
    case AccountInvestment;
    case SavingsAccount;
    case UniversityAccount;

    public function hasAfee():string
    {
       $result = match($this){
            AccountTypes::CurrentAccount, AccountTypes::AccountInvestment => 'Possui taxa',
            AccountTypes::SavingsAccount , AccountTypes::UniversityAccount => 'Sem taxa',
       };

       return 'A conta é do tipo ' . $this->name . " é $result \n";
    }
 }
  1. Crie uma classe que represente uma conta com as propriedades saldo, nome do titular e tipo. Use os tipos e formas de acesso adequadas.
<?php 

require __DIR__ .'/09-AccountEnum.php';

class Account {

    private int $balanceInCents; 
    private int $totalWithdrawn = 0; 
    private int $totalDeposited = 0; 

    public function __construct(
        public readonly AccountTypes $type,
        public readonly string $name,)
    {
        $this->balanceInCents = 0;
    }

    public function withdraw(float $valueWithdraw): void
    {
        if ($valueWithdraw <= 0 || $valueWithdraw> $this->balanceInCents) {
            throw new RuntimeException("Saldo insuficiente ou valor inválido!");
        }

        $this->balanceInCents -= $valueWithdraw;
        $this->totalWithdrawn += $valueWithdraw;
    }

    public function deposit(float $valueDeposit): void
    {

        if ($valueDeposit <= 0) {
            throw new RuntimeException("Deposite um valor positivo, por favor!");
        }

        $this->balanceInCents += $valueDeposit;
        $this->totalDeposited += $valueDeposit;
    }

    public function showInfoAccount(): void
    {
        echo "Titular da conta: " . $this->name . "\n" .
             "Total depositado: R$" . number_format($this->totalDeposited, 2, ',', '.') . "\n" .
             "Total sacado: R$" . number_format ($this->totalWithdrawn , 2, ',', '.') . "\n".
             "Saldo atual: R$" . number_format($this->balanceInCents, 2, ',','.') ."\n",
             $this->type->hasAFee();
    }
};

$newAccount = new Account(AccountTypes::SavingsAccount,'Carol Sanches');
$newAccount->deposit(200);
$newAccount->withdraw(100);
$newAccount->showInfoAccount();
1 resposta

Bom trabalho! Sempre melhorando!