void processPurchase() { String customer_name; String object; int quantity; int customer_present = 0; readString(&customer_name); readString(&object); readNum(&quantity); for (int k = 0; k < num_customers; k += 1) { //check every existing customer if (StringIsEqualTo(&(customers[k].name), &customer_name)) { customer_present = 1; processCustomerRequest(&customer_name, &object, quantity, k); } } //non existing customers here if (!customer_present && num_customers < MAX_CUSTOMERS) { if(!processCustomerRequest(&customer_name, &object, quantity, num_customers) && quantity > 0){ //enough inventory to purchase, add customer to database customers[num_customers].name = StringDup(&customer_name); num_customers += 1; } } //destroy strings used for error messages StringDestroy(&customer_name); StringDestroy(&object); }
int processCustomerRequest(String* name, String* object, int quantity, int num) { String Bottles = StringCreate("Bottles"); String Diapers = StringCreate("Diapers"); String Rattles = StringCreate("Rattles"); int error = 0; if (StringIsEqualTo(object, &Bottles)) { if (quantity <= store_inv.bottles) { store_inv.bottles -= quantity; customers[num].bottles += quantity; error = 0; } else { //error message printf("Sorry "); StringPrint(name); printf(", we only have %d Bottles\n", store_inv.bottles); error = 1; } } else if (StringIsEqualTo(object, &Diapers)) { if (quantity <= store_inv.diapers) { store_inv.diapers -= quantity; customers[num].diapers += quantity; error = 0; } else { //error message printf("Sorry "); StringPrint(name); printf(", we only have %d Diapers\n", store_inv.diapers); error = 1; } } else if (StringIsEqualTo(object, &Rattles)) { if (quantity <= store_inv.rattles) { store_inv.rattles -= quantity; customers[num].rattles += quantity; error = 0; } else { //error message printf("Sorry "); StringPrint(name); printf(", we only have %d Rattles\n", store_inv.rattles); error = 1; } } StringDestroy(&Bottles); StringDestroy(&Diapers); StringDestroy(&Rattles); if (error == 0) { return 0; } else { return 1; } }
int main(void) { String s = StringCreate("Craig"); String t = StringDup(s); t = s; printf("Hello "); StringPrint(s); printf(" I see you're %d characters long\n", StringSize(s)); StringDestroy(s); StringDestroy(t); }
int main(void) { String s = StringCreate("Craig"); String t = s; // YOU STILL ONLY HAVE ONE STRING. Remember that t is a shallow reference, only a pointer to the same string. // printf() won't work since our string doesn't have a null terminator printf("Hello "); StringPrint(s); printf(" I see you're %d characters long\n", s.length); // using s.ptr and s.length is BAD, don't do this for the project! StringDestroy(s); }
/* clear the inventory and reset the customer database to empty */ void reset(void) { /* your code here */ for (int k = 0; k < num_customers; k += 1) { StringDestroy(&(customers[k].name)); customers[k].bottles = 0; customers[k].diapers = 0; customers[k].rattles = 0; } store_inv.bottles = 0; store_inv.diapers = 0; store_inv.rattles = 0; num_customers = 0; }
void processInventory() { String object; int quantity; String Bottles = StringCreate("Bottles"); String Diapers = StringCreate("Diapers"); String Rattles = StringCreate("Rattles"); readString(&object); readNum(&quantity); if (StringIsEqualTo(&object, &Bottles)) { store_inv.bottles += quantity; } else if (StringIsEqualTo(&object, &Diapers)) { store_inv.diapers += quantity; } else if (StringIsEqualTo(&object, &Rattles)) { store_inv.rattles += quantity; } StringDestroy(&object); StringDestroy(&Bottles); StringDestroy(&Diapers); StringDestroy(&Rattles); }