Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2017
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.40 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 worksa
  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.     name = newName;
  75.     email = newEmail;
  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. {
  134.     // Here we need to make sure that our initial head pointer is null
  135.     // write this
  136. }
  137.  
  138. CustomerList::CustomerList(string filename):head(NULL)
  139. {
  140.     // Here we need to make sure that our initial head pointer is null
  141.     // and call loadFile to read in a file.
  142.     // write this
  143. }
  144.  
  145. void CustomerList::loadFile(string filename) {
  146.     // This method loads all the customers in from a file.
  147.     // Don't forget to delete the current list before loading a new one
  148.     // For each line in the file, which represents a single customer
  149.     // we need to:
  150.     // - Create a new customer using the new command
  151.     // - Input from the file into the new Customer using the >> operator
  152.     // - Add the new customer to the list by setting its nextCustomer using setNextCustomer
  153.     //   and then change the head pointer to point at the new Customer
  154.     // - Delete any unused Customer object
  155.     // write this
  156. }
  157.  
  158. void CustomerList::showNames(){
  159.  
  160.     // This should traverse the list from the head on down and print out each name
  161.     // from each customer on the list
  162.  
  163.     // write this
  164.  
  165. }
  166.  
  167. void CustomerList::showEmails(){
  168.  
  169.     // This should traverse the list from the head on down and print out each email
  170.     // from each customer on the list
  171.  
  172.     // write this
  173.  
  174. }
  175.  
  176. void CustomerList::showList(){
  177.  
  178.     // This should traverse the list from the head on down and print out each email
  179.     // and name from each customer on the list
  180.  
  181.     // write this
  182. }
  183.  
  184. int CustomerList::countNameAppearances(string name){
  185.     Customer* current = head;
  186.     int counter = 0;
  187.  
  188.     while (current != NULL){
  189.         if ((current->getName()) == name){
  190.             counter ++;
  191.         }
  192.         current = current->getNextCustomer();
  193.     }
  194.     return counter;
  195. }
  196.  
  197. int CustomerList::countEmailAppearances(string email){
  198.     Customer* current = head;
  199.     int counter = 0;
  200.  
  201.     while (current != NULL){
  202.         if ((current->getEmail()) == email){
  203.             counter ++;
  204.         }
  205.         current = current->getNextCustomer();
  206.     }
  207.     return counter;
  208. }
  209.  
  210. int CustomerList::deleteCustomer(string email){
  211.     int counter = 0;
  212.     Customer* shadow = head;
  213.     Customer* current = head;
  214.     while (current!=NULL){
  215.         if (( current->getEmail()) == email) {
  216.             if (current == head){
  217.                 shadow = current->getNextCustomer();
  218.                 delete (head);
  219.                 counter++;
  220.                 head = shadow;
  221.                 current = current->getNextCustomer();
  222.             }
  223.             else {
  224.                 shadow = current;
  225.                 current = current->getNextCustomer();
  226.                 current->setNextCustomer(shadow);
  227.                 delete (shadow);
  228.                 counter++;
  229.             }
  230.         }
  231.     }
  232. }
  233.  
  234. void CustomerList::addCustomer(string email, string name){
  235.     Customer* temp = new Customer (name, email);
  236.     //temp = new Customer (email, name);
  237.     temp->setNextCustomer(head);
  238.     head = temp;
  239. }
  240.  
  241. void CustomerList::clearCustomerList(){
  242.     // This should call delete on all the items in the list
  243.     // and leave head set to null
  244.     // write this
  245.  
  246. }
  247.  
  248. ////////////////////////////////////////
  249. // Main function
  250. //
  251. //
  252. /*start of comment out working main
  253.   int main () {
  254.  
  255.   CustomerList theList;
  256.  
  257.   char choice;
  258.   bool done = false;
  259.   string input, input2;
  260.   int count = 0;
  261.   vector<string> temp;
  262.   string filename;
  263.  
  264.   do {
  265.  
  266. // Print the menu for the user
  267. print_menu();
  268.  
  269. cin >> choice;
  270.  
  271. switch(choice){
  272. case 'L':
  273. case 'l':
  274. cout << "What file would you like to load: ";
  275. cin >> filename;
  276. theList.loadFile(filename);
  277. break;
  278. case 'S':
  279. case 's':
  280. theList.showList();
  281. break;
  282. case 'M':
  283. case 'm':
  284. theList.showNames();
  285. break;
  286. case 'R':
  287. case 'r':
  288. theList.showEmails();
  289. break;
  290. case 'C':
  291. case 'c':
  292. cout << "What name do you want to use: ";
  293. cin.ignore();
  294. getline(cin,input);
  295. count = theList.countNameAppearances(input);
  296. cout << endl << "\"" << input << "\" appeared " << count << " times."      << endl;
  297. break;
  298. case 'V':
  299. case 'v':
  300. cout << "What email address do you want to use: ";
  301. cin >> input;
  302. count = theList.countEmailAppearances(input);
  303. cout << endl << "\"" << input << "\" appeared " << count << " times."      << endl;
  304. break;
  305. case 'A':
  306. case 'a':
  307. cout << "What email address do you want to add: ";
  308. cin >> input;
  309. cout << "What name goes with that: ";
  310. cin.ignore();
  311. getline(cin, input2);
  312. theList.addCustomer(input, input2);
  313. break;
  314. case 'D':
  315. case 'd':
  316. cout << "What email address do you want to delete: ";
  317. cin >> input;
  318. count = theList.deleteCustomer(input);
  319. cout << "Deleted " << count << " instances of " << input << endl;
  320. break;
  321. case 'X':
  322. case 'x':
  323. case 'Q':
  324. case 'q':
  325. done = true;
  326. cout << "Goodbye" << endl;
  327. break;
  328. default:
  329. cout << "Bad command option." << endl;
  330. } // end switch
  331. } while (!done);
  332.  
  333.  
  334.  
  335. return 0;
  336. }
  337. //
  338. */ //end of comment out working main
  339.  
  340.  
  341. ////////////////////////////////////////
  342. // A test main for the Customer class
  343. /* Start of comment out Customer test main
  344.    int main(){
  345.  
  346.  
  347.  
  348.    cout << "Testing default constructor: " << endl;
  349.    Customer* cPtr= new Customer;
  350.    if (cPtr->getNextCustomer() != NULL){
  351.    cout << "ERR: nextCustomer is not null. You likely need to" << endl
  352.    << "set it to null in Customer::Customer" << endl;
  353.    }
  354.    else {
  355.    cout << "--> Pass" << endl;
  356.    }
  357.    delete (cPtr);
  358.  
  359.    string name = "Clay";
  360.    string email = "clay@cs.georgetown.edu";
  361.    cout << "Testing constructor with strings: " << endl;
  362.    cPtr= new Customer(name, email);
  363.    if (cPtr->getNextCustomer() != NULL){
  364.    cout << "ERR: nextCustomer is not null. You likely need to" << endl
  365.    << "set it to null in Customer::Customer(string, string)" << endl;
  366.    }
  367.    else {
  368.    cout << "--> Pass" << endl;
  369.    }
  370.  
  371.  
  372.    cout << "Testing setNextCustomer: " << endl;
  373.    Customer* c2Ptr = new Customer;
  374.    cPtr->setNextCustomer(c2Ptr);
  375.    if (cPtr->getNextCustomer() != c2Ptr){
  376.    cout << "ERR: nextCustomer is " << cPtr << " but it should be "
  377.    << c2Ptr << endl;
  378.    }
  379.    else {
  380.    cout << "--> Pass" << endl;
  381.    }
  382.  
  383.  
  384.    }
  385.  
  386. //end of comment out Customer test main
  387.  */
  388.  
  389.  
  390. ////////////////////////////////////////
  391. // Test main for CustomerList class
  392. // /* // start to comment out CustomerList test main
  393. int main(){
  394.  
  395.     CustomerList list;
  396.  
  397.     // test add customers
  398.     cout << "Adding customer 1:" << endl;
  399.     list.addCustomer("clay@cs.georgetown.edu","Clay");
  400.     cout << "Didn't crash" << endl;
  401.     cout << "Adding customer 2:" << endl;
  402.     list.addCustomer("quinn@cs.georgetown.edu","Quinn");
  403.     cout << "Didn't crash" << endl;
  404.     cout << "Adding customer 3:" << endl;
  405.     list.addCustomer("alex@cs.georgetown.edu","Alex");
  406.     cout << "Didn't crash" << endl;
  407.  
  408.     // test show list
  409.     cout << "About to test showList." << endl;
  410.     cout << "To pass, the output should contain these three items (order doesn't matter)"
  411.         << endl << endl;
  412.     cout << setw(10) << "Alex" << setw(25) << "alex@cs.georgetown.edu" <<  endl;
  413.     cout << setw(10) << "Quinn" << setw(25) << "quinn@cs.georgetown.edu" <<  endl;
  414.     cout << setw(10) << "Clay" << setw(25) << "clay@cs.georgetown.edu" <<  endl;
  415.     cout << "----------------------------------------------------------------" << endl;
  416.     list.showList();
  417.     cout << endl << endl;
  418.  
  419.     // test show emails
  420.     cout << "About to test showEmails." << endl;
  421.     cout << "To pass, the output should contain these three items (order doesn't matter)"
  422.         << endl << endl;
  423.     cout << setw(25) << "alex@cs.georgetown.edu" <<  endl;
  424.     cout  << setw(25) << "quinn@cs.georgetown.edu" <<  endl;
  425.     cout << setw(25) << "clay@cs.georgetown.edu" <<  endl;
  426.     cout << "----------------------------------------------------------------" << endl;
  427.     list.showEmails();
  428.     cout << endl << endl;
  429.  
  430.     // test show names
  431.     cout << "About to test showNames." << endl;
  432.     cout << "To pass, the output should contain these three items (order doesn't matter)"
  433.         << endl << endl;
  434.     cout << setw(10) << "Alex"  <<  endl;
  435.     cout << setw(10) << "Quinn" <<   endl;
  436.     cout << setw(10) << "Clay"  <<  endl;
  437.     cout << "----------------------------------------------------------------" << endl;
  438.     list.showNames();
  439.     cout << endl << endl;
  440.  
  441.  
  442.     // test countNameAppearances
  443.     bool pass = true;
  444.     cout << "Testing countNameAppearances: " << endl;
  445.     list.addCustomer("alex@cs.georgetown.edu","Alex");
  446.     list.addCustomer("alex@cs.georgetown.edu","Alex");
  447.     int count = list.countNameAppearances("Alex");
  448.     if (count != 3) {
  449.         cout << "ERR: countNameAppearances should have returned 3, but it returned: "
  450.             << count << endl;
  451.         pass = false;
  452.     }
  453.  
  454.     if (pass) {
  455.         count = list.countNameAppearances("Clay");
  456.         if (count != 1) {
  457.             cout << "ERR: countNameAppearances should have returned 1, but it returned: "
  458.                 << count << endl;
  459.             pass = false;
  460.         }
  461.     }
  462.  
  463.     if (!pass){
  464.         cout << "--> Fail" << endl;
  465.     }
  466.     else {
  467.         cout << "--> Pass" << endl;
  468.     }
  469.  
  470.     // test countEmailAppearances
  471.     pass = true;
  472.     cout << "Testing countEmailAppearances: " << endl;
  473.     count = list.countEmailAppearances("alex@cs.georgetown.edu");
  474.     if (count != 3) {
  475.         cout << "ERR: countEmailAppearances should have returned 3, but it returned: "
  476.             << count << endl;
  477.         pass = false;
  478.     }
  479.  
  480.     if (pass) {
  481.         count = list.countEmailAppearances("clay@cs.georgetown.edu");
  482.         if (count != 1) {
  483.             cout << "ERR: countEmailAppearances should have returned 1, but it returned: "
  484.                 << count << endl;
  485.             pass = false;
  486.         }
  487.     }
  488.  
  489.     if (!pass){
  490.         cout << "--> Fail" << endl;
  491.     }
  492.     else {
  493.         cout << "--> Pass" << endl;
  494.     }
  495.  
  496.     // Testing delete customer
  497.     cout << "Testing Delete Customer" << endl;
  498.     count = list.deleteCustomer("alex@cs.georgetown.edu");
  499.     if (count != 3){
  500.         cout << "ERR: deleteCustomer should have returned 3, but it returned: "
  501.             << count << endl;
  502.     }
  503.     count = list.deleteCustomer("clay@cs.georgetown.edu");
  504.     if (count != 1){
  505.         cout << "ERR: deleteCustomer should have returned 1, but it returned: "
  506.             << count << endl;
  507.     }
  508.     cout << "If there was no crash, then it is a good sign" << endl;
  509.     cout << "To pass, the output should contain only this item:"
  510.         << endl << endl;
  511.     cout << setw(10) << "Quinn" << setw(25) << "quinn@cs.georgetown.edu" <<  endl;
  512.     cout << "----------------------------------------------------------------" << endl;
  513.     list.showList();
  514.     cout << endl << endl;
  515.  
  516.     cout << "NOTICE: This does not test the loadFile command." << endl
  517.         << "That should be done by running the working main" << endl;
  518.  
  519. }
  520. // */ // end comment out CustomerList test main
  521.  
  522.  
  523. ////////////////////////////////////////
  524. // Print the menu so users know what options they have
  525. void print_menu(){
  526.  
  527.     cout << endl << endl;
  528.     cout << "Menu options:" << endl;
  529.     cout << setw(50) << "Load a file:" << setw(3) << "L" << endl;
  530.     cout << setw(50) << "Show file contents:" << setw(3) << "S" << endl;
  531.     cout << setw(50) << "Show all emails:" << setw(3) << "R" << endl;
  532.     cout << setw(50) << "Show all names:" << setw(3) << "M" << endl;
  533.     cout << setw(50) << "Count name appearances:"
  534.         << setw(3) << "C" << endl;
  535.     cout << setw(50) << "Count email appearances:"
  536.         << setw(3) << "V" << endl;
  537.     cout << setw(50) << "Add a customer:" << setw(3) << "A" << endl;
  538.     cout << setw(50) << "Delete an customer:" << setw(3) << "D" << endl;
  539.     cout << setw(50) << "Exit:" << setw(3) << "X" << endl << endl;
  540.     cout << "Your choice: ";
  541.  
  542. }
  543.  
  544. ////////////////////////////////////////
  545. void print_vector(vector<string> vec){
  546.  
  547.     for (int i = 0; i < vec.size(); i++){
  548.         cout << setw(40) << vec[i] << endl;
  549.     }
  550.  
  551. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement