From a9b629ee7235ded0288069661671f9d4030a8359 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Thu, 10 Jun 2021 18:20:58 -0700 Subject: [PATCH] changed order of conditionals out of preference --- src/bank.cpp | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/bank.cpp b/src/bank.cpp index f203720..2bcd975 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -118,24 +118,12 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name } else { - if (!state) - { - return state; - } - else + if (state) { //if B doesnt exist - if (!users.modify_if(b_name, [amount](User &b) { + if (users.modify_if(b_name, [amount](User &b) { b.balance += amount; })) - { - //attempt to refund if A exist - users.modify_if(a_name, [amount](User &a) { - a.balance += amount; - }); - return ErrorResponse::UserNotFound; //because had to refund transaction - } - else { if constexpr (max_log_size) { @@ -150,6 +138,18 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name } return true; } + else + { + //attempt to refund if A exist + users.modify_if(a_name, [amount](User &a) { + a.balance += amount; + }); + return ErrorResponse::UserNotFound; //because had to refund transaction + } + } + else + { + return state; } } } @@ -256,7 +256,11 @@ void Bank::Save() }); } } - if (!temp.isNull()) + if (temp.isNull()) + { + throw std::invalid_argument("Saving Failed\n"); + } + else { std::ofstream user_save("../users.json"); Json::StreamWriterBuilder builder; @@ -264,10 +268,6 @@ void Bank::Save() writer->write(temp, &user_save); user_save.close(); } - else - { - throw std::invalid_argument("Saving Failed\n"); - } } //NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS