From 419428534a29460d1e58eada8838a646ccbd6e1c Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Thu, 1 Apr 2021 18:54:15 -0700 Subject: [PATCH] :sparkles: User class --- .gitignore | 1 + include/user.hpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 5 ++++- 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 include/user.hpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..600d2d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode \ No newline at end of file diff --git a/include/user.hpp b/include/user.hpp new file mode 100644 index 0000000..4e7c7a1 --- /dev/null +++ b/include/user.hpp @@ -0,0 +1,51 @@ +#pragma once +#include +#include + +class User +{ +private: + uint_fast64_t balance; + std::string password; + + std::mutex bal_lock; + std::mutex pass_lock; + +public: + 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) + { + std::lock_guard lock{pass_lock}; + const bool state = (password == attempt); + if (state) + { + password == new_pass; + } + return state; + } + + friend bool SendFunds(User &a, User &b, uint_fast64_t amount, const std::string &attempt) + { + std::scoped_lock lock{a.bal_lock, a.pass_lock}; + const bool state = (a.password == attempt) && (a.balance >= amount); + if (state) + { + a.balance -= amount; + b.balance += amount; + } + return state; + } + + bool GetBal() + { + std::lock_guard lock{bal_lock}; + return balance; + } + + bool VerifyPassword(const std::string &attempt) //for connected services + { + std::lock_guard lock{pass_lock}; + return (password == attempt); + } +}; \ No newline at end of file diff --git a/main.cpp b/main.cpp index e213194..543241d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,10 @@ #include - +#include "user.hpp" + int main() { + User x(1000, "pass123"); + std::cout << x.ChangePassword("pass123", "newpass123"); return 0; } \ No newline at end of file