瀏覽代碼

Added VehicleManager.cpp

- LogInSystem is now a class
- All source files are moved into src folder
- Updated README.md to reflect the codes' current progress
- renamed vehiclesData.txt to vehiclesData.json
Fixing-travis-yml
BinHong Lee 7 年之前
父節點
當前提交
34e36dd141
共有 15 個文件被更改,包括 1045 次插入857 次删除
  1. +28
    -28
      .gitignore
  2. +110
    -49
      README.md
  3. +0
    -28
      VehicleManager.cpp
  4. +4
    -4
      database.txt
  5. +0
    -17
      main.cpp
  6. +360
    -431
      src/LogInSystem.cpp
  7. +33
    -0
      src/LogInSystem.hpp
  8. +76
    -76
      src/Person.cpp
  9. +27
    -27
      src/Person.hpp
  10. +156
    -141
      src/Vehicle.cpp
  11. +39
    -38
      src/Vehicle.hpp
  12. +143
    -0
      src/VehicleManager.cpp
  13. +19
    -18
      src/VehicleManager.hpp
  14. +50
    -0
      src/main.cpp
  15. +0
    -0
      vehiclesData.json

+ 28
- 28
.gitignore 查看文件

@@ -1,28 +1,28 @@
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app

+ 110
- 49
README.md 查看文件

@@ -1,49 +1,110 @@
#### __* Work in progress__

##### Disclaimer:

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.

The original version of this program (as seen in the initial commit) was a version of my work years ago. I decided that starting from scratch might not be the best idea when it comes to a programming language that I haven't use for a long time. However, as years pass, there are different things that I've learn from other programming languages that I thought would be fun to see how that implementation works with C++ so it will eventually be very much different from what it was.

# Ticketing System

Currently the program only works as a login system that keeps track of users and enable them to login and logout while updating their information.

## Person.hpp / Person.cpp

#### Constructors

| Parameters | Task |
|:-----------|:-----------------------------|
| `()` | Empty constructor. |
| `string newName`<br>`string newEmail`<br>`string newPhoneNo`<br>`string newPassword`<br>`int newId` | Constructor with all parameters to populate all data slots in the object. |

#### Functions

| Function | Task |
|:----------------------|:-------------------------|
| `void setName(string);` | Set `name` to the new given input. |
| `string getName();` | Return `name` to caller. |
| `void setEmail(string);` | Set `email` to the new given input. |
| `string getEmail();` | Return `email` to caller. |
| `void setPhoneNo(string);` | Set `phoneNo` to the new given input. |
| `string getPhoneNo();` | Return `phoneNo` to caller. |
| `void setPassword(string);` | Set `password` to the new given input. |
| `string getPassword();` | Return `password` to the caller. |
| `bool checkPassword(string);` | Return if the given string matches `password`. |
| `int getId();` | Return `id` to the caller. |

## LogInSystem.cpp

#### Functions

