- //Matthew Fong
- //5/11/2011
- //Assignment 5: Phone Contacts
- //Description: design a class for contacts and a vector to hold the object of contacts
- #ifndef CONTACTS_H_INCLUDED
- #define CONTACTS_H_INCLUDED
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- using namespace std;
- class Contacts //Class of contacts holds a last name, first name, and a phone number
- {
- private:
- string lName;
- string fName;
- string phoneNum;
- public:
- void setInfo(string l, string f, string p) //Sets the information
- {
- lName = l;
- fName = f;
- phoneNum = p;
- }
- const string& getlName() //Returns the last name
- {
- return lName;
- }
- const string& getfName() //Returns the first name
- {
- return fName;
- }
- const string& getNum() //Returns the phone number
- {
- return phoneNum;
- }
- };
- class ContactList //Holds the vector of type Contacts
- {
- private:
- vector<Contacts> cList;
- public:
- void addContact(Contacts c) //Adds contacts to the vector
- {
- cList.push_back(c);
- }
- void print() //Prints out all of the elements in the vector
- {
- for(int i = 0; i < cList.size(); i++)
- {
- cout << "Name: " << cList[i].getlName() << " " << cList[i].getfName() << endl;
- cout << "Phone number: " << cList[i].getNum() << endl;
- cout << endl;
- }
- }
- int search(string f) //Searches the vector for the name
- {
- int index = 0;
- int position = -1;
- bool found = false;
- while(index < cList.size() && !found)
- {
- if(cList[index].getfName() == f)
- {
- found = true; //If it finds the first name it will display last and first name, and the phone number
- position = index;
- cout << "Name: " << cList[position].getlName() << " " << cList[position].getfName() << endl;
- cout << "Phone number: " << cList[position].getNum() << endl;
- }
- index++;
- }
- return position;
- }
- int delSearch(string f) //If it finds the name it returns the position
- {
- int index = 0;
- int position = -1;
- bool found = false;
- while(index < cList.size() && !found)
- {
- if(cList[index].getfName() == f)
- {
- found = true;
- position = index;
- }
- index++;
- }
- return position;
- }
- void deleteContact(int pos) //Deletes the elment at the position the user searched for
- {
- cList.erase(cList.begin()+pos);
- }
- };
- class nameError //Error class that displays the error when caught
- {
- private:
- string s;
- public:
- nameError()
- {
- s = "That name is not on the list.";
- }
- void getError()
- {
- cout << "Error: " << s << endl;
- }
- };
- #endif // CONTACTS_H_INCLUDED
- #include "contacts.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- using namespace std;
- void options(char, ContactList&, Contacts&);
- int main()
- {
- string lName;
- string fName;
- string phoneNum;
- char choice = 'Y';
- ifstream inFile;
- ContactList phoneList;
- Contacts phone;
- inFile.open("friends2.txt");
- if(!inFile)
- {
- cout << "File not found." << endl;
- return 0;
- }
- while(inFile >> lName >> fName >> phoneNum)
- {
- phone.setInfo(lName, fName, phoneNum);
- phoneList.addContact(phone);
- }
- phoneList.print();
- do
- {
- cout << "What would you like to do? Add a contact(A), Delete a contact(D)" << endl;
- cout <<"Retrieve a contact(R), or Print(P)?" << endl;
- cin >> choice;
- try
- {
- options(choice, phoneList, phone);
- }
- catch(nameError n)
- {
- n.getError();
- return 1;
- }
- cout << "Would you like to do something else? Y/N" << endl;
- cin >> choice;
- } while(choice == 'Y' || choice == 'y');
- }
- void options(char c, ContactList &pList, Contacts &p)
- {
- string l;
- string f;
- string pNum;
- int pos;
- if(c == 'P' || c == 'p')
- {
- pList.print();
- }
- if(c == 'A' || c == 'a')
- {
- cout << "Add a contact: Last name, First name Phone number." << endl;
- cin >> l >> f >> pNum;
- p.setInfo(l, f, pNum);
- pList.addContact(p);
- }
- if(c == 'R' || c == 'r')
- {
- cout << "Search for a contact by FIRST name." << endl;
- cin >> f;
- pos = pList.search(f);
- if(pos == -1)
- {
- throw nameError();
- }
- }
- if(c == 'D' || c == 'd')
- {
- cout << "Search for the contact by FIRST name." << endl;
- cin >> f;
- pos = pList.delSearch(f);
- pList.deleteContact(pos);
- if(pos == -1)
- {
- throw nameError();
- }
- }
- }