#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include <iomanip>
using namespace std;
struct Person {
string firstName;
string lastName;
int birthday;
int phone;
string email;
};
void addData(vector<Person>& v);
void displayData(vector<Person> person);
void displayRaw(Person person);
void sort(vector<Person>& v, string input);
char getMenuResponse();
void save(ofstream& fout, vector<Person>&p);
void read(vector<Person>& p);
int find(vector<Person>& v, string & t );
void remove(vector<Person>&v, string rnames);
int main()
{
vector<Person> v;
bool run = true;
string name;
string ch;
ofstream fout;
ifstream fin;
int index;
do {
switch (getMenuResponse()) {
case 'A':
system("cls");
addData(v);
break;
case 'F':
cout << "Enter Last Name to find" << endl;
getline(cin, name);
cout << "The index is " ;
index = find(v, name);
cout << index;
break;
case 'D':
system("cls");
cout << "Display the Address Book Records: " << endl;
displayData(v);
break;
case 'S':
system("cls");
cout << "Would you like to sort by Name or Last_Name ?" << endl;
getline(cin, name);
sort(v, name);
cout << endl;
displayData(v);
break;
case 'R':
cout << "Enter the string to remove " << endl;
getline(cin, name);
//remove(v, name);
cout << endl;
displayData(v);
break;
case 'K':
save(fout, v);
break;
case 'G':
read(v);
break;
case 'Q':
run = false;
break;
default:
cout << "That is NOT a valid choice" << endl;
}
} while (run);
cout << endl;
cout << "Thank you for using our program, HAVE A NICE DAY!" << endl;
}
char getMenuResponse()
{
char response;
cout << endl
<< "Make your selection" << endl
<< "(A)dd Record, (D)isplay name, (F)ind name, (S)ort names, (R)emove name , (K)Save File, (G)et File, (Q)uit" << endl
<< "> ";
cin >> response;
cin.ignore(256, '\n');
return toupper(response);
}
void addData(vector<Person>& v)
{
while (true) {
Person aPerson;
if (v.size() < 2) {
cout << "Address Book Program - " << v.size() << " Item stored " << endl;
}
else {
cout << "Address Book Program - " << v.size() << " Items stored " << endl;
}
cout << "Enter Address Book Records" << endl;
string str;
stringstream ss(str);
string firstName;
cout << "Please enter your first name(quit to stop): " << endl;
getline(cin, firstName);
if (firstName == "quit") {
break;
}
string surname;
cout << "Please enter your last name: ";
getline(cin, surname);
string birthday;
cout << "Please Enter your Date of Birth YYMMDD: ";
getline(cin, birthday);
ss << birthday;
ss >> aPerson.birthday;
string phone;
cout << "Please Enter your Phone Number: ";
getline(cin,phone);
ss << phone;
ss >> aPerson.phone;
ss.clear();
string email;
cout << "Please enter your Email Address: ";
getline(cin, email);
aPerson.firstName = firstName;
aPerson.lastName = surname;
aPerson.email = email;
v.push_back(aPerson);
ss.clear();
}
}
void displayData(vector<Person> v)
{
int length = v.size();
for (int x = 0; x < length; x++) {
displayRaw(v[x]);
}
if (length < 1) {
cout << "Nothing to display" << endl;
}
}
void displayRaw(Person person)
{
const char separator = ' ';
const int nameWidth = 12;
const int numWidth = 10;
cout << left << setw(nameWidth) << setfill(separator) << "First Name:";
cout << left << setw(nameWidth) << setfill(separator) << "Last Name:";
cout << left << setw(numWidth) << setfill(separator) << "DOB:";
cout << left << setw(numWidth) << setfill(separator) << "Phone:";
cout << left << setw(numWidth) << setfill(separator) << "Email Address:" << endl;
cout << left << setw(nameWidth) << setfill(separator) << person.firstName;
cout << left << setw(nameWidth) << setfill(separator) << person.lastName;
cout << left << setw(numWidth) << setfill(separator) << person.birthday;
cout << left << setw(numWidth) << setfill(separator) << person.phone;
cout << left << setw(numWidth) << setfill(separator) << person.email << endl;
}
void sort(vector<Person>& v, string input)
{
if (input == "Name")
{
cout << " Sorting Address Book by First Name " << endl;
int n = v.size();
for (int i = 1; i < n; i++)
for (int j = 0; j < n - i; j++)
if (v[j].lastName > v[j + 1].lastName)
swap(v[j].firstName, v[j + 1].firstName);
}else if (input == "Last_Name")
{
cout << " Sorting Address Book by Last Name " << endl;
int n = v.size();
for (int i = 1; i < n; i++)
for (int j = 0; j < n - i; j++)
if (v[j].firstName > v[j + 1].firstName)
swap(v[j].lastName, v[j + 1].lastName);
}
}
void save(ofstream& fout, vector<Person>&p)
{
fout.open("addressbook.txt");
if (!fout.fail()) {
system("cls");
cout << "Saving Records to the addressbook.txt file ";
int length = p.size();
for (int x = 0; x < length; x++)
{
fout << p[x].firstName << ";";
fout << p[x].lastName << ";";
fout << p[x].birthday << ";";
fout << p[x].phone << ";";
fout << p[x].email;
if (x<(length-1))
{
fout << endl;
}
}
cout << endl;
cout << length << " records writen to the disc." << endl;
fout.close();
}
}
void read(vector<Person>& p)
{
ifstream fin("addressbook.txt");
if (!fin.fail())
{
system("cls");
cout << "Reading inventory from the disc ";
}
while(!fin.eof())
{
Person aPerson;
string str;
stringstream ss(str);
string firstName;
getline(fin, firstName, ';');
string surname;
getline(fin, surname, ';');
string birthday;
getline(fin, birthday, ';');
ss << birthday;
ss >> aPerson.birthday;
string phone;
getline(fin, phone, ';');
ss << phone;
ss >> aPerson.phone;
ss.clear();
string email;
getline(fin, email);
aPerson.firstName = firstName;
aPerson.lastName = surname;
aPerson.email = email;
p.push_back(aPerson);
ss.clear();
}
}
int find(vector<Person> &v, string & t )
{
for(int i=0; i< v.size(); i++)
{
if(v[i].lastName ==t)
{
return i;
}
}
// If execution gets here, the string was not found.
return -1;
}
void remove(vector<Person>&v, string rnames)
{
v.erase(remove(v.begin(), v.end(), rnames), v.end());
}