// CRM Project4.cpp // EE 312 Project 4 submission by // Joseph Ryan // JBR2558 // Slip days used: 0 // Spring 2018 #include #include #include "MyString.h" #include "Invent.h" #define MAX_CUSTOMERS 1000 Customer customers[MAX_CUSTOMERS]; int num_customers = 0; /* you'll probably need several more global variables */ /* and of course, you have a few functions to write */ int bottles, diapers, rattles; Customer* mostBottles() { int most = 0; Customer* who = NULL; for(int i = 0; i < num_customers; ++i) { if(customers[i].bottles > most) { who = &customers[i]; most = who->bottles; } } return who; } Customer* mostDiapers() { int most = 0; Customer* who = NULL; for(int i = 0; i < num_customers; ++i) { if(customers[i].diapers > most) { who = &customers[i]; most = who->diapers; } } return who; } Customer* mostRattles() { int most = 0; Customer* who = NULL; for(int i = 0; i < num_customers; ++i) { if(customers[i].rattles > most) { who = &customers[i]; most = who->rattles; } } return who; } /* clear the inventory and reset the customer database to empty */ void reset(void) { for(int i = 0; i < num_customers; ++i) { StringDestroy(&customers[i].name); } bottles = rattles = diapers = num_customers = 0; } void processSummarize() { printf("There are %d Bottles, %d Diapers and %d Rattles in inventory\n", bottles, diapers, rattles); printf("we have had a total of %d different customers\n", num_customers); if(num_customers) { Customer* most = mostBottles(); if(most) { StringPrint(&most->name); printf(" has purchased the most Bottles (%d)\n", most->bottles); } else { printf("no one has purchased any Bottles\n"); } most = mostDiapers(); if(most) { StringPrint(&most->name); printf(" has purchased the most Diapers (%d)\n", most->diapers); } else { printf("no one has purchased any Diapers\n"); } most = mostRattles(); if(most) { StringPrint(&most->name); printf(" has purchased the most Rattles (%d)\n", most->rattles); } else { printf("no one has purchased any Rattles\n"); } } } Customer* getCustomer(String* name) { Customer* c = NULL; for(int i = 0; i < num_customers; ++i) { if(StringIsEqualTo(name, &customers[i].name)) { c = &customers[i]; break; } } if(!c) { c = &customers[num_customers++]; c->name = StringDup(name); c->bottles = c->diapers = c->rattles = 0; } return c; } void processPurchase() { String bstr = StringCreate("Bottles"); String dstr = StringCreate("Diapers"); String rstr = StringCreate("Rattles"); String name, type; Customer* c = NULL; readString(&name); readString(&type); int ammount; readNum(&ammount); if(ammount > 0) { if(StringIsEqualTo(&type, &bstr)) { if(ammount > bottles) { printf("Sorry "); StringPrint(&name); printf(", we only have %d Diapers\n", diapers); } else { c = getCustomer(&name); bottles -= ammount; c->bottles += ammount; } } else if(StringIsEqualTo(&type, &dstr)) { if(ammount > diapers) { printf("Sorry "); StringPrint(&name); printf(", we only have %d Diapers\n", diapers); } else { c = getCustomer(&name); diapers -= ammount; c->diapers += ammount; } } else if(StringIsEqualTo(&type, &rstr)) { if(ammount > rattles) { printf("Sorry "); StringPrint(&name); printf(", we only have %d Rattles\n", rattles); } else { c = getCustomer(&name); rattles -= ammount; c->rattles += ammount; } } } StringDestroy(&name); StringDestroy(&type); StringDestroy(&bstr); StringDestroy(&dstr); StringDestroy(&rstr); } void processInventory() { String bstr = StringCreate("Bottles"); String dstr = StringCreate("Diapers"); String rstr = StringCreate("Rattles"); String input; readString(&input); int ammount; readNum(&ammount); int* field; if(StringIsEqualTo(&input, &bstr)) { field = &bottles; } else if(StringIsEqualTo(&input, &dstr)) { field = &diapers; } else if(StringIsEqualTo(&input, &rstr)) { field = &rattles; } else { StringDestroy(&input); StringDestroy(&bstr); StringDestroy(&dstr); StringDestroy(&rstr); return; } if(ammount > 0) { *field += ammount; } StringDestroy(&input); StringDestroy(&bstr); StringDestroy(&dstr); StringDestroy(&rstr); }