Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.98 KB | None | 0 0
  1. ////////////////////////////////////////
  2. // Spam Cupcakes, the pointed view
  3.  
  4. // HINTS AND GUIDELINE
  5. // You should do this assignment in the following order:
  6. // - Make the changes to the Customer class
  7. // - Test the Customer class using the provided test main
  8. // - Make the changes to the CustomerList class
  9. // - Test the CustomerList class using the provided test main
  10. // - Make changes to your main so that it works
  11.  
  12.  
  13.  
  14. ////////////////////////////////////////
  15. // Standard included things
  16. #include<iostream>
  17. #include<iomanip>
  18. #include<fstream>
  19. #include<vector>
  20. using namespace std;
  21.  
  22. ////////////////////////////////////////
  23. // Function prototypes
  24. void print_menu();
  25. void print_vector(vector<string>);
  26.  
  27. ////////////////////////////////////////
  28. // Class definitions
  29.  
  30.  
  31. // A self-referential class to hold customer information
  32. class Customer{
  33.   friend ostream &operator<<(ostream &, Customer);
  34.   friend istream &operator>>(istream &, Customer &);
  35.  
  36. public:
  37.   Customer ();
  38.   Customer (string , string);
  39.   string getName();
  40.   string getEmail();
  41.   Customer * getNextCustomer();
  42.   void setName(string);
  43.   void setEmail(string);
  44.   void setNextCustomer(Customer *);
  45.  
  46. private:
  47.   string name;
  48.   string email;
  49.  
  50.   // The private variable below is the pointer to the next customer on the list.
  51.   // It is null if there is no next customer
  52.   Customer * nextCustomer;
  53. };
  54.  
  55. ostream &operator<<(ostream &output, Customer c){
  56.   output << setw(40) << c.email << setw(40) << c.name;
  57.   return output;
  58. }
  59.  
  60. istream &operator>>(istream &input, Customer & c){
  61.   input >> c.email;
  62.   input.ignore();
  63.   getline(input, c.name);
  64.   return input;
  65. }
  66.  
  67. Customer::Customer(){
  68.   string name = "";
  69.   string email = "";
  70.   nextCustomer = NULL;
  71. }
  72.  
  73. Customer::Customer (string newName, string newEmail){
  74. email = newEmail;
  75. name = newName;
  76. nextCustomer = NULL;
  77. }
  78.  
  79. string Customer::getName(){
  80.   return name;
  81. }
  82.  
  83. string Customer::getEmail(){
  84.   return email;
  85. }
  86.  
  87. Customer * Customer::getNextCustomer(){
  88.   // This returns the next customer on the list
  89.   // The thing it returns should be a pointer
  90.   return nextCustomer;
  91. }
  92.  
  93. void Customer::setName(string newName) {
  94.   name = newName;
  95. }
  96. void Customer::setEmail(string newEmail){
  97.   email = newEmail;
  98. }
  99.  
  100. void Customer::setNextCustomer(Customer * next){
  101. nextCustomer = next;
  102. }
  103.  
  104. // A class to hold the customer email list
  105. // Using pointers, not vector
  106. class CustomerList{
  107.  
  108. public:
  109.   CustomerList();
  110.   CustomerList(string);
  111.   void loadFile(string);
  112.   void showNames();
  113.   void showEmails();
  114.   void showList();
  115.   int countNameAppearances(string);
  116.   int countEmailAppearances(string);
  117.   int deleteCustomer(string);
  118.   void addCustomer(string, string);
  119.  
  120. private:
  121.   // The pointer below is the head of the customerList. It replaces
  122.   // the vector of Customers from the last assignment. In short, this is
  123.   // pointer that points to the first Customer. The first Customer points to
  124.   // the second Customer, and so on. The last Customer on the list points to NULL.
  125.   Customer * head;
  126.  
  127.   // This is a private method that will delete all the elements on the Customer list.
  128.   // This is only used by loadFile to make sure files are loaded twice
  129.   void clearCustomerList();
  130. };
  131.  
  132. CustomerList::CustomerList():head(NULL){
  133.   // Here we need to make sure that our initial head pointer is null
  134.   // write this
  135.   //head = NULL;
  136. }
  137.  
  138. CustomerList::CustomerList(string filename):head(NULL){
  139.   // Here we need to make sure that our initial head pointer is null
  140.   // and call loadFile to read in a file.
  141.   // write this
  142.  // head = NULL;
  143. }
  144.  
  145. void CustomerList::loadFile(string filename) {
  146.  
  147.   // This method loads all the customers in from a file.
  148.   // Don't forget to delete the current list before loading a new one
  149.   // For each line in the file, which represents a single customer
  150.   // we need to:
  151.   // - Create a new customer using the new command
  152.   // - Input from the file into the new Customer using the >> operator
  153.   // - Add the new customer to the list by setting its nextCustomer using setNextCustomer
  154.   //   and then change the head pointer to point at the new Customer
  155.   // - Delete any unused Customer object
  156.   // write this
  157.  
  158. }
  159.  
  160. void CustomerList::showNames(){
  161.  
  162.   // This should traverse the list from the head on down and print out each name
  163.   // from each customer on the list
  164.  
  165.   // write this
  166.  
  167. }
  168.  
  169. void CustomerList::showEmails(){
  170.  
  171.   // This should traverse the list from the head on down and print out each email
  172.   // from each customer on the list
  173.  
  174.   // write this
  175.  
  176. }
  177.  
  178. void CustomerList::showList(){
  179.  
  180.   // This should traverse the list from the head on down and print out each email
  181.   // and name from each customer on the list
  182.  
  183.   // write this
  184. }
  185.  
  186. int CustomerList::countNameAppearances(string name){
  187.     Customer * current = head;
  188.     int counter = 0;
  189.   //
  190.     while (current!= NULL){
  191.         if ((current->getName()) == name){
  192.             counter ++;
  193.         }
  194.         //else{
  195.         //}
  196.         current = current->getNextCustomer();
  197.     }
  198. return counter;
  199. }
  200.  
  201. int CustomerList::countEmailAppearances(string email){
  202.  
  203.   // This should traverse the list from the head on down and count how many times
  204.   // the email provided as a parameter appears on the list.
  205.  
  206.   // write this
  207. }
  208.  
  209. int CustomerList::deleteCustomer(string email){
  210.  
  211.   // This method should traverse down the list and delete any Customer that contains the
  212.   // email provided as a parameter. It should count how many it deleted, and return that number
  213.   // Notice that deleting an item that is the head of the list is different than deleting
  214.   // an item that is not on the head.
  215.  
  216.   // First, delete everything from the head of the list
  217.   // In case you have multiple copies of the same email at the list head
  218.  
  219.   // Next, traverse the rest of the list and delete items from it
  220.   // Make sure not to try and go past a null pointer!
  221.  
  222.   // write this
  223.  
  224. }
  225.  
  226. void CustomerList::addCustomer(string email, string name){
  227. Customer * temp = new Customer (email, name);
  228. //temp = new Customer (email, name);
  229. temp->setNextCustomer(head);
  230. //if (temp->getNextCustomer() == temp){temp->setNextCustomer(NULL);}
  231. head = temp;
  232. }
  233.  
  234. void CustomerList::clearCustomerList(){
  235.   // This should call delete on all the items in the list
  236.   // and leave head set to null
  237.   // write this
  238.  
  239. }
  240.  
  241. ////////////////////////////////////////
  242. // Main function
  243. //
  244. //
  245. /*start of comment out working main
  246. int main () {
  247.  
  248.   CustomerList theList;
  249.  
  250.   char choice;
  251.   bool done = false;
  252.   string input, input2;
  253.   int count = 0;
  254.   vector<string> temp;
  255.   string filename;
  256.  
  257.   do {
  258.  
  259.     // Print the menu for the user
  260.     print_menu();
  261.  
  262.     cin >> choice;
  263.  
  264.     switch(choice){
  265.     case 'L':
  266.     case 'l':
  267.       cout << "What file would you like to load: ";
  268.       cin >> filename;
  269.       theList.loadFile(filename);
  270.       break;
  271.     case 'S':
  272.     case 's':
  273.       theList.showList();
  274.       break;
  275.     case 'M':
  276.     case 'm':
  277.       theList.showNames();
  278.       break;
  279.     case 'R':
  280.     case 'r':
  281.       theList.showEmails();
  282.       break;
  283.     case 'C':
  284.     case 'c':
  285.       cout << "What name do you want to use: ";
  286.       cin.ignore();
  287.       getline(cin,input);
  288.       count = theList.countNameAppearances(input);
  289.       cout << endl << "\"" << input << "\" appeared " << count << " times."        << endl;
  290.       break;
  291.     case 'V':
  292.     case 'v':
  293.       cout << "What email address do you want to use: ";
  294.       cin >> input;
  295.       count = theList.countEmailAppearances(input);
  296.       cout << endl << "\"" << input << "\" appeared " << count << " times."        << endl;
  297.       break;
  298.     case 'A':
  299.     case 'a':
  300.       cout << "What email address do you want to add: ";
  301.       cin >> input;
  302.       cout << "What name goes with that: ";
  303.       cin.ignore();
  304.       getline(cin, input2);
  305.       theList.addCustomer(input, input2);
  306.       break;
  307.     case 'D':
  308.     case 'd':
  309.       cout << "What email address do you want to delete: ";
  310.       cin >> input;
  311.       count = theList.deleteCustomer(input);
  312.       cout << "Deleted " << count << " instances of " << input << endl;
  313.       break;
  314.     case 'X':
  315.     case 'x':
  316.     case 'Q':
  317.     case 'q':
  318.       done = true;
  319.       cout << "Goodbye" << endl;
  320.       break;
  321.     default:
  322.       cout << "Bad command option." << endl;
  323.     } // end switch
  324.   } while (!done);
  325.  
  326.  
  327.  
  328.   return 0;
  329. }
  330. //
  331. */ //end of comment out working main
  332.  
  333.  
  334. ////////////////////////////////////////
  335. // A test main for the Customer class
  336. /* Start of comment out Customer test main
  337. int main(){
  338.  
  339.  
  340.  
  341.   cout << "Testing default constructor: " << endl;
  342.   Customer * cPtr= new Customer;
  343.   if (cPtr->getNextCustomer() != NULL){
  344.     cout << "ERR: nextCustomer is not null. You likely need to" << endl
  345.      << "set it to null in Customer::Customer" << endl;
  346.   }
  347.   else {
  348.     cout << "--> Pass" << endl;
  349.   }
  350.   delete (cPtr);
  351.  
  352.   string name = "Clay";
  353.   string email = "clay@cs.georgetown.edu";
  354.   cout << "Testing constructor with strings: " << endl;
  355.   cPtr= new Customer(name, email);
  356.   if (cPtr->getNextCustomer() != NULL){
  357.     cout << "ERR: nextCustomer is not null. You likely need to" << endl
  358.      << "set it to null in Customer::Customer(string, string)" << endl;
  359.   }
  360.   else {
  361.     cout << "--> Pass" << endl;
  362.   }
  363.  
  364.  
  365.   cout << "Testing setNextCustomer: " << endl;
  366.   Customer * c2Ptr = new Customer;
  367.   cPtr->setNextCustomer(c2Ptr);
  368.   if (cPtr->getNextCustomer() != c2Ptr){
  369.     cout << "ERR: nextCustomer is " << cPtr << " but it should be "
  370.      << c2Ptr << endl;
  371.   }
  372.   else {
  373.     cout << "--> Pass" << endl;
  374.   }
  375.  
  376.  
  377. }
  378.  
  379.  //end of comment out Customer test main
  380.  */
  381.  
  382.  
  383. ////////////////////////////////////////
  384. // Test main for CustomerList class
  385. // /* // start to comment out CustomerList test main
  386. int main(){
  387.  
  388.   CustomerList list;
  389.  
  390.   // test add customers
  391.   cout << "Adding customer 1:" << endl;
  392.   list.addCustomer("clay@cs.georgetown.edu","Clay");
  393.   cout << "Didn't crash" << endl;
  394.   cout << "Adding customer 2:" << endl;
  395.   list.addCustomer("quinn@cs.georgetown.edu","Quinn");
  396.   cout << "Didn't crash" << endl;
  397.   cout << "Adding customer 3:" << endl;
  398.   list.addCustomer("alex@cs.georgetown.edu","Alex");
  399.   cout << "Didn't crash" << endl;
  400.  
  401.   // test show list
  402.   cout << "About to test showList." << endl;
  403.   cout << "To pass, the output should contain these three items (order doesn't matter)"
  404.        << endl << endl;
  405.   cout << setw(10) << "Alex" << setw(25) << "alex@cs.georgetown.edu" <<  endl;
  406.   cout << setw(10) << "Quinn" << setw(25) << "quinn@cs.georgetown.edu" <<  endl;
  407.   cout << setw(10) << "Clay" << setw(25) << "clay@cs.georgetown.edu" <<  endl;
  408.   cout << "----------------------------------------------------------------" << endl;
  409.   list.showList();
  410.   cout << endl << endl;
  411.  
  412.   // test show emails
  413.   cout << "About to test showEmails." << endl;
  414.   cout << "To pass, the output should contain these three items (order doesn't matter)"
  415.        << endl << endl;
  416.   cout << setw(25) << "alex@cs.georgetown.edu" <<  endl;
  417.   cout  << setw(25) << "quinn@cs.georgetown.edu" <<  endl;
  418.   cout << setw(25) << "clay@cs.georgetown.edu" <<  endl;
  419.   cout << "----------------------------------------------------------------" << endl;
  420.   list.showEmails();
  421.   cout << endl << endl;
  422.  
  423.   // test show names
  424.   cout << "About to test showNames." << endl;
  425.   cout << "To pass, the output should contain these three items (order doesn't matter)"
  426.        << endl << endl;
  427.   cout << setw(10) << "Alex"  <<  endl;
  428.   cout << setw(10) << "Quinn" <<   endl;
  429.   cout << setw(10) << "Clay"  <<  endl;
  430.   cout << "----------------------------------------------------------------" << endl;
  431.   list.showNames();
  432.   cout << endl << endl;
  433.  
  434.  
  435.   // test countNameAppearances
  436.   bool pass = true;
  437.   cout << "Testing countNameAppearances: " << endl;
  438.   list.addCustomer("alex@cs.georgetown.edu","Alex");
  439.   list.addCustomer("alex@cs.georgetown.edu","Alex");
  440.   int count = list.countNameAppearances("Alex");
  441.   if (count != 3) {
  442.     cout << "ERR: countNameAppearances should have returned 3, but it returned: "
  443.      << count << endl;
  444.     pass = false;
  445.   }
  446.  
  447.   if (pass) {
  448.     count = list.countNameAppearances("Clay");
  449.     if (count != 1) {
  450.       cout << "ERR: countNameAppearances should have returned 1, but it returned: "
  451.        << count << endl;
  452.       pass = false;
  453.     }
  454.   }
  455.  
  456.   if (!pass){
  457.      cout << "--> Fail" << endl;
  458.   }
  459.   else {
  460.     cout << "--> Pass" << endl;
  461.   }
  462.  
  463.   // test countEmailAppearances
  464.   pass = true;
  465.   cout << "Testing countEmailAppearances: " << endl;
  466.   count = list.countEmailAppearances("alex@cs.georgetown.edu");
  467.   if (count != 3) {
  468.     cout << "ERR: countEmailAppearances should have returned 3, but it returned: "
  469.      << count << endl;
  470.     pass = false;
  471.   }
  472.  
  473.   if (pass) {
  474.     count = list.countEmailAppearances("clay@cs.georgetown.edu");
  475.     if (count != 1) {
  476.       cout << "ERR: countEmailAppearances should have returned 1, but it returned: "
  477.        << count << endl;
  478.       pass = false;
  479.     }
  480.   }
  481.  
  482.   if (!pass){
  483.      cout << "--> Fail" << endl;
  484.   }
  485.   else {
  486.     cout << "--> Pass" << endl;
  487.   }
  488.  
  489.   // Testing delete customer
  490.   cout << "Testing Delete Customer" << endl;
  491.   count = list.deleteCustomer("alex@cs.georgetown.edu");
  492.   if (count != 3){
  493.     cout << "ERR: deleteCustomer should have returned 3, but it returned: "
  494.      << count << endl;
  495.   }
  496.   count = list.deleteCustomer("clay@cs.georgetown.edu");
  497.   if (count != 1){
  498.     cout << "ERR: deleteCustomer should have returned 1, but it returned: "
  499.      << count << endl;
  500.   }
  501.   cout << "If there was no crash, then it is a good sign" << endl;
  502.   cout << "To pass, the output should contain only this item:"
  503.        << endl << endl;
  504.   cout << setw(10) << "Quinn" << setw(25) << "quinn@cs.georgetown.edu" <<  endl;
  505.   cout << "----------------------------------------------------------------" << endl;
  506.   list.showList();
  507.   cout << endl << endl;
  508.  
  509.   cout << "NOTICE: This does not test the loadFile command." << endl
  510.        << "That should be done by running the working main" << endl;
  511.  
  512. }
  513. // */ // end comment out CustomerList test main
  514.  
  515.  
  516. ////////////////////////////////////////
  517. // Print the menu so users know what options they have
  518. void print_menu(){
  519.  
  520.   cout << endl << endl;
  521.   cout << "Menu options:" << endl;
  522.   cout << setw(50) << "Load a file:" << setw(3) << "L" << endl;
  523.   cout << setw(50) << "Show file contents:" << setw(3) << "S" << endl;
  524.   cout << setw(50) << "Show all emails:" << setw(3) << "R" << endl;
  525.   cout << setw(50) << "Show all names:" << setw(3) << "M" << endl;
  526.   cout << setw(50) << "Count name appearances:"
  527.        << setw(3) << "C" << endl;
  528.   cout << setw(50) << "Count email appearances:"
  529.        << setw(3) << "V" << endl;
  530.   cout << setw(50) << "Add a customer:" << setw(3) << "A" << endl;
  531.   cout << setw(50) << "Delete an customer:" << setw(3) << "D" << endl;
  532.   cout << setw(50) << "Exit:" << setw(3) << "X" << endl << endl;
  533.   cout << "Your choice: ";
  534.  
  535. }
  536.  
  537. ////////////////////////////////////////
  538. void print_vector(vector<string> vec){
  539.  
  540.   for (int i = 0; i < vec.size(); i++){
  541.     cout << setw(40) << vec[i] << endl;
  542.   }
  543.  
  544. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement