+/EE312
software design
git clone https://git.julia.blue/+/EE312
0c27270
master
1// CRM Project4.cpp 2// EE 312 Project 4 submission by 3// Joseph Ryan 4// JBR2558 5// Slip days used: 0 6// Spring 2018 7 8#include <stdio.h> 9#include <assert.h> 10#include "MyString.h" 11#include "Invent.h" 12 13#define MAX_CUSTOMERS 1000 14Customer customers [MAX_CUSTOMERS ]; 15int num_customers = 0 ; 16 17/* you'll probably need several more global variables */ 18/* and of course, you have a few functions to write */ 19int bottles ,diapers ,rattles ; 20 21Customer * mostBottles () { 22int most = 0 ; 23Customer * who = NULL ; 24for (int i = 0 ;i < num_customers ;++ i ) { 25if (customers [i ].bottles > most ) { 26who = & customers [i ]; 27most = who -> bottles ; 28 } 29 } 30return who ; 31} 32 33Customer * mostDiapers () { 34int most = 0 ; 35Customer * who = NULL ; 36for (int i = 0 ;i < num_customers ;++ i ) { 37if (customers [i ].diapers > most ) { 38who = & customers [i ]; 39most = who -> diapers ; 40 } 41 } 42return who ; 43} 44 45Customer * mostRattles () { 46int most = 0 ; 47Customer * who = NULL ; 48for (int i = 0 ;i < num_customers ;++ i ) { 49if (customers [i ].rattles > most ) { 50who = & customers [i ]; 51most = who -> rattles ; 52 } 53 } 54return who ; 55} 56 57/* clear the inventory and reset the customer database to empty */ 58void reset (void ) { 59for (int i = 0 ;i < num_customers ;++ i ) { 60StringDestroy (& customers [i ].name ); 61 } 62bottles = rattles = diapers = num_customers = 0 ; 63} 64 65void processSummarize () { 66printf ("There are %d Bottles, %d Diapers and %d Rattles in inventory\n" , 67bottles ,diapers ,rattles ); 68printf ("we have had a total of %d different customers\n" ,num_customers ); 69if (num_customers ) { 70Customer * most = mostBottles (); 71if (most ) { 72StringPrint (& most -> name ); 73printf (" has purchased the most Bottles (%d)\n" ,most -> bottles ); 74 }else { 75printf ("no one has purchased any Bottles\n" ); 76 } 77most = mostDiapers (); 78if (most ) { 79StringPrint (& most -> name ); 80printf (" has purchased the most Diapers (%d)\n" ,most -> diapers ); 81 }else { 82printf ("no one has purchased any Diapers\n" ); 83 } 84most = mostRattles (); 85if (most ) { 86StringPrint (& most -> name ); 87printf (" has purchased the most Rattles (%d)\n" ,most -> rattles ); 88 }else { 89printf ("no one has purchased any Rattles\n" ); 90 } 91 } 92} 93 94Customer * getCustomer (String * name ) { 95Customer * c = NULL ; 96for (int i = 0 ;i < num_customers ;++ i ) { 97if (StringIsEqualTo (name ,& customers [i ].name )) { 98c = & customers [i ]; 99break ; 100 } 101 } 102if (!c ) { 103c = & customers [num_customers ++ ]; 104c -> name = StringDup (name ); 105c -> bottles = c -> diapers = c -> rattles = 0 ; 106 } 107return c ; 108} 109 110void processPurchase () { 111String bstr = StringCreate ("Bottles" ); 112String dstr = StringCreate ("Diapers" ); 113String rstr = StringCreate ("Rattles" ); 114 115String name ,type ; 116Customer * c = NULL ; 117readString (& name ); 118readString (& type ); 119int ammount ; 120readNum (& ammount ); 121 122if (ammount > 0 ) { 123if (StringIsEqualTo (& type ,& bstr )) { 124if (ammount > bottles ) { 125printf ("Sorry " ); 126StringPrint (& name ); 127printf (", we only have %d Diapers\n" ,diapers ); 128 }else { 129c = getCustomer (& name ); 130bottles -= ammount ; 131c -> bottles += ammount ; 132 } 133 }else if (StringIsEqualTo (& type ,& dstr )) { 134if (ammount > diapers ) { 135printf ("Sorry " ); 136StringPrint (& name ); 137printf (", we only have %d Diapers\n" ,diapers ); 138 }else { 139c = getCustomer (& name ); 140diapers -= ammount ; 141c -> diapers += ammount ; 142 } 143 }else if (StringIsEqualTo (& type ,& rstr )) { 144if (ammount > rattles ) { 145printf ("Sorry " ); 146StringPrint (& name ); 147printf (", we only have %d Rattles\n" ,rattles ); 148 }else { 149c = getCustomer (& name ); 150rattles -= ammount ; 151c -> rattles += ammount ; 152 } 153 } 154 } 155 156StringDestroy (& name ); 157StringDestroy (& type ); 158StringDestroy (& bstr ); 159StringDestroy (& dstr ); 160StringDestroy (& rstr ); 161} 162 163void processInventory () { 164String bstr = StringCreate ("Bottles" ); 165String dstr = StringCreate ("Diapers" ); 166String rstr = StringCreate ("Rattles" ); 167 168String input ; 169readString (& input ); 170int ammount ; 171readNum (& ammount ); 172int * field ; 173if (StringIsEqualTo (& input ,& bstr )) { 174field = & bottles ; 175 }else if (StringIsEqualTo (& input ,& dstr )) { 176field = & diapers ; 177 }else if (StringIsEqualTo (& input ,& rstr )) { 178field = & rattles ; 179 }else { 180StringDestroy (& input ); 181StringDestroy (& bstr ); 182StringDestroy (& dstr ); 183StringDestroy (& rstr ); 184return ; 185 } 186 187if (ammount > 0 ) { 188* field += ammount ; 189 } 190 191StringDestroy (& input ); 192StringDestroy (& bstr ); 193StringDestroy (& dstr ); 194StringDestroy (& rstr ); 195}