import 'controllers/bank_controller.dart';
import 'exceptions/bank_controller_exceptions.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: "Kako", idReceiver: "Ricarth", amount: 90);
if (result){
print("The transaction was sucessful!");
}
// Observando resultado
print(result);
} on SenderIdInvalidException catch (e){
print(e);
print( "The ID '${e.idSender}' is not valid.");
} on ReceiverIdInvalidException catch (e){
print(e);
print("The receiver ID '${e.idReceiver}' is not valid.");
} on SenderNotAuthenticatedException catch (e){
print(e);
print("Sorry! The account '${e.idSender}'is not authenticated.");
} on SenderBalanceLowerThanAmountException catch (e){
print(e);
print("Your balance is not enough you need ${e.amount - e.senderBalance}.");
}on ReceiverNotAuthenticatedException catch (e){
print(e);
print("Sorry! The receiver account '${e.idReceiver}' is not authenticated.");
}
}
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(idSender: idSender);
}
//Verificar se o destinatario esta autenticado
if (!accountReceiver.isAuthenticated) {
throw ReceiverNotAuthenticatedException(idReceiver: idReceiver);
}
// Verificar se o remetente possui saldo suficiente
if (accountSender.balance < amount) {
throw SenderBalanceLowerThanAmountException(amount: amount, senderBalance: accountSender.balance);
}
// Se tudo estiver certo, efetivar transação
accountSender.balance -= amount;
accountReceiver.balance += amount;
return true;
}
bool verifyId(String id) {
return _database.containsKey(id);
}
}
class SenderIdInvalidException implements Exception{
static const report = "SenderIdInvalidException";
String idSender;
SenderIdInvalidException({required this.idSender});
@override
String toString() {
return "${report}\nID sender: ${idSender}";
}
}
class ReceiverIdInvalidException implements Exception{
static const String report = "ReceiverIdInvalidException";
String idReceiver;
ReceiverIdInvalidException({required this.idReceiver});
@override
String toString(){
return "${report}\nID Receiver: ${idReceiver}";
}
}
class SenderNotAuthenticatedException implements Exception{
static const String report = "SenderNotAuthenticatedException";
String idSender;
SenderNotAuthenticatedException({required this.idSender});
@override
String toString(){
return "${report}\n${idSender}";
}
}
class SenderBalanceLowerThanAmountException implements Exception{
static const String report = "SenderBalanceLowerThanAmountException";
num amount;
num senderBalance;
SenderBalanceLowerThanAmountException({required this.amount, required this.senderBalance});
@override
String toString(){
return "${report}\nSend: ${amount}\nSender Balance: ${senderBalance}\n";
}
}
class ReceiverNotAuthenticatedException implements Exception{
static const String report = "ReceiverNotAuthenticatedException";
String idReceiver;
ReceiverNotAuthenticatedException({required this.idReceiver});
@override
String toString(){
return "${report}\n${idReceiver}";
}
}