| Function | Task |
|:----------------------|:-------------------------|
| `Person getUser(string);` | Takes in a string parameter that will be used to searched for a matching username in the users vector. |
| `Person getUser(int);` | Takes in an int parameter that will be used to searched for a matching id in the users vector. |
| `void login();` | |
| `void loggedIn(Person);` | 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();` | Request all the required information from the user to register for a new account and add them into the users vector. |
| `Person editCredentials(Person);` | Takes 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);` | 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. |
#### __* Work in progress__
##### Disclaimer:
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.
The original version of this program (as seen in the initial commit) was a version of my work years ago. I decided that starting from scratch might not be the best idea when it comes to a programming language that I haven't use for a long time. However, as years pass, there are different things that I've learn from other programming languages that I thought would be fun to see how that implementation works with C++ so it will eventually be very much different from what it was.
# Ticketing System
Currently the program only works as a login system that keeps track of users and enable them to login and logout while updating their information.
## Person.hpp / Person.cpp
#### Constructors
| Parameters | Task |
|:-----------|:-----------------------------|
| `()` | Empty constructor. |
| `(string newName, string newEmail, string newPhoneNo, string newPassword, int newId)` | Constructor with all parameters to populate all data slots in the object. |
#### Functions
| Function | Task |
|:----------------------|:-------------------------|
| `void setName(string);` | Set `name` to the new given input. |
| `string getName();` | Return `name` to caller. |
| `void setEmail(string);` | Set `email` to the new given input. |
| `string getEmail();` | Return `email` to caller. |
| `void setPhoneNo(string);` | Set `phoneNo` to the new given input. |
| `string getPhoneNo();` | Return `phoneNo` to caller. |
| `void setPassword(string);` | Set `password` to the new given input. |
| `string getPassword();` | Return `password` to the caller. |
| `bool checkPassword(string);` | Return if the given string matches `password`. |
| `int getId();` | Return `id` to the caller. |
## LogInSystem.hpp / LogInSystem.cpp
#### Constructors
| Parameters | Task |
|:-----------|:-----------------------------------|
| `()` | Creates an empty `LogInSystem` with nothing in the `users` vector. |
| `(string filename)` | Initialize the `users` vector from the file of the given filename. |
#### Functions
| Function | Task |
|:----------------------|:-------------------------|
| `Person getUser(string);` | Takes in a string parameter that will be used to searched for a matching username in the users vector. |
| `Person getUser(int);` | Takes in an int parameter that will be used to searched for a matching id in the users vector. |
| `int login();` | Verify user credentials and return the user's id. |
| `void loggedIn(Person);` | 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();` | Request all the required information from the user to register for a new account and add them into the users vector. |
| `Person editCredentials(Person);` | Takes 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);` | 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. |
| `string chgUsername();` | Changes the `username` of the `Person`. |
| `string chgPassword(Person);` | Changes the `password` of the `Person`. |
| `string chgEmail();` | Changes the `email` of the `Person`. |
| `string chgPhoneNo();` | Changes the `phoneNo` of the `Person`.
## Vehicle.hpp / Vehicle.cpp
#### Constructors
| Parameters | Task |
|:-----------|:-----------------------------|
| `(int length, int width, int id)` | Basic constructor that fill in the rest of the missing information with "TBA". |
| `(string type, int length, int width, string origin, string destination, string dateNtime, int id)` | Complete constructor with all the information except the seatMap. |
| `(string type, int length, int width, string origin, string destination, string dateNtime, int id, vector< vector<int> > seatMap)` | Complete constructor that is used to read data from the database. |
#### Functions
| Function | Task |
|:---------------------------|:-------------------------|
| `void setType(string type);` | Set `type` to the new given input. |
| `void setOrigin(string origin);` | Set `origin` to the new given input. |
| `void setDestination(string destination);` | Set `destination` to the new given input. |
| `void setDateNTime(string dateNtime);` | Set `dateNtime` to the new given input. |
| `string getType();` | Return `type` to the caller. |
| `int getLength();` | Return `length` to the caller. |
| `int getWidth();` | Return `width` to the caller. |
| `string getOrigin();` | Return `origin` to the caller. |
| `string getDestination();` | Return `destination` to the caller. |
| `string getDateNTime();` | Return `dateNtime` to the caller. |
| `int getId();` | Return `id` to the caller. |
| `void initialize();` | Initialize the `seatMap` to the intended size. |
| `bool bookSeat(int x, int y, int guestId);` | Register a specific seat to the given guestId. Returns if the proces is successful. |
| `bool checkAvailabilty(int x, int y);` | Check if the seat at the given coordinate is still available. |
| `void printMap();` | Print the map with label of 'A' as *available* and 'X' as *not available*. |
| `void printMap(int guestId);` | Takes in the user id and print the map with label of 'A' as *available*, 'U' as the *user* and 'X' as *not available*. |
## VehicleManager.hpp / VehicleManager.cpp
#### Constructors
| Parameters | Task |
|:-----------|:--------------------------------|
| `()` | Empty constructor. Creates an empty vector of `vehicles`. |
| `(string jsonFile)` | Creates a vector of `vehicles` by importing the data from a json file. |
#### Functions
| Function | Task |
|:----------------------------------|:-------------------------|
| `bool add(Vehicle newVehicle);` | Add a new `Vehicle` into the vector. |
| `bool remove(Vehicle toRemove);` | Remove a specific `Vehicle` from the vector. |
| `Vehicle get(int id);` | Get a specific `Vehicle` with the given `id`. |
| `void toJson(string jsonFile);` | Export all data to a json file. |
| `int getId();` | Returns a suggested `id` for the next item. |

+ 0
- 28
VehicleManager.cpp 查看文件

@@ -1,28 +0,0 @@
#include <string>
#include <vector>
#include <iostream>
#include <json/json.h>
#include "VehicleManager.hpp"
using namespace std;

VehicleManager::VehicleManager()
{

}

VehicleManager::VehicleManager(string jsonFile)
{
ifstream jsonFile(jsonFile.c_str());

Json::Reader reader;
Json::Value vehicle;

ifstream json(jsonFile.c_str(), ifstream::binary);

bool parseSuccess = reader.parse(json, vehicle, false);

if(parseSuccess)
{

}
}

+ 4
- 4
database.txt 查看文件

@@ -1,4 +1,4 @@
binhonglee bhlee03655 binhonglee@hotmail.com +13479618886 1
binhong binhong binhong@binhong.me +14802527013 2
who whut wtf_is_happening@binhong.me +999999999 3
testing password testing@binhong.me +601111111111 4
binhonglee bhlee03655 binhonglee@hotmail.com +13479618886 1
binhong binhong binhong@binhong.me +14802527013 2
who whut wtf_is_happening@binhong.me +999999999 3
testing password testing@binhong.me +601111111111 4

+ 0
- 17
main.cpp 查看文件

@@ -1,17 +0,0 @@
#include <string>
#include <vector>
#include <iostream>
#include <json/json.h>
#include "VehicleManager.cpp"
using namespace std;

int main()
{
Vehicle test = Vehicle(10, 5, 0);

if (test.bookSeat(3, 4, 10))
{
cout << "Booking success" << endl;
}
test.printMap(10);
}

LogInSystem.cpp → src/LogInSystem.cpp 查看文件

@@ -1,431 +1,360 @@
/*
* Written by : Bin Hong Lee
* Last edited : Dec 29, 2016
*
*/

#include <iostream>
#include <fstream>
#include <json/json.h>
#include <string>
#include <vector>
#include "Person.cpp"
#include "Vehicle.cpp"
using namespace std;

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

vector<Vehicle> vehicles;
vector<Person> users;
static int wrongPass = 0;
static int globalUserId;

int main()
{
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();
globalUserId = users.back().getId() + 1;

/*
ifstream otherFin("vehiclesData.txt");

while (!otherFin.eof())
{
string type;
int length;
int width;
string origin;
string destination;
string dateNtime;
int id;
otherFin >> type >> length >> width >> origin >> destination >> dateNtime >> id;

vector< vector<int> > seatMap;

for (int i = 0; i < length; i++)
{
vector<int> row;
for (int j = 0; j < width; j++)
{
int temp;
otherFin >> temp;
row.push_back(temp);
}
seatMap.push_back(row);
}

Vehicle newVehicle(type, length, width, origin, destination, dateNtime, id, seatMap);
vehicles.push_back(newVehicle);
}

vehicles.pop_back();
*/

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;
bool loggedOut = 0;

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

ofstream fout("database.txt");

while (!users.empty())
{
fout << users.begin()->getName() << " " << users.begin()->getPassword() << " " << users.begin()->getEmail() << " " << users.begin()->getPhoneNo() << " " << users.begin()->getId() << endl;
users.erase(users.begin());
}

return 0;
}

Person getUser(string name)
{
for (int i = 0; i < users.size(); i++)
{
if (users.at(i).getName() == name)
{
return users.at(i);
}
}

throw 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("");
}

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

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

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

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

wrongPass++;
return false;
}

if (!currentUser.checkPassword(password))
{
cout << "Invalid username or password. Please try again." << endl;

wrongPass++;
return false;
}

loggedIn(currentUser);
return true;
}

Person registration()
{
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 : ";
cin >> password;

//Confirm password
cout << "Confirm password :";
cin >> password2;

//Print error message if both password is not the same
if (password != password2)
cout << "Password unmatched. Please try again.";
//Loop until both password input is the same
} while (password != password2);

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

//Get phone number
cout << "Phone No. : ";
cin >> phoneNo;

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

globalUserId++;

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

return newUser;
}

void loggedIn(Person currentUser)
{
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:
currentUser = editCredentials(currentUser);
break;
case 0:
return;
default:
cout << "Invalid option. Please try again." << endl;
cout << endl;
}
} while(choice != 0);
}

Person editCredentials(Person currentUser)
{
int choice;

cout << "Which of the following to edit?" << endl;
cout << "Username - 1" << endl;
cout << "Password - 2" << endl;
cout << "Email - 3" << endl;
cout << "Phone No. - 4" << endl;
cout << "Exit - 0" << endl;

cin >> choice;

switch (choice)
{
case 1:
currentUser.setName(chgUsername());
update(currentUser);
cout << "Username is updated." << endl;
break;
case 2:
currentUser.setPassword(chgPassword(currentUser));
cout << "Password is updated." << endl;
break;
case 3:
currentUser.setEmail(chgEmail());
cout << "Email is updated." << endl;
break;
case 4:
currentUser.setPhoneNo(chgPhoneNo());
cout << "Phone number is updated." << endl;
break;
}

return currentUser;
}

