class SenderIdInvalidException implements Exception {
String report = "SenderIdInvalidExecption";
String idSender;
SenderIdInvalidException({required this.idSender});
@override
String toString(){
return "$report\nID Sender: $idSender";
}
}
class ReceiverIdInvalidException implements Exception
{
String report = "ReceiverIdInvalidException";
String idReceiver;
ReceiverIdInvalidException({required this.idReceiver});
@override
String toString(){
return "$report\nID Sender: $idReceiver";
}
}
class SendeNotAuthenticatedException implements Exception
{
String idSender;
SendeNotAuthenticatedException({required this.idSender});
}
class SenderBalenceLowerThaAmountException implements Exception
{
String idSender;
double senderBalce;
double amount;
SenderBalenceLowerThaAmountException({required this.idSender, required this.senderBalce, required this.amount});
}
import '../exceptions/bank_controller_exception.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 SendeNotAuthenticatedException(idSender: idSender);
}
// Verificar se o remetente possui saldo suficiente
if (accountSender.balance < amount) {
throw SenderBalenceLowerThaAmountException(idSender: idSender, senderBalce: accountSender.balance, amount: amount);
}
// Se tudo estiver certo, efetivar transação
accountSender.balance -= amount;
accountReceiver.balance += amount;
return true;
}
bool verifyId(String id) {
return _database.containsKey(id);
}
}
import 'controllers/bank_controller.dart';
import 'exceptions/bank_controller_exception.dart';
import 'models/account.dart';
void main() {
// Criando o banco
BankController bankController = BankController();
// Adicionando contas
bankController.addAccount(
id: "Ricarth",
account:
Account(name: "Ricarth Lima", balance: 400, isAuthenticated: true));
bankController.addAccount(
id: "Kako",
account:
Account(name: "Caio Couto", balance: 600, isAuthenticated: true));
// Fazendo transferência
try{
bool result = bankController.makeTransfer(
idSender: "Kako22", idReceiver: "Ricarth", amount: 700);
// Observando resultado
if(result){
print(result);
}
} on SenderIdInvalidException catch (e){
print(e.toString());
print("O id '${e.idSender}' está invalido para transferencia!");
} on ReceiverIdInvalidException catch(e){
print("O id '${e.idReceiver}' está invalido para receber transferencia!");
} on SendeNotAuthenticatedException catch(e){
print("O id '${e.idSender}' não está autorizado para fazer transferencia!");
} on SenderBalenceLowerThaAmountException catch(e) {
print("O id '${e.idSender}' tentou enviar ${e.amount} sendo que na sua conta tem apenas ${e.senderBalce}");
} on Exception catch(e){
print("Algo deu errado: ${e.toString()}");
}
}