#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include "contact.h"
#include <stdlib.h>
#include <cctype>
using namespace std;
//globals
//forward declarations of menu functions
void quit(list<Contact> alist);
Contact makeContact(list<Contact> & alist);
void search(list<Contact> alist);
Contact displayMenu();
void initdata();
void kill();
void listall(list<Contact> alist);
list<Contact> mainlist; // this structure holds the main list of contacts
int main()
{
initdata(); //load data from file and display user menu
}
void quit(list<Contact> alist)
{// quit and save changes to disk
ofstream output;
output.open("lob");
list<Contact>::iterator li=alist.begin();
for(;li!=alist.end();li++)
{
output << *li;
}
output.close();
}
Contact makeContact(list<Contact> & alist)
{// ask user for details of new contact
string name,address,email;
cout << "Please enter a name" << endl;
getline(cin,name);
cout << "Please enter an address" << endl;
getline(cin,address);
cout << "Please enter an email address" << endl;
getline(cin,email);
Contact newcontact(name,address,email);
alist.push_back(newcontact); //insert into mainlist
quit(mainlist);//save back to disk
return newcontact;
}
void search(list<Contact> alist)
{//search mainlist based on name
system("clear");
string term="";
cout << "please enter name" << endl;
getline(cin,term);
list<Contact>::iterator ali;
ali=alist.begin();
for(;ali!=alist.end();ali++)
{
if(ali->returnName()==term)
{
cout << "Your search has returned the following record(s) >> " << endl << endl;
cout << "name: " << ali->returnName() << endl;
cout << "address: " << ali->returnAddress() << endl;
cout << "email: " << ali->returnEmail() << endl << endl;
}
}
getchar();
}
Contact displayMenu()
{
int choice;
Contact acontact;
do{//this loop redraws menu unless quit is selected
cin.clear();
system("clear");
cout << "Welcome to Lobster's Database!" << endl << endl;
cout << "YOU HAVE " << mainlist.size() -1 << " CONTACTS IN YOUR DATABASE" << endl << endl;
cout << "enter your choice >>" << endl;
cout << "1. Add contact" << endl;
cout << "2. List all contacts" << endl;
cout << "3. Search for a contact" << endl;
cout << "4. Delete a contact" << endl;
cout << "5. Quit" << endl;
cin >> choice;
cin.ignore();
if(choice < 1 || choice > 5 )
{
cout << "you must select a NUMBER from the menu list" << endl;
exit(1);
}
switch(choice)
{
case 1: makeContact(mainlist);
break;
case 2: listall(mainlist);
break;
case 3: search(mainlist);
break;
case 4: kill();
break;
case 5: quit(mainlist);
break;
}
}while(choice!=5); //end of do loop
return acontact;
}
void initdata()
{
ifstream input;
input.open("lob");
while(input.good() )
{//recover saved data from disk file and insert into mainlist
Contact acontact;
input >> acontact;
mainlist.push_back(acontact);
}
input.close();
displayMenu(); //show main menu
}
void kill()
{
string temp="";
char ans='z';
list<Contact>::iterator xi;
cout << "This action will permanently delete a selected contact from your database!" << endl;
cout << "name of contact to delete >> " << endl;
getline(cin,temp);
cout << "Are you sure? enter y or n" << endl;
ans=getchar();
cin.ignore();
if(ans=='Y'||ans=='y')
{
xi=mainlist.begin();
for(;xi!=mainlist.end();xi++)
if(xi->returnName()==temp)
{
mainlist.erase(xi);
cout << "contact " << temp << " has been deleted from your list of contacts" << endl << endl;
cout << "Press enter to exit" << endl;
getchar();
quit(mainlist);
return;
}
}
else
cout << "deletion of contact " << temp << " aborted" << endl << endl;
cout << "Press enter to exit" << endl;
getchar();
}
void listall(list<Contact> alist)
{
cout <<"Your list of all contacts: " << endl <<endl;
list<Contact>::iterator li=alist.begin();
for(;li!=alist.end();li++)
{
cout << li->returnName() << endl;
cout << li->returnAddress() << endl;
cout << li->returnEmail() << endl << endl;
}
cout << "Press enter to exit. Have a nice day
" << endl;
getchar();
}