Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////
- // Spam Cupcakes, the pointed view
- // HINTS AND GUIDELINE
- // You should do this assignment in the following order:
- // - Make the changes to the Customer class
- // - Test the Customer class using the provided test main
- // - Make the changes to the CustomerList class
- // - Test the CustomerList class using the provided test main
- // - Make changes to your main so that it worksa
- ////////////////////////////////////////
- // Standard included things
- #include<iostream>
- #include<iomanip>
- #include<fstream>
- #include<vector>
- using namespace std;
- ////////////////////////////////////////
- // Function prototypes
- void print_menu();
- void print_vector(vector<string>);
- ////////////////////////////////////////
- // Class definitions
- // A self-referential class to hold customer information
- class Customer{
- friend ostream &operator<<(ostream &, Customer);
- friend istream &operator>>(istream &, Customer &);
- public:
- Customer ();
- Customer (string , string);
- string getName();
- string getEmail();
- Customer* getNextCustomer();
- void setName(string);
- void setEmail(string);
- void setNextCustomer(Customer *);
- private:
- string name;
- string email;
- // The private variable below is the pointer to the next customer on the list.
- // It is null if there is no next customer
- Customer* nextCustomer;
- };
- ostream &operator<<(ostream &output, Customer c){
- output << setw(40) << c.email << setw(40) << c.name;
- return output;
- }
- istream &operator>>(istream &input, Customer & c){
- input >> c.email;
- input.ignore();
- getline(input, c.name);
- return input;
- }
- Customer::Customer(){
- string name = "";
- string email = "";
- nextCustomer = NULL;
- }
- Customer::Customer (string newName, string newEmail){
- name = newName;
- email = newEmail;
- nextCustomer = NULL;
- }
- string Customer::getName(){
- return name;
- }
- string Customer::getEmail(){
- return email;
- }
- Customer* Customer::getNextCustomer(){
- // This returns the next customer on the list
- // The thing it returns should be a pointer
- return nextCustomer;
- }
- void Customer::setName(string newName) {
- name = newName;
- }
- void Customer::setEmail(string newEmail){
- email = newEmail;
- }
- void Customer::setNextCustomer(Customer* next){
- nextCustomer = next;
- }
- // A class to hold the customer email list
- // Using pointers, not vector
- class CustomerList{
- public:
- CustomerList();
- CustomerList(string);
- void loadFile(string);
- void showNames();
- void showEmails();
- void showList();
- int countNameAppearances(string);
- int countEmailAppearances(string);
- int deleteCustomer(string);
- void addCustomer(string, string);
- private:
- // The pointer below is the head of the customerList. It replaces
- // the vector of Customers from the last assignment. In short, this is
- // pointer that points to the first Customer. The first Customer points to
- // the second Customer, and so on. The last Customer on the list points to NULL.
- Customer* head;
- // This is a private method that will delete all the elements on the Customer list.
- // This is only used by loadFile to make sure files are loaded twice
- void clearCustomerList();
- };
- CustomerList::CustomerList():head(NULL)
- {
- }
- CustomerList::CustomerList(string filename):head(NULL)
- {
- loadFile(filename);
- }
- void CustomerList::loadFile(string filename) {
- // This method loads all the customers in from a file.
- // Don't forget to delete the current list before loading a new one
- // For each line in the file, which represents a single customer
- // we need to:
- // - Create a new customer using the new command
- // - Input from the file into the new Customer using the >> operator
- // - Add the new customer to the list by setting its nextCustomer using setNextCustomer
- // and then change the head pointer to point at the new Customer
- // - Delete any unused Customer object
- clearCustomerList();
- ifstream inputFile;
- string inputname, inputemail;
- inputFile.open(filename.c_str());
- if (inputFile.is_open()){
- while (inputFile.good()){
- Customer customer_object;
- cin >> customer_object;
- Customer* temp = new Customer (customer_object.getEmail(), customer_object.getName());
- temp->setNextCustomer(head);
- head = temp;
- }
- inputFile.close();
- }
- else {
- cerr << "couldn't open the file." << endl;
- }
- }
- void CustomerList::showNames(){
- Customer * current = head;
- while (current != NULL){
- cout << current->getName() << endl;
- current = current->getNextCustomer();
- }
- }
- void CustomerList::showEmails(){
- Customer * current = head;
- while (current != NULL){
- cout << current->getEmail() << endl;
- current = current->getNextCustomer();
- }
- }
- void CustomerList::showList(){
- Customer * current = head;
- while (current != NULL){
- cout << current->getEmail() << " " << current->getName() << endl;
- current = current->getNextCustomer();
- }
- }
- int CustomerList::countNameAppearances(string name){
- Customer* current = head;
- int counter = 0;
- while (current != NULL){
- if ((current->getName()) == name){
- counter ++;
- }
- current = current->getNextCustomer();
- }
- return counter;
- }
- int CustomerList::countEmailAppearances(string email){
- Customer* current = head;
- int counter = 0;
- while (current != NULL){
- if ((current->getEmail()) == email){
- counter ++;
- }
- current = current->getNextCustomer();
- }
- return counter;
- }
- int CustomerList::deleteCustomer(string email){
- int counter = 0;
- Customer* shadow = head;
- Customer* current = head;
- while (current!=NULL){
- if (( current->getEmail()) == email) {
- if (current == head){
- shadow = head->getNextCustomer();
- delete (head);
- counter++;
- head = shadow;
- current = shadow;
- }
- else {
- Customer* temp = current;
- current = current->getNextCustomer();
- delete (temp);
- shadow->setNextCustomer(current);
- shadow = current;
- counter++;
- }
- }
- else {
- shadow = current;
- current = current->getNextCustomer();
- }
- }
- return counter;
- }
- void CustomerList::addCustomer(string email, string name){
- Customer* temp = new Customer (name, email);
- temp->setNextCustomer(head);
- head = temp;
- }
- void CustomerList::clearCustomerList(){
- Customer* shadow = head;
- while (head!=NULL){
- shadow = head->getNextCustomer();
- delete (head);
- head = shadow;
- }
- }
- ////////////////////////////////////////
- // Main function
- //
- //
- //start of comment out working main
- int main () {
- CustomerList theList;
- char choice;
- bool done = false;
- string input, input2;
- int count = 0;
- vector<string> temp;
- string filename;
- do {
- // Print the menu for the user
- print_menu();
- cin >> choice;
- switch(choice){
- case 'L':
- case 'l':
- cout << "What file would you like to load: ";
- cin >> filename;
- theList.loadFile(filename);
- break;
- case 'S':
- case 's':
- theList.showList();
- break;
- case 'M':
- case 'm':
- theList.showNames();
- break;
- case 'R':
- case 'r':
- theList.showEmails();
- break;
- case 'C':
- case 'c':
- cout << "What name do you want to use: ";
- cin.ignore();
- getline(cin,input);
- count = theList.countNameAppearances(input);
- cout << endl << "\"" << input << "\" appeared " << count << " times." << endl;
- break;
- case 'V':
- case 'v':
- cout << "What email address do you want to use: ";
- cin >> input;
- count = theList.countEmailAppearances(input);
- cout << endl << "\"" << input << "\" appeared " << count << " times." << endl;
- break;
- case 'A':
- case 'a':
- cout << "What email address do you want to add: ";
- cin >> input;
- cout << "What name goes with that: ";
- cin.ignore();
- getline(cin, input2);
- theList.addCustomer(input, input2);
- break;
- case 'D':
- case 'd':
- cout << "What email address do you want to delete: ";
- cin >> input;
- count = theList.deleteCustomer(input);
- cout << "Deleted " << count << " instances of " << input << endl;
- break;
- case 'X':
- case 'x':
- case 'Q':
- case 'q':
- done = true;
- cout << "Goodbye" << endl;
- break;
- default:
- cout << "Bad command option." << endl;
- } // end switch
- } while (!done);
- return 0;
- }
- //
- //end of comment out working main
- ////////////////////////////////////////
- // A test main for the Customer class
- /* Start of comment out Customer test main
- int main(){
- cout << "Testing default constructor: " << endl;
- Customer* cPtr= new Customer;
- if (cPtr->getNextCustomer() != NULL){
- cout << "ERR: nextCustomer is not null. You likely need to" << endl
- << "set it to null in Customer::Customer" << endl;
- }
- else {
- cout << "--> Pass" << endl;
- }
- delete (cPtr);
- string name = "Clay";
- string email = "clay@cs.georgetown.edu";
- cout << "Testing constructor with strings: " << endl;
- cPtr= new Customer(name, email);
- if (cPtr->getNextCustomer() != NULL){
- cout << "ERR: nextCustomer is not null. You likely need to" << endl
- << "set it to null in Customer::Customer(string, string)" << endl;
- }
- else {
- cout << "--> Pass" << endl;
- }
- cout << "Testing setNextCustomer: " << endl;
- Customer* c2Ptr = new Customer;
- cPtr->setNextCustomer(c2Ptr);
- if (cPtr->getNextCustomer() != c2Ptr){
- cout << "ERR: nextCustomer is " << cPtr << " but it should be "
- << c2Ptr << endl;
- }
- else {
- cout << "--> Pass" << endl;
- }
- }
- //end of comment out Customer test main
- */
- ////////////////////////////////////////
- // Test main for CustomerList class
- // /* // start to comment out CustomerList test main
- /*int main(){
- CustomerList list;
- // test add customers
- cout << "Adding customer 1:" << endl;
- list.addCustomer("clay@cs.georgetown.edu","Clay");
- cout << "Didn't crash" << endl;
- cout << "Adding customer 2:" << endl;
- list.addCustomer("quinn@cs.georgetown.edu","Quinn");
- cout << "Didn't crash" << endl;
- cout << "Adding customer 3:" << endl;
- list.addCustomer("alex@cs.georgetown.edu","Alex");
- cout << "Didn't crash" << endl;
- // test show list
- cout << "About to test showList." << endl;
- cout << "To pass, the output should contain these three items (order doesn't matter)"
- << endl << endl;
- cout << setw(10) << "Alex" << setw(25) << "alex@cs.georgetown.edu" << endl;
- cout << setw(10) << "Quinn" << setw(25) << "quinn@cs.georgetown.edu" << endl;
- cout << setw(10) << "Clay" << setw(25) << "clay@cs.georgetown.edu" << endl;
- cout << "----------------------------------------------------------------" << endl;
- list.showList();
- cout << endl << endl;
- // test show emails
- cout << "About to test showEmails." << endl;
- cout << "To pass, the output should contain these three items (order doesn't matter)"
- << endl << endl;
- cout << setw(25) << "alex@cs.georgetown.edu" << endl;
- cout << setw(25) << "quinn@cs.georgetown.edu" << endl;
- cout << setw(25) << "clay@cs.georgetown.edu" << endl;
- cout << "----------------------------------------------------------------" << endl;
- list.showEmails();
- cout << endl << endl;
- // test show names
- cout << "About to test showNames." << endl;
- cout << "To pass, the output should contain these three items (order doesn't matter)"
- << endl << endl;
- cout << setw(10) << "Alex" << endl;
- cout << setw(10) << "Quinn" << endl;
- cout << setw(10) << "Clay" << endl;
- cout << "----------------------------------------------------------------" << endl;
- list.showNames();
- cout << endl << endl;
- // test countNameAppearances
- bool pass = true;
- cout << "Testing countNameAppearances: " << endl;
- list.addCustomer("alex@cs.georgetown.edu","Alex");
- list.addCustomer("alex@cs.georgetown.edu","Alex");
- int count = list.countNameAppearances("Alex");
- if (count != 3) {
- cout << "ERR: countNameAppearances should have returned 3, but it returned: "
- << count << endl;
- pass = false;
- }
- if (pass) {
- count = list.countNameAppearances("Clay");
- if (count != 1) {
- cout << "ERR: countNameAppearances should have returned 1, but it returned: "
- << count << endl;
- pass = false;
- }
- }
- if (!pass){
- cout << "--> Fail" << endl;
- }
- else {
- cout << "--> Pass" << endl;
- }
- // test countEmailAppearances
- pass = true;
- cout << "Testing countEmailAppearances: " << endl;
- count = list.countEmailAppearances("alex@cs.georgetown.edu");
- if (count != 3) {
- cout << "ERR: countEmailAppearances should have returned 3, but it returned: "
- << count << endl;
- pass = false;
- }
- if (pass) {
- count = list.countEmailAppearances("clay@cs.georgetown.edu");
- if (count != 1) {
- cout << "ERR: countEmailAppearances should have returned 1, but it returned: "
- << count << endl;
- pass = false;
- }
- }
- if (!pass){
- cout << "--> Fail" << endl;
- }
- else {
- cout << "--> Pass" << endl;
- }
- // Testing delete customer
- cout << "Testing Delete Customer" << endl;
- count = list.deleteCustomer("alex@cs.georgetown.edu");
- if (count != 3){
- cout << "ERR: deleteCustomer should have returned 3, but it returned: "
- << count << endl;
- }
- count = list.deleteCustomer("clay@cs.georgetown.edu");
- if (count != 1){
- cout << "ERR: deleteCustomer should have returned 1, but it returned: "
- << count << endl;
- }
- cout << "If there was no crash, then it is a good sign" << endl;
- cout << "To pass, the output should contain only this item:"
- << endl << endl;
- cout << setw(10) << "Quinn" << setw(25) << "quinn@cs.georgetown.edu" << endl;
- cout << "----------------------------------------------------------------" << endl;
- list.showList();
- cout << endl << endl;
- cout << "NOTICE: This does not test the loadFile command." << endl
- << "That should be done by running the working main" << endl;
- }
- */ // end comment out CustomerList test main
- ////////////////////////////////////////
- // Print the menu so users know what options they have
- void print_menu(){
- cout << endl << endl;
- cout << "Menu options:" << endl;
- cout << setw(50) << "Load a file:" << setw(3) << "L" << endl;
- cout << setw(50) << "Show file contents:" << setw(3) << "S" << endl;
- cout << setw(50) << "Show all emails:" << setw(3) << "R" << endl;
- cout << setw(50) << "Show all names:" << setw(3) << "M" << endl;
- cout << setw(50) << "Count name appearances:"
- << setw(3) << "C" << endl;
- cout << setw(50) << "Count email appearances:"
- << setw(3) << "V" << endl;
- cout << setw(50) << "Add a customer:" << setw(3) << "A" << endl;
- cout << setw(50) << "Delete an customer:" << setw(3) << "D" << endl;
- cout << setw(50) << "Exit:" << setw(3) << "X" << endl << endl;
- cout << "Your choice: ";
- }
- ////////////////////////////////////////
- void print_vector(vector<string> vec){
- for (int i = 0; i < vec.size(); i++){
- cout << setw(40) << vec[i] << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement