[cpp] dec1

Viewer

  1.  
  2. /*
  3.  *  ContactList.cpp
  4.  *
  5.  *  Interviewer:
  6.  *  Candidate:
  7.  */
  8.  
  9. #include <string>
  10. #include <iostream>
  11. #include "console.h"
  12. #include "simpio.h"
  13. #include "strlib.h"
  14. #include "hashcode.h"
  15.  
  16. using namespace std;
  17.  
  18. class ContactList {
  19. public:
  20.     /*
  21.      * Contructor for ContactList
  22.      */
  23.     ContactList();
  24.  
  25.     /*
  26.      * Destructor for ContactList
  27.      */
  28.     ~ContactList();
  29.  
  30.     /*
  31.      * Method: putContact
  32.      * Usage: list.putContact("NAME", "###-####", "BIRTHDAY");
  33.      * ---------------------------
  34.      * Places a contact with a name, phone number, and birthday, into the ContactList.
  35.      * If a phone number is not in the format ###-####, insert “unknown” as the phone
  36.      * number for the contact, instead of the inputted number.
  37.      * Assume that all birthday strings entered are valid. Also, assume that no two users
  38.      * in the ContactList will ever have the same name, and that names are case sensitive.
  39.      */
  40.     void putContact(string name, string number, string birthday);
  41.  
  42.     /*
  43.      * Method: printContact
  44.      * Usage: list.printContact("NAME");
  45.      * ---------------------------
  46.      * Prints and returns the birthday and phone number of a contact.
  47.      * Format must look like this:
  48.      * "Contact: CONTACT, Phone Number: NUMBER, Birthday: BIRTHDAY"
  49.      *
  50.      * If contact not in list return the following string: "Contact not found."
  51.      */
  52.     string printContact(string name);
  53.  
  54.     /*
  55.      * Method: changePhoneNumber
  56.      * Usage: list.changePhoneNumber("NAME", ###-####);
  57.      * ---------------------------
  58.      * Changes the phone number associated with a contact.
  59.      * If a phone number is not in the format ###-####, insert “unknown” as the phone
  60.      * number for the contact, instead of the inputted number.
  61.      * If the contact is not in the ContactList, throw any string literal exception.
  62.      */
  63.     void changePhoneNumber(string name, string number);
  64.  
  65. private:
  66.     // TODO add private members and function declarations here
  67.     struct Node {
  68.         string name;
  69.         string birthday;
  70.         string number;
  71.         Node* next;
  72.     };
  73.  
  74.     Node** table;
  75.     int NUM_BUCKETS = 1000;
  76.     void deleteLL(Node* head);
  77.     bool checkPhoneNumber(string number);
  78.  
  79. };
  80.  
  81. // TODO implement public (and optional private) functions below
  82.  
  83. ContactList::ContactList() {
  84.       table = new Node*[NUM_BUCKETS]();
  85.       for (int i = 0; i < NUM_BUCKETS; i++) {
  86.           table[i] = nullptr;
  87.       }
  88.     /* TODO: Your implementation here */
  89. }
  90.  
  91. ContactList::~ContactList() {
  92.     for (int i = 0; i < NUM_BUCKETS; i++) {
  93.         deleteLL(table[i]);
  94.     }
  95.     /* TODO: Your implementation here */
  96. }
  97.  
  98. void ContactList::deleteLL(Node* head) {
  99.     Node* temp = head;
  100.     while (temp) {
  101.         head = head->next;
  102.         temp = head;
  103.         delete temp;
  104.     }
  105.  
  106. }
  107.  
  108. bool ContactList::checkPhoneNumber(string number) {
  109.     if (number.size() == 8 && number[3] == '-') {
  110.         for (int i = 0; i < 8; i++) {
  111.             if (!= 3 && !isdigit(number[i])) {
  112.                 return false;
  113.             }
  114.             return true;
  115.         }
  116.     }
  117.  
  118.     return false;
  119. }
  120.  
  121.  
  122.  
  123. void ContactList::putContact(string name, string number, string birthday){
  124.     if (checkPhoneNumber(number)) {
  125.         number = "unknown";
  126.     }
  127.  
  128.     Node* toAdd = new Node();
  129.     toAdd->name = name;
  130.     toAdd->number = number;
  131.     toAdd->birthday = birthday;
  132.  
  133.     Node* temp = table[hashCode(name) % NUM_BUCKETS];
  134.  
  135.  
  136.     table[hashCode(name) % NUM_BUCKETS] = toAdd;
  137.     toAdd->next = temp;
  138. }
  139.  
  140. string ContactList::printContact(string name){
  141.     /* TODO: Your implementation here */
  142.     string output = "Contact not found.";
  143.  
  144.     Node* ref = table[hashCode(name) % NUM_BUCKETS];
  145.  
  146.     output = "Contact: ";
  147.     while (ref) {
  148.         if (ref->name == name) {
  149.             output += ref->name;
  150.             output += ", Phone Number: ";
  151.             output += ref->number;
  152.             output += ", Birthday: ";
  153.             output += ref->birthday;
  154.  
  155.             cout << output << endl;
  156.             break;
  157.         }
  158.    }
  159.  
  160.     return output;
  161.  
  162. }
  163.  
  164. void ContactList::changePhoneNumber(string name, string number){
  165.     if (!checkPhoneNumber(number)) {
  166.         number = "unknown";
  167.     }
  168.  
  169.     Node* ref = table[hashCode(name) % NUM_BUCKETS];
  170.  
  171.     while (ref) {
  172.         ref = ref->next;
  173.         if (ref->name == name) {
  174.             ref->number = number;
  175.         }
  176.     }
  177. }
  178.  
  179.  
  180. /* ------------------------------------------------------------------------------
  181.  * Code beyond this line is a bug-free test harness. You may edit code below this line
  182.  * if desired to add tests, but there are no bugs that need to be fixed in the section
  183.  * below.
  184.  * ------------------------------------------------------------------------------
  185.  */
  186.  
  187. void confirmPrint(ContactList& list, string expectedName, string expectedNumber, string expectedBirth, bool found) {
  188.     bool passed;
  189.     if (!found) {
  190.         cout << "Expected output: " << "Contact not found." << endl << "Student output: ";
  191.         passed = list.printContact(expectedName) == "Contact not found.";
  192.     } else {
  193.         stringstream ss;
  194.         ss << "Contact: " << expectedName;
  195.         ss << ", Phone Number: " << expectedNumber;
  196.         ss << ", Birthday: " << expectedBirth;
  197.         cout << "Expected output: " << ss.str() << endl << "Student output: ";
  198.         passed = list.printContact(expectedName) == ss.str();
  199.     }
  200.  
  201.     if (passed) {
  202.         cout << "TEST CASE PASSED!" << endl;
  203.     } else {
  204.         cout << "TEST CASE FAILED!" << endl;
  205.     }
  206. }
  207.  
  208. int main() {
  209.     string testnames [6] = {"Lisa""Shubham""David""Jeremiah""Clyde""Kate"};
  210.     string testnumbers [6] = {"666-7777""408-625-4897""443-8856""494-89565""563-8425""i forgot"};
  211.     string testbirths [6] = {"1/23/1997""8/21/1965""4/7/1983""8/21/1993""9/21/2000""5/4/1995"};
  212.  
  213.     ContactList list;
  214.  
  215.     cout << "Test Case 1: Add one contact." << endl;
  216.     list.putContact(testnames[0], testnumbers[0], testbirths[0]);
  217.     confirmPrint(list, testnames[0], testnumbers[0], testbirths[0]true);
  218.     cout << endl << endl;
  219.  
  220.     cout << "Test Case 2: Add one contact with an invalid phone number." << endl;
  221.     list.putContact(testnames[1], testnumbers[1], testbirths[1]);
  222.     confirmPrint(list, testnames[1]"unknown",  testbirths[1]true);
  223.     cout << endl << endl;
  224.  
  225.     cout << "Test Case 3: Checking for a nonexistent contact." << endl;
  226.     confirmPrint(list, "Jerry"""""false);
  227.     cout << endl << endl;
  228.  
  229.     cout << "Test Case 4: Adding some more contacts." << endl;
  230.     for (int i = 2; i < 6; i++) {
  231.         list.putContact(testnames[i], testnumbers[i], testbirths[i]);
  232.     }
  233.     for (int i  = 2; i < 6; i++) {
  234.         confirmPrint(list, testnames[i], i % 2 == 0 ? testnumbers[i] : "unknown", testbirths[i]true);
  235.     }
  236.     cout << endl << endl;
  237.  
  238.     cout << "Test Case 5: Changing one phone number to something valid." << endl;
  239.     list.changePhoneNumber(testnames[5], testnumbers[0]);
  240.     confirmPrint(list, testnames[5], testnumbers[0], testbirths[5]true);
  241.     cout << endl << endl;
  242.  
  243.     cout << "Test Case 6: Changing one phone number to something invalid." << endl;
  244.     list.changePhoneNumber(testnames[4], testnumbers[1]);
  245.     confirmPrint(list, testnames[4]"unknown", testbirths[4]true);
  246.     cout << endl << endl;
  247.  
  248.     cout << "Test Case 7: Changing every phone number." << endl;
  249.     for (int i = 2; i < 6; i++) {
  250.         list.changePhoneNumber(testnames[5-i], testnumbers[i]);
  251.     }
  252.     for (int i = 2; i < 6; i++) {
  253.         confirmPrint(list, testnames[5-i], i % 2 == 0 ? testnumbers[i] : "unknown", testbirths[5-i]true);
  254.     }
  255.     cout << endl << endl;
  256.  
  257.     return 0;
  258. }
  259.  

Editor

You can edit this paste and save as new:


File Description
  • dec1
  • Paste Code
  • 02 Dec-2022
  • 7.69 Kb
You can Share it: