Skip to content

Instantly share code, notes, and snippets.

@mohshbool
Last active December 27, 2021 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohshbool/e4bb9ac6678f1e78bdf6d48a309bb226 to your computer and use it in GitHub Desktop.
Save mohshbool/e4bb9ac6678f1e78bdf6d48a309bb226 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstring>
#include <regex>
#include <queue>
#include <stack>
using namespace std;
class UserAccountType
{
protected:
string userId;
string encrypted_password;
string FirstName;
string LastName;
public:
UserAccountType();
UserAccountType(string, string, string, string);
void setUserId(string);
void setFirstName(string);
void setLastName(string);
void setPassword(string);
string getUserId();
bool isCompleteUserAccount();
string encryptPassword(string);
void printUserInfo();
bool isValidPassword(string);
};
UserAccountType::UserAccountType()
{
userId = "";
FirstName = "";
LastName = "";
encrypted_password = "";
}
UserAccountType::UserAccountType(string id, string fn, string ln, string ep)
{
userId = id;
FirstName = fn;
LastName = ln;
setPassword(ep);
}
void UserAccountType::setUserId(string id)
{
userId = id;
}
void UserAccountType::setFirstName(string n)
{
FirstName = n;
}
void UserAccountType::setLastName(string n)
{
LastName = n;
}
void UserAccountType::setPassword(string pw)
{
if (isValidPassword(pw))
encrypted_password = encryptPassword(pw);
else
{
cout << "Incorrent password: " << pw << endl;
encrypted_password = "";
}
}
string UserAccountType::getUserId()
{
return userId;
}
bool UserAccountType::isCompleteUserAccount()
{
if (userId.compare("") == 0 || FirstName.compare("") == 0 || LastName.compare("") == 0 || encrypted_password.compare("") == 0)
{
cout << "User account is not complete!" << endl;
return false;
}
return true;
}
string UserAccountType::encryptPassword(string old_pw)
{
int n = old_pw.length();
char pw[n];
strcpy(pw, old_pw.c_str());
char temp;
temp = pw[0];
pw[0] = pw[n - 1];
pw[n - 1] = temp;
queue<char> q;
for (int i = 0; i < n; i++)
{
q.push(pw[i]);
}
for (int i = 0; i < n; i++)
{
char current = q.front();
q.pop();
pw[i] = current + (q.front() % 10);
}
stack<char> st;
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
st.push(pw[i]);
}
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
pw[i] = st.top();
st.pop();
}
}
string new_pw = old_pw;
for (int i = 0; i < n; i++)
{
new_pw += pw[i];
}
return new_pw;
}
void UserAccountType::printUserInfo()
{
cout << "User ID: " << userId << endl;
cout << "First Name: " << FirstName << endl;
cout << "Last Name: " << LastName << endl;
cout << "Encrypted Password: " << encrypted_password << endl;
}
bool UserAccountType::isValidPassword(string pw)
{
regex number_expression("[0-9]+"); // at least one number
regex special_char_expression("[*#$]+"); // at least one special case
int found = regex_search(pw, number_expression) + regex_search(pw, special_char_expression) + (pw.length() >= 6) + (pw.length() <= 8);
return found == 4;
}
class UserAccountNode
{
public:
UserAccountType user_account;
UserAccountNode *link;
};
class UserAccountListType
{
UserAccountNode *first;
UserAccountNode *last;
int counter;
public:
UserAccountListType();
void destroyList();
bool isEmpty();
void print();
bool IsUserIdExist(string);
void insertUserAccount(const UserAccountType);
void deleteUserAccount(string);
void printInfo_of_UserId(string);
~UserAccountListType();
};
UserAccountListType::UserAccountListType()
{
first = NULL;
last = NULL;
counter = 0;
}
void UserAccountListType::destroyList()
{
UserAccountListType();
}
bool UserAccountListType::isEmpty()
{
return counter == 0;
}
void UserAccountListType::print()
{
if (isEmpty())
{
cout << "The list is currently empty!";
return;
}
cout << "Current List Users: " << counter << endl;
UserAccountNode *current = first;
int i = 1;
while (current != NULL)
{
cout << "User " << i << " Info: " << endl;
current->user_account.printUserInfo();
current = current->link;
i++;
}
}
bool UserAccountListType::IsUserIdExist(string uid)
{
if (isEmpty())
return false;
UserAccountNode *current = first;
while (current != NULL)
{
if (current->user_account.getUserId() == uid)
{
cout << "User ID " << uid << " already exists!" << endl;
return true;
}
current = current->link;
}
return false;
}
void UserAccountListType::insertUserAccount(UserAccountType user_account)
{
if (!IsUserIdExist(user_account.getUserId()) && user_account.isCompleteUserAccount())
{
UserAccountNode *new_node = new UserAccountNode();
new_node->user_account = user_account;
new_node->link = NULL;
if (isEmpty())
{
first = new_node;
}
else if (counter == 1)
{
first->link = new_node;
}
else
{
last->link = new_node;
}
last = new_node;
counter++;
}
}
void UserAccountListType::deleteUserAccount(string uid)
{
if (isEmpty())
return;
UserAccountNode *prev = NULL;
UserAccountNode *temp = first;
if (temp != NULL && temp->user_account.getUserId() == uid)
{
first = temp->link;
delete temp;
counter--;
return;
}
while (temp != NULL && temp->user_account.getUserId() != uid)
{
prev = temp;
temp = temp->link;
}
if (temp == NULL)
return;
prev->link = temp->link;
counter--;
delete temp;
}
void UserAccountListType::printInfo_of_UserId(string uid)
{
UserAccountNode *current = first;
while (current != NULL)
{
if (current->user_account.getUserId() == uid)
{
current->user_account.printUserInfo();
return;
}
current = current->link;
}
cout << "User ID " << uid << " doesn't exist!" << endl;
}
UserAccountListType::~UserAccountListType()
{
UserAccountNode *current = first;
while (current != NULL)
{
UserAccountNode *next = current->link;
delete current;
current = next;
}
first = NULL;
last = NULL;
}
int main()
{
UserAccountType u1("Ahamd_ali", "Ahmad", "Ali", "J8y$jg8");
UserAccountType u2("Hala98", "Hala", "Mohammad", "Jfj#8djr");
UserAccountType u3("Abeer_1999", "Abeer", "Khalid", "94t*gm9");
UserAccountListType users;
users.insertUserAccount(u1);
users.insertUserAccount(u2);
users.insertUserAccount(u3);
users.deleteUserAccount("Abeer_1999");
users.print();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment