+/EE312
software design
git clone https://git.julia.blue/+/EE312
6241299
master
1/* 2* Name: Joseph Ryan 3* EID: jbr2558 4* Project 7, CRM part 2 5* EE 312, Spring 2017 6*/ 7 8#include <stdio.h> 9#include <iostream> 10#include "String.h" 11#include "Customer.h" 12#include "CustomerDB.h" 13 14using namespace std ; 15 16void readString (String & ); 17void readNum (int & ); 18 19CustomerDB database ; 20 21int num_bottles = 0 ; 22int num_rattles = 0 ; 23int num_diapers = 0 ; 24 25/* clear the inventory and reset the customer database to empty */ 26void reset (void ) { 27database .clear (); 28num_bottles = 0 ; 29num_rattles = 0 ; 30num_diapers = 0 ; 31} 32 33/* 34* selectInventItem is a convenience function that allows you 35* to obtain a pointer to the inventory record using the item type name 36* word must be "Bottles", "Diapers" or "Rattles" 37* for example the expression *selectInventItem("Bottles") returns the 38* current global variable for the number of bottls in the inventory 39*/ 40int * selectInventItem (String word ) { 41if (word == "Bottles" ) { 42return & num_bottles ; 43 }else if (word == "Diapers" ) { 44return & num_diapers ; 45 }else if (word == "Rattles" ) { 46return & num_rattles ; 47 } 48 49/* NOT REACHED */ 50return 0 ; 51} 52 53/* 54* this overload of selectInventItem is similar to selectInventItem above, however 55* this overload takes a Customer as a second argument and selects the data member 56* of that Customer that matches "word". The function returns a pointer to one of the three data 57* members from the specified customer. 58*/ 59int * selectInventItem (String word ,Customer & cust ) { 60if (word == "Bottles" ) { 61return & cust .bottles ; 62 }else if (word == "Diapers" ) { 63return & cust .diapers ; 64 }else if (word == "Rattles" ) { 65return & cust .rattles ; 66 } 67 68/* NOT REACHED */ 69return 0 ; 70} 71 72/* 73* findMax searches through the CustomerDB "database" and returns the Customer 74* who has purchased the most items of the specified type. 75* type must be one of "Bottles", "Rattles" or "Diapers". 76* 77* Note: if two or more Customers are tied for having purchased the most of that item type 78* then findMax returns the first Customer in the CustomerDB who has purchased that maximal 79* quantity. 80* 81* Note: in the special case (invalid case) where there are zero Customers in the 82* CustomerDB, fundMax returns a null pointer (0) 83*/ 84Customer * findMax (String type ) { 85Customer * result = 0 ; 86int max = 0 ; 87for (int k = 0 ;k < database .size ();k += 1 ) { 88Customer & cust = database [k ]; 89int * p = selectInventItem (type ,cust ); 90if (* p > max ) { 91max = * p ; 92result = & cust ; 93 } 94 } 95return result ; 96} 97 98bool itemExists (String item ) { 99auto found = false; 100for (auto s : { 101"Bottles" ,"Diapers" ,"Rattles" 102 }) { 103if (String (s )== item ) { 104found = true; 105 } 106 } 107return found ; 108} 109 110void processPurchase () { 111String name ,type ; 112int amt ; 113readString (name ); 114readString (type ); 115readNum (amt ); 116if (!itemExists (type )|| amt <=0 ) { 117return ; 118 } 119auto dbamt = selectInventItem (type ); 120if (amt > * dbamt ) { 121cout <<"Sorry " <<name .c_str () <<", we only have " <<* dbamt <<" " << 122type .c_str () <<endl ; 123return ; 124 } 125* dbamt -= amt ; 126* selectInventItem (type ,database [name ])+= amt ; 127} 128 129void processSummarize () { 130cout <<"There are " <<num_bottles <<" Bottles, " <<num_diapers << 131" Diapers and " <<num_rattles <<" Rattles in inventory" <<endl << 132"we have had a total of " <<database .size () <<" different customers" 133 <<endl ; 134for (auto s : { 135"Bottles" ,"Diapers" ,"Rattles" 136 }) { 137auto type = String (s ); 138auto temp = findMax (type ); 139if (temp ) { 140cout <<temp -> name .c_str () <<" has purchased the most " <<s << 141" (" <<* selectInventItem (type ,* temp ) <<")" <<endl ; 142continue ; 143 } 144cout <<"no one has purchased any " <<s <<endl ; 145 } 146} 147 148void processInventory () { 149String type ; 150int amt ; 151readString (type ); 152readNum (amt ); 153if (amt > 0 && itemExists (type )) { 154* selectInventItem (type )+= amt ; 155 } 156}