void update(Person newInfo)
{
int position = users.size() / 2;

while (position < users.size() && position >= 0)
{
if (users.at(position).getId() == newInfo.getId())
{
users.erase(users.begin() + position);
users.insert(users.begin() + position, newInfo);
cout << users.at(position).getName() << endl;
return;
}

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

string chgUsername()
{
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;
}

string chgPassword(Person currentUser)
{
string password;
string newPassword0;
string newPassword1;

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

while (!currentUser.checkPassword(password) && wrongPass < 3)
{
cout << "Wrong password. Please try again." << endl;
wrongPass++;

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

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

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

return newPassword0;
}

string chgEmail()
{
string newEmail;

cout << "Please input the new email : ";
cin >> newEmail;

return newEmail;
}

string chgPhoneNo()
{
string newPhoneNo;

cout << "Please input the new phone no. : ";
cin >> newPhoneNo;

return newPhoneNo;
}
/*
* Written by : Bin Hong Lee
* Last edited : Dec 29, 2016
*
*/
#include <iostream>
#include <fstream>
#include <json/json.h>
#include <string>
#include <vector>
#include "LogInSystem.hpp"
using namespace std;
LogInSystem::LogInSystem()
{
}
LogInSystem::LogInSystem(string filename)
{
ifstream fin(filename);
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();
globalUserId = users.back().getId() + 1;
}
Person LogInSystem::getUser(string name)
{
for (int i = 0; i < users.size(); i++)
{
if (users.at(i).getName() == name)
{
return users.at(i);
}
}
throw invalid_argument("");
}
Person LogInSystem::getUser(int toSearchId)
{
for (int i = 0; i < users.size(); i++)
{
if (users.at(i).getId() == toSearchId)
{
return users.at(i);
}
}
throw invalid_argument("");
}
int LogInSystem::getWrongPass()
{
return wrongPass;
}
int LogInSystem::login()
{
Person currentUser(-1);
string username;
string password;
//If the user already has 3 fail attempt to login
if (wrongPass > 2)
{
//Print error message and exit
cout << "Too many failed login attempt. The program will now be terminated." << endl;
return -1;
}
try
{
//Ask for username
cout << "Username:";
cin >> username;
//Ask for password
cout << "Password:";
cin >> password;
//Get the user
currentUser = getUser(username);
}
catch (invalid_argument ag)
{
cout << "Invalid username or password. Please try again." << endl;
wrongPass++;
return -1;
}
if (!currentUser.checkPassword(password))
{
cout << "Invalid username or password. Please try again." << endl;
wrongPass++;
return -1;
}
return currentUser.getId();
}
Person LogInSystem::registration()
{
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 : ";
cin >> password;
//Confirm password
cout << "Confirm password :";
cin >> password2;
//Print error message if both password is not the same
if (password != password2)
cout << "Password unmatched. Please try again.";
//Loop until both password input is the same
} while (password != password2);
//Get email
cout << "Email : ";
cin >> email;
//Get phone number
cout << "Phone No. : ";
cin >> phoneNo;
//Create and push the new 'Person' into stack
Person newUser(username, password, email, phoneNo, globalUserId);
users.push_back(newUser);
globalUserId++;
//Print success message
cout << "Account is successfully registered." << endl;
return newUser;
}
void LogInSystem::loggedIn(Person currentUser)
{
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:
currentUser = editCredentials(currentUser);
break;
case 0:
return;
default:
cout << "Invalid option. Please try again." << endl;
cout << endl;
}
} while(choice != 0);
}
Person LogInSystem::editCredentials(Person currentUser)
{
int choice;
cout << "Which of the following to edit?" << endl;
cout << "Username - 1" << endl;
cout << "Password - 2" << endl;
cout << "Email - 3" << endl;
cout << "Phone No. - 4" << endl;
cout << "Exit - 0" << endl;
cin >> choice;
switch (choice)
{
case 1:
currentUser.setName(chgUsername());
update(currentUser);
cout << "Username is updated." << endl;
break;
case 2:
currentUser.setPassword(chgPassword(currentUser));
cout << "Password is updated." << endl;
break;
case 3:
currentUser.setEmail(chgEmail());
cout << "Email is updated." << endl;
break;
case 4:
currentUser.setPhoneNo(chgPhoneNo());
cout << "Phone number is updated." << endl;
break;
}
return currentUser;
}
void LogInSystem::update(Person newInfo)
{
int position = users.size() / 2;
while (position < users.size() && position >= 0)
{
if (users.at(position).getId() == newInfo.getId())
{
users.erase(users.begin() + position);
users.insert(users.begin() + position, newInfo);
cout << users.at(position).getName() << endl;
return;
}
if (users.at(position).getId() < newInfo.getId())
{
position++;
}
else
{
position--;
}
}
}
string LogInSystem::chgUsername()
{
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;
}
string LogInSystem::chgPassword(Person currentUser)
{
string password;
string newPassword0;
string newPassword1;
cout << "Please input the current password : ";
cin >> password;
while (!currentUser.checkPassword(password) && wrongPass < 3)
{
cout << "Wrong password. Please try again." << endl;
wrongPass++;
cout << "Please input the current password : ";
cin >> password;
}
do {
cout << "Please input the new password : ";
cin >> newPassword0;
cout << "Please confirm your new password : ";
cin >> newPassword1;
} while(newPassword0 != newPassword1);
return newPassword0;
}
string LogInSystem::chgEmail()
{
string newEmail;
cout << "Please input the new email : ";
cin >> newEmail;
return newEmail;
}
string LogInSystem::chgPhoneNo()
{
string newPhoneNo;
cout << "Please input the new phone no. : ";
cin >> newPhoneNo;
return newPhoneNo;
}
void LogInSystem::toTxtFile(string filename)
{
ofstream fout(filename);
while (!users.empty())
{
fout << users.begin()->getName() << " " << users.begin()->getPassword() << " " << users.begin()->getEmail() << " " << users.begin()->getPhoneNo() << " " << users.begin()->getId() << endl;
users.erase(users.begin());
}
}

+ 33
- 0
src/LogInSystem.hpp 查看文件

@@ -0,0 +1,33 @@
#include <iostream>
#include <fstream>
#include <json/json.h>
#include <string>
#include <vector>
#include "Person.cpp"
using namespace std;
class LogInSystem
{
public:
LogInSystem();
LogInSystem(string filename);
//Declaration of functions
Person getUser(string);
Person getUser(int);
int getWrongPass();
int login();
void loggedIn(Person);
Person registration();
Person editCredentials(Person);
void update(Person);
string chgUsername();
string chgPassword(Person);
string chgEmail();
string chgPhoneNo();
void toTxtFile();
private:
vector<Person> users;
static int wrongPass = 0;
static int globalUserId;
};

Person.cpp → src/Person.cpp 查看文件

@@ -1,76 +1,76 @@
/*
* Written by : BinHong Lee
* Last edited : 5/11/2016
*/
#include <string>
#include "Person.hpp"
using namespace std;
//Empty constructor
Person::Person(int id)
{
this->id = id;
}
//Complete comstructor
Person::Person(string name, string password, string email, string phoneNo, int id)
{
this->name = name;
this->password = password;
this->email = email;
this->phoneNo = phoneNo;
this->id = id;
}
//Getters and setters
void Person::setName(string name)
{
this->name = name;
}
string Person::getName()
{
return name;
}
void Person::setEmail(string email)
{
this->email = email;
}
string Person::getEmail()
{
return email;
}
void Person::setPhoneNo(string phoneNo)
{
this->phoneNo = phoneNo;
}
string Person::getPhoneNo()
{
return phoneNo;
}
void Person::setPassword(string password)
{
this->password = password;
}
string Person::getPassword()
{
return password;
}
//"password" do not have getter but only a checker instead
bool Person::checkPassword(string password)
{
return (this->password == password);
}
int Person::getId()
{
return id;
}
/*
* Written by : BinHong Lee
* Last edited : 5/11/2016
*/
#include <string>
#include "Person.hpp"
using namespace std;
//Empty constructor
Person::Person(int id)
{
this->id = id;
}
//Complete comstructor
Person::Person(string name, string password, string email, string phoneNo, int id)
{
this->name = name;
this->password = password;
this->email = email;
this->phoneNo = phoneNo;
this->id = id;
}
//Getters and setters
void Person::setName(string name)
{
this->name = name;
}
string Person::getName()
{
return name;
}
void Person::setEmail(string email)
{
this->email = email;
}
string Person::getEmail()
{
return email;
}
void Person::setPhoneNo(string phoneNo)
{
this->phoneNo = phoneNo;
}
string Person::getPhoneNo()
{
return phoneNo;
}
void Person::setPassword(string password)
{
this->password = password;
}
string Person::getPassword()
{
return password;
}
//"password" do not have getter but only a checker instead
bool Person::checkPassword(string password)
{
return (this->password == password);
}
int Person::getId()
{
return id;
}

Person.hpp → src/Person.hpp 查看文件

@@ -1,27 +1,27 @@
#include <string>
#include <iostream>
using namespace std;
class Person
{
private :
string name;
string password;
string email;
string phoneNo;
int id;
public :
Person(int id);
Person(string, string, string, string, int);
void setName(string);
string getName();
void setEmail(string);
string getEmail();
void setPhoneNo(string);
string getPhoneNo();
void setPassword(string);
string getPassword();
bool checkPassword(string);
int getId();
};
#include <string>
#include <iostream>
using namespace std;
class Person
{
private :
string name;
string password;
string email;
string phoneNo;
int id;
public :
Person(int id);
Person(string, string, string, string, int);
void setName(string);
string getName();
void setEmail(string);
string getEmail();
void setPhoneNo(string);
string getPhoneNo();
void setPassword(string);
string getPassword();
bool checkPassword(string);
int getId();
};

Vehicle.cpp → src/Vehicle.cpp 查看文件

@@ -1,141 +1,156 @@
#include <string>
#include "Vehicle.hpp"
using namespace std;

Vehicle::Vehicle(int length, int width, int id)
{
type = "TBA";
origin = "TBA";
destination = "TBA";
dateNtime = "TBA";
this->length = length;
this->width = width;
this->id = id;
initialize();
}

Vehicle::Vehicle(string type, int length, int width, string origin, string destination, string dateNtime, int id)
{
this->type = type;
this->length = length;
this->width = width;
this->origin = origin;
this->destination = destination;
this->dateNtime = dateNtime;
this->id = id;
initialize();
}

Vehicle::Vehicle(string type, int length, int width, string origin, string destination, string dateNtime, int id, vector< vector<int> > seatMap)
{
this->type = type;
this->length = length;
this->width = width;
this->origin = origin;
this->destination = destination;
this->dateNtime = dateNtime;
this->id = id;
this->seatMap = seatMap;
}

void Vehicle::setType(string type)
{
this->type = type;
}

void Vehicle::setOrigin(string origin)
{
this->origin = origin;
}

void Vehicle::setDestination(string destination)
{
this->destination = destination;
}

string Vehicle::getType()
{
return type;
}

int Vehicle::getLength()
{
return length;
}

int Vehicle::getWidth()
{
return width;
}

string Vehicle::getOrigin()
{
return origin;
}

string Vehicle::getDestination()
{
return destination;
}

string Vehicle::getDateNTime()
{
return dateNtime;
}

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

void Vehicle::initialize()
{
for (int i = 0; i < length; i++)
{
vector<int> row;
for (int j = 0; j < width; j++)
{
row.push_back(-1);
}
seatMap.push_back(row);
}
}

bool Vehicle::bookSeat(int x, int y, int guestId)
{
if (seatMap[x][y] == -1)
{
seatMap[x][y] = guestId;
return true;
}

return false;
}

bool Vehicle::checkAvailabilty(int x, int y)
{
return seatMap[x][y] == -1;
}

