I built a banking system in C++ (still a lot of features to do but (I decided to post it here to see what can I change so I wont make those mistakes in the future).
User header and c++ files
Header
#pragma once
#include <string>
class User
{
private:
int id;
std::string username;
std::string password;
std::string balance;
public:
User(int id, std::string username, std::string &password, int balance);
~User();
int GetId();
std::string GetUsername();
};
CPP
#include <iostream>
#include "HashPassword.h"
#include "User.h"
User::User(int uId, std::string uUsername, std::string& uPassword, int uBalance)
{
id = uId;
username = uUsername;
password = uPassword;
HashPassword(password);
std::cout << password << '\n';
balance = uBalance;
std::cout << balance << '\n';
}
User::~User()
{
}
int User::GetId()
{
return id;
}
std::string User::GetUsername()
{
return username;
}
all of this is pretty easy to understand (I hope) but a couple of point: first of all the HashPassword method will be posted here so scroll down a bit for the implementation, but basically I used the picosha2 header file I got from GitHub and it does some magic I don't understand and hashes a string using sha256 algorithm.
second point I got a weird feeling about how I use pointers and pass by reference here so I would like some directions on that topic.
HashPassword
#include "HashPassword.h"
void HashPassword(std::string& password)
{
std::vector<unsigned char> hash(picosha2::k_digest_size);
picosha2::hash256(password.begin(), password.end(), hash.begin(), hash.end());
password = picosha2::bytes_to_hex_string(hash.begin(), hash.end());
}
this is my hashing process I took straight from the GitHub example and I have a broad idea on how it works but I don't understand it deeply.
my main concern here is the use of pointers and references.
UserRepository
#include <iostream>
#include <vector>
#include "User.h"
class UserRepository
{
private:
std::vector<User> userRepo;
public:
UserRepository()
{
}
~UserRepository(){}
void AddUser(std::string& username, std::string &password, int& balance)
{
int id = userRepo.size() + 1;
User user(id, username, password, balance);
userRepo.push_back(user);
}
void PrintAllUsers()
{
for(User user : userRepo)
{
std::cout << user.GetUsername() << ", ";
}
std::cout << '\n';
}
User* FindUserByUsername(std::string username)
{
for (User user : userRepo)
{
if (user.GetUsername() == username)
{
User* p_user = &user;
return p_user;
}
}
return NULL;
}
};
Here im using a vector as a repository that, and I just add users to it via AddUser and the id gets generated automatically based on how many users there are so if there are 7 users and another one joins then his id will be 8.
and this is it for now, working on adding authentication process and changing the way I go about using pointers and references.