Don't like ads? PRO users don't see any ads ;-)
Guest

CS2370 Prog 5

By: mattfong on May 12th, 2011  |  syntax: None  |  size: 5.38 KB  |  hits: 53  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Matthew Fong
  2. //5/11/2011
  3. //Assignment 5: Phone Contacts
  4. //Description: design a class for contacts and a vector to hold the object of contacts
  5.  
  6. #ifndef CONTACTS_H_INCLUDED
  7. #define CONTACTS_H_INCLUDED
  8. #include <iostream>
  9. #include <string>
  10. #include <vector>
  11. #include <fstream>
  12. using namespace std;
  13.  
  14. class Contacts //Class of contacts holds a last name, first name, and a phone number
  15. {
  16.     private:
  17.         string lName;
  18.         string fName;
  19.         string phoneNum;
  20.  
  21.     public:
  22.         void setInfo(string l, string f, string p) //Sets the information
  23.         {
  24.             lName = l;
  25.             fName = f;
  26.             phoneNum = p;
  27.         }
  28.  
  29.         const string& getlName() //Returns the last name
  30.         {
  31.             return lName;
  32.         }
  33.  
  34.         const string& getfName() //Returns the first name
  35.         {
  36.             return fName;
  37.         }
  38.  
  39.         const string& getNum() //Returns the phone number
  40.         {
  41.             return phoneNum;
  42.         }
  43. };
  44.  
  45. class ContactList //Holds the vector of type Contacts
  46. {
  47.     private:
  48.         vector<Contacts> cList;
  49.  
  50.     public:
  51.         void addContact(Contacts c) //Adds contacts to the vector
  52.         {
  53.             cList.push_back(c);
  54.         }
  55.  
  56.         void print() //Prints out all of the elements in the vector
  57.         {
  58.             for(int i = 0; i < cList.size(); i++)
  59.             {
  60.                 cout << "Name: " << cList[i].getlName() << " " << cList[i].getfName() << endl;
  61.                 cout << "Phone number: " << cList[i].getNum() << endl;
  62.                 cout << endl;
  63.             }
  64.         }
  65.  
  66.         int search(string f) //Searches the vector for the name
  67.         {
  68.             int index = 0;
  69.             int position = -1;
  70.             bool found = false;
  71.             while(index < cList.size() && !found)
  72.             {
  73.                 if(cList[index].getfName() == f)
  74.                 {
  75.                     found = true; //If it finds the first name it will display last and first name, and the phone number
  76.                     position = index;
  77.                     cout << "Name: " << cList[position].getlName() << " " << cList[position].getfName() << endl;
  78.                     cout << "Phone number: " << cList[position].getNum() << endl;
  79.                 }
  80.                 index++;
  81.             }
  82.             return position;
  83.         }
  84.  
  85.         int delSearch(string f) //If it finds the name it returns the position
  86.         {
  87.             int index = 0;
  88.             int position = -1;
  89.             bool found = false;
  90.             while(index < cList.size() && !found)
  91.             {
  92.                 if(cList[index].getfName() == f)
  93.                 {
  94.                     found = true;
  95.                     position = index;
  96.                 }
  97.                 index++;
  98.             }
  99.             return position;
  100.         }
  101.  
  102.         void deleteContact(int pos) //Deletes the elment at the position the user searched for
  103.         {
  104.             cList.erase(cList.begin()+pos);
  105.         }
  106. };
  107.  
  108. class nameError //Error class that displays the error when caught
  109. {
  110.     private:
  111.         string s;
  112.     public:
  113.         nameError()
  114.         {
  115.             s = "That name is not on the list.";
  116.         }
  117.  
  118.         void getError()
  119.         {
  120.             cout << "Error: " << s << endl;
  121.         }
  122. };
  123. #endif // CONTACTS_H_INCLUDED
  124.  
  125.  
  126. #include "contacts.h"
  127. #include <iostream>
  128. #include <fstream>
  129. #include <string>
  130. #include <vector>
  131. using namespace std;
  132.  
  133. void options(char, ContactList&, Contacts&);
  134.  
  135. int main()
  136. {
  137.     string lName;
  138.     string fName;
  139.     string phoneNum;
  140.     char choice = 'Y';
  141.     ifstream inFile;
  142.     ContactList phoneList;
  143.     Contacts phone;
  144.  
  145.     inFile.open("friends2.txt");
  146.     if(!inFile)
  147.     {
  148.         cout << "File not found." << endl;
  149.         return 0;
  150.     }
  151.  
  152.     while(inFile >> lName >> fName >> phoneNum)
  153.     {
  154.         phone.setInfo(lName, fName, phoneNum);
  155.         phoneList.addContact(phone);
  156.     }
  157.  
  158.     phoneList.print();
  159.  
  160.     do
  161.     {
  162.         cout << "What would you like to do? Add a contact(A), Delete a contact(D)" << endl;
  163.         cout <<"Retrieve a contact(R), or Print(P)?" << endl;
  164.         cin >> choice;
  165.         try
  166.         {
  167.             options(choice, phoneList, phone);
  168.         }
  169.  
  170.         catch(nameError n)
  171.         {
  172.             n.getError();
  173.             return 1;
  174.         }
  175.  
  176.         cout << "Would you like to do something else? Y/N" << endl;
  177.         cin >> choice;
  178.     } while(choice == 'Y' || choice == 'y');
  179.  
  180. }
  181.  
  182. void options(char c, ContactList &pList, Contacts &p)
  183. {
  184.     string l;
  185.     string f;
  186.     string pNum;
  187.     int pos;
  188.  
  189.     if(c == 'P' || c == 'p')
  190.     {
  191.         pList.print();
  192.     }
  193.  
  194.     if(c == 'A' || c == 'a')
  195.     {
  196.         cout << "Add a contact: Last name, First name Phone number." << endl;
  197.         cin >> l >> f >> pNum;
  198.         p.setInfo(l, f, pNum);
  199.         pList.addContact(p);
  200.     }
  201.  
  202.     if(c == 'R' || c == 'r')
  203.     {
  204.         cout << "Search for a contact by FIRST name." << endl;
  205.         cin >> f;
  206.         pos = pList.search(f);
  207.  
  208.         if(pos == -1)
  209.         {
  210.             throw nameError();
  211.         }
  212.     }
  213.  
  214.     if(c == 'D' || c == 'd')
  215.     {
  216.         cout << "Search for the contact by FIRST name." << endl;
  217.         cin >> f;
  218.         pos = pList.delSearch(f);
  219.         pList.deleteContact(pos);
  220.  
  221.         if(pos == -1)
  222.         {
  223.             throw nameError();
  224.         }
  225.     }
  226. }