void Vehicle::printMap(int guestId)
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < width; j++)
{
if (seatMap[i][j] == -1)
{
cout << "A ";
}
else if (seatMap[i][j] == guestId)
{
cout << "U ";
}
else
{
cout << "X ";
}
}
cout << endl;
}
}
#include <string>
#include "Vehicle.hpp"
using namespace std;
Vehicle::Vehicle(int length, int width, int id)
{
type = "TBA";
origin = "TBA";
destination = "TBA";
dateNtime = "TBA";
this->length = length;
this->width = width;
this->id = id;
initialize();
}
Vehicle::Vehicle(string type, int length, int width, string origin, string destination, string dateNtime, int id)
{
this->type = type;
this->length = length;
this->width = width;
this->origin = origin;
this->destination = destination;
this->dateNtime = dateNtime;
this->id = id;
initialize();
}
Vehicle::Vehicle(string type, int length, int width, string origin, string destination, string dateNtime, int id, vector< vector<int> > seatMap)
{
this->type = type;
this->length = length;
this->width = width;
this->origin = origin;
this->destination = destination;
this->dateNtime = dateNtime;
this->id = id;
this->seatMap = seatMap;
}
void Vehicle::setType(string type)
{
this->type = type;
}
void Vehicle::setOrigin(string origin)
{
this->origin = origin;
}
void Vehicle::setDestination(string destination)
{
this->destination = destination;
}
void Vehicle::setDateNTime(string dateNtime)
{
this->dateNtime = dateNtime;
}
string Vehicle::getType()
{
return type;
}
int Vehicle::getLength()
{
return length;
}
int Vehicle::getWidth()
{
return width;
}
string Vehicle::getOrigin()
{
return origin;
}
string Vehicle::getDestination()
{
return destination;
}
string Vehicle::getDateNTime()
{
return dateNtime;
}
int Vehicle::getId()
{
return id;
}
void Vehicle::initialize()
{
for (int i = 0; i < length; i++)
{
vector<int> row;
for (int j = 0; j < width; j++)
{
row.push_back(-1);
}
seatMap.push_back(row);
}
}
bool Vehicle::bookSeat(int x, int y, int guestId)
{
if (checkAvailabilty(x, y))
{
seatMap[x][y] = guestId;
return true;
}
return false;
}
bool Vehicle::checkAvailabilty(int x, int y)
{
return seatMap[x][y] == -1;
}
int Vehicle::getGuest(int x, int y)
{
return seatMap[x][y];
}
void Vehicle::printMap()
{
printMap(-2);
}
void Vehicle::printMap(int guestId)
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < width; j++)
{
if (seatMap[i][j] == -1)
{
cout << "A ";
}
else if (seatMap[i][j] == guestId)
{
cout << "U ";
}
else
{
cout << "X ";
}
}
cout << endl;
}
}

Vehicle.hpp → src/Vehicle.hpp 查看文件

@@ -1,38 +1,39 @@
#include <string>
#include <iostream>
#include <vector>
using namespace std;

class Vehicle
{
private:
string type;
int length;
int width;
string origin;
string destination;
string dateNtime;
int id;
vector< vector<int> > seatMap;

public:
Vehicle(int length, int width, int id);
Vehicle(string type, int length, int width, string origin, string destination, string dateNtime, int id);
Vehicle(string type, int length, int width, string origin, string destination, string dateNtime, int id, vector< vector<int> > seatMap);
void setType(string type);
void setLength(int length);
void setWidth(int width);
void setOrigin(string origin);
void setDestination(string origin);
string getType();
int getLength();
int getWidth();
string getOrigin();
string getDestination();
string getDateNTime();
int getId();
void initialize();
bool bookSeat(int x, int y, int guestId);
bool checkAvailabilty(int x, int y);
void printMap(int guestId);
};
#include <string>
#include <iostream>
#include <vector>
using namespace std;
class Vehicle
{
private:
string type;
int length;
int width;
string origin;
string destination;
string dateNtime;
int id;
vector< vector<int> > seatMap;
public:
Vehicle(int length, int width, int id);
Vehicle(string type, int length, int width, string origin, string destination, string dateNtime, int id);
Vehicle(string type, int length, int width, string origin, string destination, string dateNtime, int id, vector< vector<int> > seatMap);
void setType(string type);
void setOrigin(string origin);
void setDestination(string origin);
void setDateNTime(string dateNtime);
string getType();
int getLength();
int getWidth();
string getOrigin();
string getDestination();
string getDateNTime();
int getId();
void initialize();
bool bookSeat(int x, int y, int guestId);
bool checkAvailabilty(int x, int y);
int getGuest(int x, int y);
void printMap();
void printMap(int guestId);
};

+ 143
- 0
src/VehicleManager.cpp 查看文件

