1
resposta

Destinatário Autenticado

      • bank_controller_exceptions * * * class ReceiverNotAuthenticatedException implements Exception { String receiverNotAuth; ReceiverNotAuthenticatedException({required this.receiverNotAuth}); }
      • bank_controller.dart * * * import '../exceptions/bank_controller_exceptions.dart'; import '../models/account.dart';

class BankController { final Map<String, Account> _database = {};

addAccount({required String id, required Account account}) { _database[id] = account; }

bool makeTransfer( {required String idSender, required String idReceiver, required double amount}) { // Verificar se ID de remetente é válido if (!verifyId(idSender)) { throw SenderIdInvalidException(idSender: idSender); }

// Verificar se ID de destinatário é válido
if (!verifyId(idReceiver)) {
  throw ReceiverIdInvalidException(idReceiver: idReceiver);
}

Account accountSender = _database[idSender]!;
Account accountReceiver = _database[idReceiver]!;

// Verificar se o remetente está autenticado
if (!accountSender.isAuthenticated) {
  throw SenderNotAuthenticatedException(idSenderNotAuthenticate: idSender);
}

// Verificar se o destinatario está autenticado
try {
  if (!accountReceiver.isAuthenticated) {
    throw ReceiverNotAuthenticatedException(receiverNotAuth: idReceiver);
  }
} on ReceiverNotAuthenticatedException catch (e) {
  print(
      "O usuário '${e.receiverNotAuth}' não esta autenticado no sistema!");
}

// Verificar se o remetente possui saldo suficiente
if (accountSender.balance < amount) {
  throw SenderBalanceLowerThanAmountException(
      idSenderBalance: idSender,
      senderBalance: accountSender.balance,
      amout: amount);
}

// Se tudo estiver certo, efetivar transação
accountSender.balance -= amount;
accountReceiver.balance += amount;

if (!accountReceiver.isAuthenticated) {
  return false;
} else {
  return true;
}

}

bool verifyId(String id) { return _database.containsKey(id); } }

1 resposta

Oii Yuri, tudo bem?

O seu código tá bem estruturado e você tá lidando de forma eficaz com exceções personalizadas e a autenticação do destinatário. Continue assim!

Lembre-se de que, além de compartilhar aqui no fórum, você também pode marcar a Alura nas redes sociais para que mais pessoas possam ver seu trabalho.

A Alura tem perfis em várias redes sociais, você pode encontrá-los aqui. E não se esqueça do nosso Discord de alunos da Alura.

Um abraço e bons estudos.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software