From 1f6378ad92e8939652f966efcd4aaed284acb855 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Fri, 2 Apr 2021 12:32:00 -0700 Subject: [PATCH] :bug: bug fixes and implementing Bank API --- CMakeLists.txt | 4 ++-- include/bank.hpp | 48 ++++++++++++++++++++++++++++++++++++------ include/user.hpp | 55 ++---------------------------------------------- main.cpp | 15 +++++++------ 4 files changed, 54 insertions(+), 68 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 16f38f7..8758108 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,5 +14,5 @@ add_executable(${PROJECT_NAME} main.cpp) target_include_directories(${PROJECT_NAME} PUBLIC include) target_include_directories(${PROJECT_NAME} PUBLIC third_party) -add_subdirectory(third_party/drogon) -target_link_libraries(${PROJECT_NAME} PRIVATE drogon) \ No newline at end of file +# add_subdirectory(third_party/drogon) +# target_link_libraries(${PROJECT_NAME} PRIVATE drogon) \ No newline at end of file diff --git a/include/bank.hpp b/include/bank.hpp index fb3edc0..fc97cd6 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -20,25 +20,61 @@ public: bool AddUser(const std::string &name, std::string &&init_pass) { return users.try_emplace_l( - name, []() {}, init_pass); + name, [](User &) {}, std::forward(init_pass)); } - bool AdminAddUser(const std::string &attempt, std::string &&name, uint_fast64_t init_bal, std::string &&init_pass) + bool AdminAddUser(const std::string &attempt, std::string &&name, uint_fast32_t init_bal, std::string &&init_pass) { const bool state = (admin_pass == attempt); if (state) { users.try_emplace_l( - name, []() {}, init_bal, init_pass); + name, [](User &) {}, init_bal, std::forward(init_pass)); } return state; } - bool SendFunds(const std::string &a_name, const std::string &b_name, uint_fast64_t amount, const std::string &attempt) + + bool DelUser(const std::string &name, const std::string &attempt) + { + bool state = false; + //if password for user is correct, and user exists + users.if_contains(name, [&attempt, &state](const User &u) { + if (attempt == u.password) + { + state = true; + } + }); + if (state) //correct password + { + state = users.erase_if(name, [](const User &) { return true; }); + } + return state; + } + bool AdminDelUser(const std::string &name, const std::string &attempt) + { + bool state = (attempt == admin_pass); + if (state) + { + state = users.erase_if(name, [&state](const User &) { state = true; return state; }); + } + return state; + } + + int_fast64_t GetBal(const std::string &name) + { + int_fast64_t res = -1; + users.if_contains(name, [&res](const User &u) { + res = u.balance; + }); + return res; + } + + bool SendFunds(const std::string &a_name, const std::string &b_name, uint_fast32_t amount, const std::string &attempt) { //if A exists, A can afford it, and A's password matches bool state = false; users.modify_if(a_name, [&state, amount, &attempt](User &a) { - if (state = (a.balance >= amount) && (a.password == attempt)) + if (state = (a.balance >= amount) && (a.password == attempt), state) { a.balance -= amount; } @@ -55,7 +91,7 @@ public: users.modify_if(a_name, [amount](User &a) { a.balance += amount; }); - state = false; //because had to refund trasnaction + state = false; //because had to refund transaction } } diff --git a/include/user.hpp b/include/user.hpp index aeca485..da1ba6f 100644 --- a/include/user.hpp +++ b/include/user.hpp @@ -4,7 +4,7 @@ struct User { - uint_fast64_t balance = 0; + uint_fast32_t balance = 0; std::string password; /** @@ -20,56 +20,5 @@ struct User * @param init_bal initial balance * @param init_pass initial password */ - User(uint_fast64_t init_bal, std::string &&init_pass) : balance(init_bal), password(init_pass) {} - - // bool ChangePassword(const std::string &attempt, std::string &&new_pass) - // { - // const bool state = (password == attempt); - // if (state) - // { - // password = new_pass; - // } - // return state; - // } - - // // /** - // // * @brief SendFunds allows sending of money between users if verification is provided in the form of a password and if the user has sufficient funds - // // * - // // * @param a first user - // // * @param b second user - // // * @param amount amount being sent - // // * @param attempt password of first user - // // * @return wether transaction was succesful - // // */ - // // static bool SendFunds(User &a, User &b, uint_fast64_t amount, const std::string &attempt) - // // { - // // const bool state = (a.password == attempt) && (a.balance >= amount); - // // if (state) - // // { - // // a.balance -= amount; - // // b.balance += amount; - // // } - // // return state; - // // } - - // /** - // * @brief Get the balance of the User object - // * - // * @return the balance - // */ - // uint_fast64_t GetBal() const - // { - // return balance; - // } - - // /** - // * @brief Used for Verification of password by external services, this can be used for logging in or signature - // * - // * @param attempt the password - // * @return wether the passwords match - // */ - // bool VerifyPassword(const std::string &attempt) const - // { - // return (password == attempt); - // } + User(uint_fast32_t init_bal, std::string &&init_pass) : balance(init_bal), password(init_pass) {} }; \ No newline at end of file diff --git a/main.cpp b/main.cpp index 5b5d1a7..b96355e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,14 +1,15 @@ #include -#include "user.hpp" +#include "bank.hpp" int main() { - User a(1000, "pass123"); - User b(0, "pass123"); - a.ChangePassword("pass123", "newpass123"); - User::SendFunds(a, b, 250, "newpass123"); - a.GetBal(); - a.VerifyPassword("newpass124"); + Bank.admin_pass = "root"; + std::cout << Bank.AddUser("Twix", "pass123") << '\n'; + std::cout << Bank.GetBal("Twixy") << '\n'; + std::cout << Bank.AdminAddUser("root", "Jollymonsam", 2500, "pass123") << '\n'; + std::cout << Bank.GetBal("Jollymonsam") << '\n'; + std::cout << Bank.SendFunds("Jollymonsam", "Twix", 333, "pass123") << '\n'; + std::cout << Bank.GetBal("Twix") << " | " << Bank.GetBal("Jollymonsam") << '\n'; return 0; } \ No newline at end of file