Przeglądaj źródła

Major overhaul

- Refactored the entire LogInSystem.cpp
- Renamed Person.h into Person.hpp
- Added "int id" into Person.hpp
- Added getter and setter for id in Person.cpp
- Updated README.md with description of each functions
Fixing-travis-yml
BinHong Lee 8 lat temu
rodzic
commit
a0d13e3888
4 zmienionych plików z 303 dodań i 299 usunięć
  1. +266
    -291
      LogInSystem.cpp
  2. +7
    -5
      Person.cpp
  3. +3
    -1
      Person.hpp
  4. +27
    -2
      README.md

+ 266
- 291
LogInSystem.cpp Wyświetl plik

@@ -1,201 +1,192 @@
/*
* Written by : BinHong Lee
* Last edited : 7/7/2016
* Written by : Bin Hong Lee
* Last edited : Dec 18, 2016
*
*/

#include <iostream>
#include <string>
#include <stack>
#include <fstream>
#include "Person.cpp"
#include <string>
#include <vector>
#include <algorithm>
#include "Person.hpp"
using namespace std;

//Declaration of functions
void getUser(string);
Person getUser(string);
Person getUser(int);
void login();
void adminLogin();
void loggedIn();
void registration();
void editCredentials();
void loggedIn(Person);
Person registration();
Person editCredentials(Person);
void update(Person);
void chgUsername();
void chgPassword();
void chgEmail();
void chgPhoneNo();
//void quit();

//Stacks to store the users' personal data
stack<Person> users;

//Declaration of place holding variables
Person currentUser;
string username;
string password;
string password2;
string email;
string phoneNo;
int wrongPass = 0;
string chgUsername();
string chgPassword();
string chgEmail();
string chgPhoneNo();

vector<Person> users;
static int wrongPass = 0;
static int globalId;

int main()
{
//Reading from file to build the database
ifstream fin("database.txt");
while (!fin.eof())
{
//Reading in line-by-line into the placeholding variables
fin >> username >> password >> email >> phoneNo;

//Create the Person and push it into the stack
Person temp(username, password, email, phoneNo);
users.push(temp);
}

//Pop off the additional data added into the stack due to the empty line
users.pop();

getOption:
//Display user input options
cout << "Please choose one of the following options :" << endl;
cout << "Log In - 1" << endl;
cout << "Registration - 2" << endl;
cout << "Exit - 0" << endl;

//Get user input
int choice;
cin >> choice;

//Switch according to user input
switch (choice)
{
case 1:
login();
break;
case 2:
registration();
break;
case 0:
break;
default:
//Display error message
cout << "Invalid input. Please try again." << endl;
goto getOption;
}
ifstream fin("database.txt");
while (!fin.eof())
{
string username;
string password;
string email;
string phoneNo;
int id;

fin >> username >> password >> email >> phoneNo >> id;

Person newPerson(username, password, email, phoneNo, id);
users.push_back(newPerson);
}

users.pop_back();

globalId = users.back().getId() + 1;
int userOption = -1;

while (userOption != 0)
{
cout << "Please choose one of the following options :" << endl;
cout << "Log In - 1" << endl;
cout << "Registration - 2" << endl;
cout << "Exit - 0" << endl;

cin >> userOption;

switch (userOption)
{
case 1:
while (wrongPass < 3)
{
login();
}
break;
case 2: loggedIn(registration()); break;
default:
cout << "Invalid input. Please try again." << endl;
}
}

ofstream fout("database.txt");

while (!users.empty())
{
fout << users.back().getName() << " " << users.back().getPassword() << " " << users.back().getEmail() << " " << users.back().getPhoneNo() << " " << users.back().getId() << endl;
users.pop_back();
}

//Saving data into file
ofstream fout("database.txt");

//Check if the stack is empty
while (!users.empty())
{
//Move the data into the files each Person per line
fout << users.top().getName() << " " << users.top().getPassword() << " " << users.top().getEmail() << " " << users.top().getPhoneNo() << " " << endl;
//Pop the Person out of stack after the data is moved
users.pop();
}

//End the program
return 0;
}

void getUser()
Person getUser(string name)
{
//Duplicate the current stack to be checked through
stack<Person> temp = users;

//Loop while the temporary stack is not empty
while (!temp.empty())
{
//Check if the username match the query
//If so, set it as the currentUser
if (users.top().getName() == username) currentUser = users.top();

//Pop the checked user
users.pop();
}
for (int i = 0; i < users.size(); i++)
{
if (users.at(i).getName() == name)
{
return users.at(i);
}
}

throw invalid_argument("");
}

//Throw invalid_argument error to be caught if the person is not found
throw std::invalid_argument("");
Person getUser(int toSearchId)
{
for (int i = 0; i < users.size(); i++)
{
if (users.at(i).getId() == toSearchId)
{
return users.at(i);
}
}

throw invalid_argument("");
}

void login()
{
//If the user already has 3 fail attempt to login
if (wrongPass >= 3)
{
//Print error message and exit
cout << "Too much failed login attempt. The program will now be terminated." << endl;
return;
}

try {
//Ask for username
Person currentUser;
string username;
string password;
//If the user already has 3 fail attempt to login
if (wrongPass > 2)
{
//Print error message and exit
cout << "Too much failed login attempt. The program will now be terminated." << endl;
return;
}

try
{
//Ask for username
cout << "Username:";
cin >> username;

//Ask for password
//Ask for password
cout << "Password:";
cin >> password;

//Get the user
getUser();
currentUser = getUser(username);
}
catch (invalid_argument ag)
{
cout << "Invalid username or password. Please try again.";

} catch (invalid_argument inae) {
//Print error message
cout << "Invalid username or password. Please try again.";
//Increase wrongPass count that indicate the amount of times of invalid
//credentials input by the user
wrongPass++;
login();
}
wrongPass++;
return;
}

//Check if the password is correct
if (!currentUser.checkPassword(password))
{
//Print error message
cout << "Invalid username or password. Please try again.";
//Increase wrongPass count that indicate the amount of times of invalid
//credentials input by the user
wrongPass++;
login();
}
if (!currentUser.checkPassword(password))
{
cout << "Invalid username or password. Please try again.";

wrongPass++;
return;
}

//User is successfully logged in
loggedIn();
loggedIn(currentUser);
}

void registration()
Person registration()
{
//Initialize boolean
bool available;

do
{
try
{
//Get username
cout << "Username: ";
cin >> username;

//Check if username is already in use
//If not, it will throw an invalid_argument error to be caught
getUser();

//Print error message
cout << "Username unavailable. Please try again." << endl;
//Set available as false and continue the loop
available = false;
//Catch the invalid_argument error thrown by getUser()
} catch (invalid_argument inae)
{
//Set available to true and quit the loop
available = true;
}
} while (available == false);

//Print success message
cout << "Username is available." << endl;

do
bool available;
string username;
string password;
string password2;
string email;
string phoneNo;

do
{
try
{
cout << "Username: ";
cin >> username;

getUser(username);

cout << "Username taken. Please try again." << endl;

available = false;
}
catch (invalid_argument ag)
{
available = true;
}
} while (!available);

cout << "Username is available." << endl;

do
{
//Get password
cout << "Password : ";
@@ -211,7 +202,7 @@ void registration()
//Loop until both password input is the same
} while (password != password2);

//Get email
//Get email
cout << "Email : ";
cin >> email;

@@ -220,55 +211,51 @@ void registration()
cin >> phoneNo;

//Create and push the new 'Person' into stack
Person newUser(username, password, email, phoneNo);
users.push(newUser);
Person newUser(username, password, email, phoneNo, globalId);
users.push_back(newUser);

globalId++;

//Print success message
cout << "Account is successfully registered." << endl;
//Print success message
cout << "Account is successfully registered." << endl;

//Automatically login user
currentUser = newUser;
loggedIn();
return newUser;
}

void loggedIn()
void loggedIn(Person currentUser)
{
//Declare space to save user's option
int choice;

//Print options
cout << "Please choose one of the following option :" << endl;
cout << "View credentials - 1" << endl;
cout << "Edit credentials - 2" << endl;
cout << "Exit - 0" << endl;
//Get user selection
cin >> choice;

switch (choice)
{
case 1:
//Print out user credentials
cout << "Username : " << currentUser.getName() << "\nEmail : " << currentUser.getEmail() << "\nPhone No. : " << currentUser.getPhoneNo() << endl;
//Nested call back into the menu
loggedIn();
break;
case 2:
editCredentials();
//Nested call back into the menu
loggedIn();
break;
case 0:
break;
default:
cout << "Invalid option. Please try again." << endl;
cout << endl;
loggedIn();
}
int choice;

do {
cout << "Please choose one of the following options: " << endl;
cout << "View credentials - 1" << endl;
cout << "Edit credentials - 2" << endl;
cout << "Exit - 0" << endl;

cin >> choice;

switch(choice)
{
case 1:
cout << "Username : " << currentUser.getName() << endl;
cout << "Email : " << currentUser.getEmail() << endl;
cout << "Phone No.: " << currentUser.getPhoneNo() << endl;
break;
case 2:
editCredentials(currentUser);
break;
case 0:
return;
default:
cout << "Invalid option. Please try again." << endl;
cout << endl;
}
} while(choice != 0);
}

void editCredentials()
Person editCredentials(Person currentUser)
{
int choice;
int choice;

cout << "Which of the following to edit?" << endl;
cout << "Username - 1" << endl;
@@ -282,84 +269,86 @@ void editCredentials()
switch (choice)
{
case 1:
chgUsername();
currentUser.setName(chgUsername());
cout << "Username is updated." << endl;
break;
case 2:
chgPassword();
currentUser.setPassword(chgPassword());
cout << "Password is updated." << endl;
break;
case 3:
chgEmail();
currentUser.setEmail(chgEmail());
cout << "Email is updated." << endl;
break;
case 4:
chgPhoneNo();
currentUser.setPhoneNo(chgPhoneNo());
cout << "Phone number is updated." << endl;
break;
}

return currentUser;
}

void update(Person newUser)
void update(Person newInfo)
{
stack<Person> newUsers;

while (!users.empty())
{
if (users.top().getName() == currentUser.getName())
{
cout << "replaced" << endl;
newUsers.push(newUser);
}
else
{
newUsers.push(users.top());
}

users.pop();
}

users = newUsers;
currentUser = newUser;
int position = users.size() / 2;

while (position < users.size() && position >= 0)
{
if (users.at(position).getId() == newInfo.getId())
{
Person oldInfo = users.at(position);
replace(users.begin(), users.end(), oldInfo, newInfo);
return;
}

if (users.at(position).getId() < newInfo.getId())
{
position++;
}
else
{
position--;
}
}
}

void chgUsername()
string chgUsername()
{
bool available;
Person newUsers;

do
{
available = true;
cout << available << endl;
cout << "Username : ";
cin >> username;

stack<Person> temp = users;

while (!temp.empty())
{
if (temp.top().getName() == username)
{
cout << "Username unavailable. Please try again." << endl;
cout << temp.top().getName() << " " << temp.top().getPhoneNo() << endl;
available = false;
break;
}

temp.pop();
}
} while (available == false);

cout << "Username is available." << endl;
Person newPerson(username, currentUser.getPassword(), currentUser.getEmail(), currentUser.getPhoneNo());
update(newPerson);

cout << "Username is updated." << endl;
bool availablility;
string newUsername;

do
{
availablility = true;

cout << "New Username: ";
cin >> newUsername;

for (int i = 0; i < users.size(); i++)
{
if (users.at(i).getName() == newUsername)
{
availablility = false;
break;
}
}
} while (!availablility);

cout << "Username is available." << endl;
return newUsername;
}

void chgPassword()
string chgPassword(Person currentUser)
{
cout << "Please input the current password : ";
string password;
string newPassword0;
string newPassword1;

cout << "Please input the current password : ";
cin >> password;

while (!currentUser.checkPassword(password))
while (!currentUser.checkPassword(password) && wrongPass < 3)
{
cout << "Wrong password. Please try again." << endl;
wrongPass++;
@@ -370,45 +359,31 @@ void chgPassword()

do {
cout << "Please input the new password : ";
cin >> password;
cin >> newPassword0;

cout << "Please confirm your new password : ";
cin >> password2;
} while(password2 != password);
cin >> newPassword1;
} while(newPassword0 != newPassword1);

currentUser.setPassword(password);
update(currentUser);
return newPassword0;
}

void chgEmail()
string chgEmail()
{
cout << "Please input the new email : ";
cin >> email;
string newEmail;

currentUser.setEmail(email);
update(currentUser);
}
cout << "Please input the new email : ";
cin >> newEmail;

void chgPhoneNo()
{
cout << "Please input the new phone no. : ";
cin >> phoneNo;

currentUser.setPhoneNo(phoneNo);
update(currentUser);
return newEmail;
}

/*
void quit()
string chgPhoneNo()
{
ofstream fout("database.txt");
string newPhoneNo;

while (!users.empty())
{
fout << users.top().getName() << " " << users.top().getPassword() << " " << users.top().getEmail() << " " << users.top().getPhoneNo() << " " << endl;
users.pop();
}
cout << "Please input the new phone no. : ";
cin >> newPhoneNo;

exit (EXIT_SUCCESS);
return newPhoneNo;
}
*/

+ 7
- 5
Person.cpp Wyświetl plik

@@ -4,16 +4,13 @@
*/

#include <string>
#include "Person.h"
#include "Person.hpp"
using namespace std;

//Empty constructor
Person::Person()
{
name = "unknown";
password = "";
email = "unknown";
phoneNo = "555-5555";

}

//Complete comstructor
@@ -71,3 +68,8 @@ bool Person::checkPassword(string password)
{
return (this->password == password);
}

int Person::getId()
{
return id;
}

Person.h → Person.hpp Wyświetl plik

@@ -9,10 +9,11 @@ class Person
string password;
string email;
string phoneNo;
int id;

public :
Person();
Person(string, string, string, string);
Person(string, string, string, string, int);
void setName(string);
string getName();
void setEmail(string);
@@ -22,4 +23,5 @@ class Person
void setPassword(string);
string getPassword();
bool checkPassword(string);
int getId();
};

+ 27
- 2
README.md Wyświetl plik

@@ -1,4 +1,7 @@
## Bus Ticket Management System
#### __* Currently not working as intended__
#### __* Work in progress__

# Bus Ticket Management System

This program is written solely on personal time with the purpose to pick up the C++ programming language once again after a long long time since I last wrote anything with it.

@@ -6,4 +9,26 @@ The original version of this program (as seen in the initial commit) was a versi

Thank you for your time in reading this. I sure hope someone would find this useful in someway someday.

BinHong Lee.
## LogInSystem.cpp

### Person getUser(string);
This function will take in a string parameter that will be used to searched for a matching username in the users vector.

### Person getUser(int);
This function will take in an int parameter that will be used to searched for a matching id in the users vector.

### void login();

### void loggedIn(Person);
This function takes in a Person parameter after the user has logged in as that identity. It provides the user options to display and modify the information of that specific identity in the database (vector).

### Person registration();
This function request all the required information from the user to register for a new account and add them into the users vector.

### Person editCredentials(Person);
This function will take in a Person parameter and make edit to it according to the user's intention. It will then return the updated Person to the caller.

### void update(Person);
This function will take in a Person parameter that is to be updated into the users vector. It will search for the Person in the vector with matching ID and replace it.

## Person.hpp / Person.cpp

Ładowanie…
Anuluj
Zapisz