@@ -0,0 +1,143 @@
#include <string>
#include <vector>
#include <iostream>
#include <json/json.h>
#include "VehicleManager.hpp"
using namespace std;
VehicleManager::VehicleManager()
{
}
VehicleManager::VehicleManager(string jsonFile)
{
ifstream jsonFile(jsonFile.c_str());
Json::Reader reader;
Json::Value vehicles;
ifstream json(jsonFile.c_str(), ifstream::binary);
bool parseSuccess = reader.parse(json, vehicles, false);
if(parseSuccess)
{
Json::Value::Members mbr = vehicles.getMemberNames();
for (vector<string>::const_iterator i = mbr.begin(); i != mbr.end(); i++)
{
Json::Value jsonVehicle = vehicles[*i];
string vhcStr = "vehicle";
if (vhcStr.compare(*i) == 0)
{
string placeholder = jsonVehicle.asString();
}
else
{
string type = jsonVehicle["Type"].asString();
int length = jsonVehicle["Length"].asInt();
int width = jsonVehicle["Width"].asInt();
string origin = jsonVehicle["Origin"].asString();
string destination = jsonVehicle["Destination"].asString();
string dateNtime = jsonVehicle["DateNTime"].asString();
int id = jsonVehicle["id"].asInt();
Json::Value jsonSeatMap = jsonVehicle["SeatMap"];
vector< vector<int> > seatMap;
for (int i = 0; i < length; i++)
{
vector<int> row;
for (int j = 0; i < width; j++)
{
row.push_back(jsonSeatMap[i][j].asInt());
}
seatMap.push_back(row);
}
Vehicle vehicle(type, length, width, origin, destination, dateNtime, id, seatMap);
add(vehicle);
}
}
}
}
bool VehicleManager::add(Vehicle newVehicle)
{
if (newVehicle.getId() <= vehicle.back().getId())
{
return false;
}
vehicles.push_back(newVehicle);
return (vehicles.back().getId() == newVehicle.getId());
}
bool VehicleManager::remove(Vehicle toRemove)
{
for (int i = 0; i < vehicles.size(); i++)
{
if (vehicles.at(i).getId() == toRemove.getId())
{
vehicles.erase(vehicles.begin() + i);
return true;
}
}
return false;
}
Vehicle VehicleManager::get(int id)
{
for (int i = 0; i < vehicles.size(); i++)
{
if (vehicles.at(i).getId() == id)
{
return vehicles.at(i);
}
}
}
void VehicleManager::toJson(string jsonFile)
{
Json::Value jsonLib;
for (int i = 0; i < vehicles.size(); i++)
{
Json::Value jsonVehicle;
Vehicle vehicle = vehicles.at(i);
jsonVehicle["Type"] = vehicle.getType();
jsonVehicle["Length"] = vehicle.getLength();
jsonVehicle["Width"] = vehicle.getWidth();
jsonVehicle["Origin"] = vehicle.getOrigin();
jsonVehicle["Destination"] = vehicle.getDestination();
jsonVehicle["DateNTime"] = vehicle.getDateNTime();
jsonVehicle["id"] = vehicle.getId();
int* seatMap = new int[vehicle.getLength()][vehicle.getWidth()];
for (int i = 0; i < vehicle.getLength(); i++)
{
for (int j = 0; j < vehicle.getWidth(); j++)
{
seatMap[i][j] = vehicle.getGuest(i, y);
}
}
jsonVehicle["SeatMap"] = seatMap;
}
Json::StyledStreamWriter ssw(" ");
ofstream jsonOutFile(jsonFile.c_str(), ofstream::binary);
ssw.write(jsonOutFile, jsonLib);
}
int VehicleManager::getId()
{
return (vehicles.back().getId() + 1);
}

VehicleManager.hpp → src/VehicleManager.hpp 查看文件

@@ -1,18 +1,19 @@
#include <string>
#include <vector>
#include "Vehicle.hpp"
using namespace std;

class VehicleManager
{
private:
vector<Vehicle> vehicles;

public:
VehicleManager();
VehicleManager(string jsonFile);
bool add(Vehicle newVehicle);
bool remove(Vehicle toRemove);
Vehicle get(int id);
void toJson(string jsonFile);
}
#include <string>
#include <vector>
#include "Vehicle.hpp"
using namespace std;
class VehicleManager
{
private:
vector<Vehicle> vehicles;
public:
VehicleManager();
VehicleManager(string jsonFile);
bool add(Vehicle newVehicle);
bool remove(Vehicle toRemove);
Vehicle get(int id);
void toJson(string jsonFile);
int getId();
}

+ 50
- 0
src/main.cpp 查看文件

@@ -0,0 +1,50 @@
#include <string>
#include <vector>
#include <iostream>
#include <json/json.h>
#include "VehicleManager.cpp"
#include "LogInSystem.cpp"
#include "Person.cpp"
using namespace std;
int main()
{
LogInSystem users("database.txt");
VehicleManager vehicles("vehiclesData.json");
int currentUser = -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 (users.getWrongPass() < 3 && currentUser != -1)
{
currentUser = users.login();
}
break;
case 2: users.loggedIn(users.registration()); break;
case 0: break;
default:
cout << "Invalid input. Please try again." << endl;
}
}
Vehicle test = Vehicle(10, 5, 0);
if (test.bookSeat(3, 4, 10))
{
cout << "Booking success" << endl;
}
test.printMap(10);
}

vehiclesData.txt → vehiclesData.json 查看文件


Loading…
取消
儲存