/* * Name: Joseph Ryan * EID: jbr2558 * Project 7, CRM part 2 * EE 312, Spring 2017 */ #include #include #include "String.h" #include "Customer.h" #include "CustomerDB.h" using namespace std; void readString(String &); void readNum(int &); CustomerDB database; int num_bottles = 0; int num_rattles = 0; int num_diapers = 0; /* clear the inventory and reset the customer database to empty */ void reset(void) { database.clear(); num_bottles = 0; num_rattles = 0; num_diapers = 0; } /* * selectInventItem is a convenience function that allows you * to obtain a pointer to the inventory record using the item type name * word must be "Bottles", "Diapers" or "Rattles" * for example the expression *selectInventItem("Bottles") returns the * current global variable for the number of bottls in the inventory */ int* selectInventItem(String word) { if (word == "Bottles") { return &num_bottles; } else if (word == "Diapers") { return &num_diapers; } else if (word == "Rattles") { return &num_rattles; } /* NOT REACHED */ return 0; } /* * this overload of selectInventItem is similar to selectInventItem above, however * this overload takes a Customer as a second argument and selects the data member * of that Customer that matches "word". The function returns a pointer to one of the three data * members from the specified customer. */ int* selectInventItem(String word, Customer &cust) { if (word == "Bottles") { return &cust.bottles; } else if (word == "Diapers") { return &cust.diapers; } else if (word == "Rattles") { return &cust.rattles; } /* NOT REACHED */ return 0; } /* * findMax searches through the CustomerDB "database" and returns the Customer * who has purchased the most items of the specified type. * type must be one of "Bottles", "Rattles" or "Diapers". * * Note: if two or more Customers are tied for having purchased the most of that item type * then findMax returns the first Customer in the CustomerDB who has purchased that maximal * quantity. * * Note: in the special case (invalid case) where there are zero Customers in the * CustomerDB, fundMax returns a null pointer (0) */ Customer* findMax(String type) { Customer* result = 0; int max = 0; for (int k = 0; k < database.size(); k += 1) { Customer &cust = database[k]; int* p = selectInventItem(type, cust); if (*p > max) { max = *p; result = &cust; } } return result; } bool itemExists(String item) { auto found = false; for(auto s : { "Bottles", "Diapers", "Rattles" }) { if(String(s) == item) { found = true; } } return found; } void processPurchase() { String name, type; int amt; readString(name); readString(type); readNum(amt); if(!itemExists(type) || amt <= 0) { return; } auto dbamt = selectInventItem(type); if(amt > *dbamt) { cout << "Sorry " << name.c_str() << ", we only have " << *dbamt << " " << type.c_str() << endl; return; } *dbamt -= amt; *selectInventItem(type, database[name]) += amt; } void processSummarize() { cout << "There are " << num_bottles << " Bottles, " << num_diapers << " Diapers and " << num_rattles << " Rattles in inventory" << endl << "we have had a total of " << database.size() << " different customers" << endl; for(auto s : { "Bottles", "Diapers", "Rattles" }) { auto type = String(s); auto temp = findMax(type); if(temp) { cout << temp->name.c_str() << " has purchased the most " << s << " (" << *selectInventItem(type, *temp) << ")" << endl; continue; } cout << "no one has purchased any " << s << endl; } } void processInventory() { String type; int amt; readString(type); readNum(amt); if(amt > 0 && itemExists(type)) { *selectInventItem(type) += amt; } }