Advertisement
Guest User

Untitled

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