//////////////////////////////////////// // Spam Cupcakes //////////////////////////////////////// // Standard included things #include #include #include #include using namespace std; //////////////////////////////////////// // Function prototypes void print_menu(); void print_vector(vector); //////////////////////////////////////// // Class definitions // A 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(); void setName(string); void setEmail(string); private: string name; string email; }; ostream &operator<<(ostream &output, Customer c){ output << c.email << c.name; return output; } istream &operator>>(istream &input, Customer & c){ input >> c.email; input.ignore(); getline(input, c.name); return input; } Customer::Customer(){ } Customer::Customer (string newName, string newEmail){ name = newName; email = newEmail; } string Customer::getName(){ return name; } string Customer::getEmail(){ return email; } void Customer::setName(string newName) { name = newName; } void Customer::setEmail(string newEmail){ email = newEmail; } // A class to hold the customer email list class CustomerList{ public: CustomerList(); CustomerList(string); void loadFile(string); void showNames(); void showEmails(); void showList(); int countNameAppearances(string); int countEmailAppearances(string); vector getUniqueNames(); vector getUniqueEmails(); void showEmailsWithNames(); private: vector customerList; vector uniqueEmailList; vector uniqueNameList; int countStringAppearances(string, vector); }; CustomerList::CustomerList(){ } CustomerList::CustomerList(string filename){ loadFile(filename); } void CustomerList::loadFile(string filename) { ifstream inputFile; Customer cust; // open file if it is there inputFile.open(filename.c_str()); if (!inputFile) { cerr << "Could not open file: "<< filename << " for reading." << endl; exit(1); } customerList.clear(); // Read the lines from the file while (inputFile >> cust){ customerList.push_back(cust); // Add names to the unique name list if (countStringAppearances(cust.getName(), uniqueNameList) == 0){ uniqueNameList.push_back(cust.getName()); } // Add names to the unique email list if (countStringAppearances(cust.getEmail(), uniqueEmailList) == 0){ uniqueEmailList.push_back(cust.getEmail()); } } } void CustomerList::showNames(){ cout << setw(40) << "Names" << endl; for (int i = 0 ; i < customerList.size(); i++){ cout << setw(40) << customerList[i].getName() << endl; } } void CustomerList::showEmails(){ cout << setw(40) << "Emails" << endl; for (int i = 0 ; i < customerList.size(); i++){ cout << setw(40) << customerList[i].getEmail() << endl; } } void CustomerList::showList(){ cout << setw(40) << "Names" << setw(40) << "Emails" << endl; for (int i = 0 ; i < customerList.size(); i++){ cout << setw(40) << customerList[i].getName() << setw(40) << customerList[i].getEmail() << endl; } } int CustomerList::countNameAppearances(string name){ int count = 0; for (int i = 0; i < customerList.size(); i++){ if (customerList[i].getName() == name){ count ++; } } return count; } int CustomerList::countEmailAppearances(string email){ int count = 0; for (int i = 0; i < customerList.size(); i++){ if (customerList[i].getEmail() == email){ count ++; } } return count; } int CustomerList::countStringAppearances(string target, vector search){ int count = 0; for (int i = 0; i < search.size(); i++){ if (target == search[i]){ count++; } } return count; } vector CustomerList::getUniqueNames(){ return uniqueNameList; } vector CustomerList::getUniqueEmails(){ return uniqueEmailList; } void CustomerList::showEmailsWithNames(){ for (int i = 0; i < uniqueEmailList.size(); i++){ cout << setw(10) << "Email: " << setw(30) << uniqueEmailList[i] << endl; for (int j = 0; j < customerList.size(); j++){ if (customerList[j].getEmail() == uniqueEmailList[i]){ cout << setw(40) << customerList[j].getName() << endl; } } cout << endl; } } //////////////////////////////////////// // Main function // int main () { CustomerList theList; char choice; bool done = false; string input; int count = 0; vector 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 'E': case 'e': cout << endl << setw(40) << "Unique Emails: " << endl; temp = theList.getUniqueEmails(); print_vector(temp); break; case 'N': case 'n': cout << endl << setw(40) << "Unique Names: " << endl; temp = theList.getUniqueNames(); print_vector(temp); break; case 'U': case 'u': theList.showEmailsWithNames(); 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; } //////////////////////////////////////// // 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) << "Show unique emails:" << setw(3) << "E" << endl; cout << setw(50) << "Show unique names:" << setw(3) << "N" << endl; cout << setw(50) << "Show names for unique emails:" << setw(3) << "U" << endl; cout << setw(50) << "Exit:" << setw(3) << "X" << endl << endl; cout << "Your choice: "; } //////////////////////////////////////// void print_vector(vector vec){ for (int i = 0; i < vec.size(); i++){ cout << setw(40) << vec[i] << endl; } }