Advertisement
COSCI539

atwbe

Apr 9th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.98 KB | None | 0 0
  1. // Lab 3 Wilson, Drayden T Th
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. enum MenuOption { INVALID = 0, PRINT, SEARCH, ADD, DELETE, EXIT };
  11.  
  12. class Address
  13. {
  14. public:
  15.     Address();
  16.     void SetAddressFromFile(ifstream& inFile);
  17.     void SetAddressFromUser(); // has user prompts
  18.     void PrintToFile(ofstream& outFile) const;
  19.     void PrintToScreen() const;
  20.     string GetNameLast() const { return nameLast; }
  21.     string GetPhoneNumber() const { return phoneNumber; }
  22. private:
  23.     string nameFirst;
  24.     string nameLast;
  25.     int buildingNumber;
  26.     string street;
  27.     string city;
  28.     string state;
  29.     int zipcode;
  30.     string phoneNumber;
  31.     void CapitalizeMembers();
  32. };
  33.  
  34. class Record
  35. {
  36. public:
  37.     Record() : next(nullptr) {} //  data = new Address; is this needed in {}?
  38.     //~Record() { delete next; cout << "killing record in record\n"; /*@debug@*/}
  39.     Address data;
  40.     Record *next;
  41. };
  42.  
  43. class AddressBook
  44. {
  45. public:
  46.     AddressBook() : head(nullptr), tail(nullptr) {}
  47.     ~AddressBook();
  48.     void FillBookFromFile();
  49.     void AppendRecord(const Address& newData);
  50.     void RemoveRecordAfter(Record *curRecord);
  51.     void PrintBookToFile() const;
  52.     Record *SearchBook(string searchQuery) const;
  53. private:
  54.     Record *head;
  55.     Record *tail;
  56. };
  57.  
  58. void Menu(AddressBook recordList);
  59.  
  60. int main()
  61. {
  62.     AddressBook losAngeles;
  63.    
  64.     losAngeles.FillBookFromFile();
  65.    
  66.     // Record *dummy = losAngeles.SearchBook("Moe"); // @debug@
  67.    
  68.     Menu(losAngeles);
  69.  
  70.     return 0;
  71. }
  72.  
  73. void Menu(AddressBook recordList)
  74. {
  75.     int userMenuSelection = 0;
  76.     Record *searchRecord;
  77.     Address addressToAppend;
  78.     string searchQuery;
  79.  
  80.     do
  81.     {
  82.         cout << "\n[1] Print to file\n"
  83.              << "[2] Search\n"
  84.              << "[3] Add Entry\n"
  85.              << "[4] Delete Entry\n"
  86.              << "[5] Exit\n";
  87.  
  88.         if (userMenuSelection)
  89.         {
  90.             cout << "Select another option: ";
  91.         }
  92.         else
  93.         {
  94.             cout << "Select an option by entering its corresponding number: ";
  95.         }
  96.  
  97.         cin >> userMenuSelection;
  98.  
  99.         switch (userMenuSelection)
  100.         {
  101.         case PRINT:
  102.                 cout << "Printing address book to output file.\n";
  103.                
  104.                 recordList.PrintBookToFile();
  105.                 break;
  106.         case SEARCH:
  107.                 cout << "Enter a last name or phone number to search by: ";
  108.                 cin  >> searchQuery;
  109.                
  110.                 searchRecord = recordList.SearchBook(searchQuery);
  111.                
  112.                 if(searchRecord == nullptr)
  113.                 {
  114.                     cout << "Record not found.\n";
  115.                 }
  116.                 else
  117.                 {
  118.                     cout << "Record found.\n";
  119.                     searchRecord->next->data.PrintToScreen();
  120.                 }
  121.                 break;
  122.         case ADD:
  123.                 cout << "Enter information for new record.\n";
  124.                
  125.                 addressToAppend.SetAddressFromUser();
  126.                 recordList.AppendRecord(addressToAppend);
  127.                
  128.                 cout << "Record Added.\n";
  129.                 break;
  130.         case DELETE:
  131.                 cout << "Enter the last name or phone number of the record you want to delete: ";
  132.                 cin  >> searchQuery;
  133.                
  134.                 searchRecord = recordList.SearchBook(searchQuery);
  135.                
  136.                 if(searchRecord == nullptr)
  137.                 {
  138.                     cout << "Record not found.\n";
  139.                 }
  140.                 else
  141.                 {
  142.                     cout << "Record found. Deleting record.\n";
  143.                     recordList.RemoveRecordAfter(searchRecord);
  144.                 }
  145.                 break;
  146.         case EXIT:
  147.                 cout << "Exiting the program.\n\n";
  148.                 break;
  149.         default:
  150.                 cout << " Invalid menu option selected.";
  151.                 userMenuSelection = 1; // 1 == !INVALID
  152.                 break;
  153.         }
  154.     } while (userMenuSelection != 5);
  155. }
  156.  
  157. Address::Address()
  158. {
  159.     nameFirst = "NoFirst";
  160.     nameLast = "NoLast";
  161.     buildingNumber = -1;
  162.     street = "NoStreet";
  163.     city = "NoCity";
  164.     state = "NoState";
  165.     zipcode = -1;
  166.     phoneNumber = -1;
  167. }
  168.  
  169. void Address::SetAddressFromFile(ifstream& inFile)
  170. {
  171.     inFile >> nameFirst >> nameLast >> buildingNumber >> street >> city >> state >> zipcode >> phoneNumber;
  172.    
  173.     CapitalizeMembers();
  174. }
  175.  
  176. void Address::SetAddressFromUser()
  177. {
  178.     cout << "First Name: ";
  179.     cin  >> nameFirst;
  180.     cout << "Last Name: ";
  181.     cin  >> nameLast;
  182.     cout << "Building Number: ";
  183.     cin  >> buildingNumber;
  184.     cout << "Street: ";
  185.     cin  >> street;
  186.     cout << "City: ";
  187.     cin  >> city;
  188.     cout << "State: ";
  189.     cin  >> state;
  190.     cout << "Zipcode: ";
  191.     cin  >> zipcode;
  192.     cout << "Phone Number: ";
  193.     cin  >> phoneNumber;
  194.    
  195.     CapitalizeMembers();
  196. }
  197.  
  198. void Address::PrintToFile(ofstream& outFile) const
  199. {
  200.     outFile << nameFirst << ' ' << nameLast << "\t\t"
  201.             << buildingNumber << ' ' << street << ' ' << city << ' ' << state << ' ' << zipcode << "\t\t"
  202.             << '(' << phoneNumber.substr(0, 3) << ") "
  203.             << phoneNumber.substr(3, 3) << ' ' << phoneNumber.substr(6, 4) << '\n';
  204. }
  205.  
  206. void Address::PrintToScreen() const
  207. {
  208.     cout << nameFirst << ' ' << nameLast << '\t'
  209.          << buildingNumber << ' ' << street << ' ' << city << ' ' << state << ' ' << zipcode << '\t'
  210.          << '(' << phoneNumber.substr(0, 3) << ") "
  211.          << phoneNumber.substr(3, 3) << ' ' << phoneNumber.substr(6, 4);
  212. }
  213.  
  214. void Address::CapitalizeMembers()
  215. {
  216.     transform(nameFirst.begin(), nameFirst.end(), nameFirst.begin(), ::toupper);
  217.     transform(nameLast.begin(), nameLast.end(), nameLast.begin(), ::toupper);
  218.     transform(street.begin(), street.end(), street.begin(), ::toupper);
  219.     transform(city.begin(), city.end(), city.begin(), ::toupper);
  220.     transform(state.begin(), state.end(), state.begin(), ::toupper);
  221. }
  222.  
  223. AddressBook::~AddressBook()
  224. {
  225.     while(head)
  226.     {
  227.         Record *nextRecord = head->next;
  228.         delete head;
  229.         head = nextRecord;
  230.        
  231.         cout << "killing record in book\n"; /*@debug@*/
  232.     }
  233. }
  234.  
  235. void AddressBook::FillBookFromFile()
  236. {
  237.     ifstream inFile;
  238.     Address curAddress;
  239.    
  240.     inFile.open("input.txt");
  241.    
  242.     if (!inFile)
  243.     {
  244.         cout << "Failed to open input file. Exiting Program.\n";
  245.         exit(1);
  246.     }
  247.     else
  248.     {
  249.         cout << "Input file read successfully.\n";
  250.     }
  251.    
  252.     if(inFile.peek() == EOF)
  253.     {
  254.         cout << "Input file is empty. Exiting Program.\n";
  255.         exit(1);
  256.     }
  257.    
  258.     while(inFile.peek() != EOF)
  259.     {
  260.         curAddress.SetAddressFromFile(inFile);
  261.         AppendRecord(curAddress);
  262.     }
  263.    
  264.     inFile.close();
  265. }
  266.  
  267. void AddressBook::AppendRecord(const Address& newData)
  268. {
  269.     Record *newRecord = new Record;
  270.     newRecord->data = newData;
  271.     newRecord->next = nullptr;
  272.    
  273.     if (head == nullptr)
  274.     {
  275.         head = newRecord;
  276.         tail = newRecord;
  277.     }
  278.     else
  279.     {
  280.         tail->next = newRecord;
  281.         tail = newRecord;
  282.     }
  283. }
  284.  
  285. void AddressBook::RemoveRecordAfter(Record *curRecord)
  286. {
  287.     Record *sucRecord;
  288.    
  289.     if(curRecord == nullptr && head != nullptr) // removing head
  290.     {
  291.         sucRecord = head->next;
  292.         delete head;
  293.         head = sucRecord;
  294.        
  295.         if(sucRecord == nullptr) // removed last record/tail
  296.         {
  297.             tail = nullptr;
  298.         }
  299.     }
  300.     else if(curRecord->next != nullptr)
  301.     {
  302.         sucRecord = curRecord->next->next;
  303.         delete curRecord->next;
  304.         curRecord->next = sucRecord;
  305.        
  306.         if(sucRecord == nullptr) // removed tail
  307.         {
  308.             tail = curRecord;
  309.         }
  310.     }
  311. }
  312.  
  313. void AddressBook::PrintBookToFile() const
  314. {
  315.     ofstream outFile;
  316.     Record *curRecord = head; // does this work?
  317.    
  318.     outFile.open("output.txt");
  319.    
  320.     while(curRecord != nullptr)
  321.     {
  322.         curRecord->data.PrintToFile(outFile);
  323.         curRecord = curRecord->next;
  324.     }
  325.    
  326.     outFile.close();
  327. }
  328.  
  329. Record *AddressBook::SearchBook(string searchQuery) const
  330. {
  331.     Record *curRecord = nullptr;
  332.     curRecord = head;
  333.     string curItem;
  334.    
  335.     while(curRecord != nullptr)
  336.     {
  337.         if (isdigit(searchQuery[0])) // search item is a phone number
  338.         {
  339.             curItem = curRecord->data.GetPhoneNumber();
  340.         }
  341.         else // search item is a last name
  342.         {
  343.             curItem = curRecord->data.GetNameLast();
  344.             transform(searchQuery.begin(), searchQuery.end(), searchQuery.begin(), ::toupper);
  345.         }
  346.        
  347.         if(curItem == searchQuery)
  348.         {
  349.             return curRecord;
  350.         }
  351.        
  352.         curRecord = curRecord->next;
  353.     }
  354.    
  355.     return nullptr; // record not found
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement