mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-16 16:12:14 +11:00
commit
4056bad70e
3 changed files with 56 additions and 1 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
.vscode
|
||||
51
include/user.hpp
Normal file
51
include/user.hpp
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#pragma once
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
|
||||
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<std::mutex> 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<std::mutex, std::mutex> 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<std::mutex> lock{bal_lock};
|
||||
return balance;
|
||||
}
|
||||
|
||||
bool VerifyPassword(const std::string &attempt) //for connected services
|
||||
{
|
||||
std::lock_guard<std::mutex> lock{pass_lock};
|
||||
return (password == attempt);
|
||||
}
|
||||
};
|
||||
5
main.cpp
5
main.cpp
|
|
@ -1,7 +1,10 @@
|
|||
#include <iostream>
|
||||
|
||||
#include "user.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
User x(1000, "pass123");
|
||||
std::cout << x.ChangePassword("pass123", "newpass123");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in a new issue