Advertisement
Guest User

Untitled

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