#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
using namespace std;
//Two global variables for the "fixed" administrator name and password to be easily changed.
char adminname[] = "admin";
char adminpass[] = "pass";
class userscloud;
//Every object of the class User is one user of the network.
class User {
char name[31],loginname[31],password[31];
int id,friends[10],friendsnum;
public:
char const* getname() const {return name;};
char const* getloginname() const {return loginname;};
char const* getpassword() const {return password;};
int getid() const {return id;};
void addfriend(int totusers);
void delfriend();
void printfriends() const;
User(int ids);//Constructor for User-type objects.
};
//A class that contains an array of pointers to each User object.
class userscloud{
User *usersarray;
public:
userscloud(int num);//Constructor for the userscloud object.
User getuser(int temp) const;
};
User userscloud::getuser(int temp) const{
return usersarray[temp];
}
userscloud::userscloud(int num){
if (num==1){ //If it's the first time here, allocate enough memory.
if ( (usersarray=(User *)malloc (sizeof(User))) == NULL){
cout<<"Not enough memory! \n";
exit(1);
}
}
else{ //Expand allocated memory.
if ( (usersarray = (User *) realloc(usersarray,num*sizeof(User))) == NULL){
cout<<"Not enough memory! \n";
exit(2);
}
}
usersarray[num-1]=User(num);
}
User::User(int ids){ //User class constructor gets user's name, password and login name as inputs.
cout << "Give name, login name and password for the new user ( <=30 characters eatch ) \n";
cin >> name >> loginname >> password;
id=ids;
friendsnum=0;
for (int l=0;l<9;l++){
friends[l]=0;
}
}
int loginpross(userscloud Obj,int totusers){ //Gets a login name as input, finds if this login name exists, if yes then also checks the password.
char temploginname[31], temppassword[31];
cout << "Give your login name. \n";
cin >> temploginname;
if (!strcmp(temploginname,adminname)){
cout << "Admin name was given. Give password to terminate program. \n";
cin >> temppassword;
if (!strcmp(temppassword,adminpass)){
cout << "Admin password right! \n" << "Terminating program.\n";
return 0;
}
cout << "Wrong password!";
return -1;
}
for (int k=0;k<=(totusers-2);k++){
if (!(strcmp(Obj.getuser(k).getname(),temploginname))){
cout << "Give your password. \n";
cin >> temppassword;
if (!strcmp((Obj.getuser(k)).getpassword(),temppassword)){
cout << "Login successful! \n" << "Welcome user: " << k << endl;
return k;
}
cout << "Wrong password!";
return -1;
}
}
cout << "No such user found!";
return -2;
}
void User::addfriend(int totusers){ //In addfriend function the argument totusers is the number of total users + 1 (last user is actually the administrator).
if (friendsnum > 9){
cout << "Friends list is full! \n";
return;
}
int tempid;
cout << "Please enter friend's id number \n";
cin >> tempid;
if (tempid <= 0 || tempid > totusers){ //The id given from input is out of range.
cout << "Invalid id \n";
return;
}
friends[friendsnum]=tempid;
friendsnum++;
}
void User::delfriend(){
if (friendsnum == 0){
cout << "Friend's list is empty! \n";
return;
}
int tempid;
cout << "Which friend do you want to delete? \n";
cin >> tempid;
int counter=-1;
for(int k=0;k<friendsnum;k++){
if (friends[k]==tempid){
friends[k]=friends[friendsnum-1];
k--;
friendsnum--;
counter++;
}
}
if (counter==-1){
cout << "You have no such friend! \n";
}
else{
cout << counter+1 << " friends deleted. \n";
}
}
void User::printfriends() const{
if (friendsnum==0){
cout << "Friend's list is empty! \n";
return;
}
for (int k=0;k<friendsnum;k++){
cout << k+1 << ": " << getname() << endl;
}
}
void searchuser(userscloud Obj,int totusers){
char tempname[31];
cout << "Give the name of the user. \n";
cin >> tempname;
for (int k=0;k<=(totusers-1);k++){
if (!(strcmp((Obj.getuser(k)).getname(),tempname))){
cout << "Requested user found with id: " << (Obj.getuser(k)).getid() << endl;
}
}
}
int main()
{
int newuser = 1;
userscloud Obj(1); //Create a userscloud object.
while (1){
if( !(strcmp(Obj.getuser(newuser-1).getname(),adminname)) && !(strcmp(Obj.getuser(newuser-1).getpassword(),adminpass))){
break;
}
newuser++;
userscloud Obj(newuser);
}
while (1){
int curruser = loginpross(Obj,newuser);
if(curruser == 0){
break;
}
if (curruser>0){
char inp;
cout << "Add friend, Delete friend, Print friends, Search someone \n";
cin >> inp;
switch (inp){
case 'A': case 'a': Obj.getuser(curruser).addfriend(newuser);
break;
case 'D': case 'd': Obj.getuser(curruser).delfriend();
break;
case 'P': case 'p': Obj.getuser(curruser).printfriends();
break;
case 'S': case 's': searchuser(Obj,newuser);
break;
default: cout << "Invalid option";
break;
}
}
}
}