29 if (list != NULL && output != NULL)
38 printf(
" Error! Out of memory condition appending ',' to list of strings being concatenated into a single string!\n");
45 printf(
" Error! Out of memory condition appending a string to list of strings being concatenated into a single string!\n");
63 bool doesSell =
false;
68 doesSell = foundIndex != -1;
86 if (shop != NULL && item != NULL)
109 bool success =
false;
111 if (shop != NULL && item != NULL)
114 if (foundIndex != -1)
124 printf(
" Error! Out of memory condition adding an item to a shop's inventory!\n");
144 if (name != NULL && address != NULL && village != NULL)
181 if (itemsToOrder != NULL && shop != NULL)
186 bool success =
false;
188 for (
size_t index = 0; index < itemsToOrder->
strings_count; index++)
190 const char* item = itemsToOrder->
strings[index];
198 printf(
" Error! Out of memory condition adding ingredient to out of stock item list!\n");
205 printf(
" Error! Out of memory condition adding ingredient to local list of items sold by a store!\n");
219 printf(
" %s: Received an order for %s.\n",
236 for (
size_t index = 0; index < outOfStockItems.
strings_count; index++)
238 const char* ingredient = outOfStockItems.
strings[index];
242 printf(
" %s: %s out of stock, ordering ingredients to make more...\n",
243 shop->
Name, ingredient);
248 printf(
" Error! Out of memory creating order for out of stock items!\n");
254 printf(
" Error! Failed to visit shops for out of stock items, probably an out of memory condition!\n");
279 printf(
" Error! Out of memory creating string list of needed ingredients!\n");
287 printf(
" Error! Out of memory creating string list of received items!\n");
291 printf(
" %s: Error! Ordered %s but only received %s.\n",
293 received_items_list.
string);
304 printf(
" %s: %s out of stock, making...\n",
305 shop->
Name, ingredient);
333 bool success =
false;
335 if (items != NULL && itemsToBePickedUp != NULL && shop != NULL)
337 for (
size_t index = 0; index < items->
strings_count; index++)
339 const char* item = items->
strings[index];
348 printf(
" Error! Out of memory adding item to list of items to be picked up!\n");
354 printf(
" Error! %s: Item %s is not in the inventory when it should be.\n",
366 for (
size_t index = 0; index < itemsToBePickedUp->
strings_count; index++)
368 const char* itemToBePickedUp = itemsToBePickedUp->
strings[index];
372 if (inventoryIndex != -1)
378 printf(
" Error! Out of memory adding item to list of items to be picked up from shop %s!\n",
390 printf(
" %s: Order picked up for %s.\n", shop->
Name, output.
string);
394 printf(
" Error! Out of memory creating list of strings for items received from shop %s!\n",
void OrderVisitor_Clear(OrderVisitor *visitor)
Clear the specified OrderVisitor object, freeing up any memory that it was using. The resulting objec...
bool Shop_PickupOrder(Visitor_Shop *shop, ConstStringList *items, ConstStringList *itemsToBePickedUp)
Pick up the items sold by this shop (assumes the items were ordered already). Basically,...
void Shop_Destroy(Visitor_Shop *shop)
Destroy an instance of the Visitor_Shop structure, freeing up any memory that may have been allocated...
Visitor_Shop * Shop_Create(const char *name, const char *address, struct Village *village)
Creates a new instance of a Visitor_Shop structure, and initializes it with the given name,...
static bool Shop_DoesShopSellItem(Visitor_Shop *shop, const char *item)
Determine if the given Visitor_Shop object sells the specified item.
static bool Shop_AddItemToInventory(Visitor_Shop *shop, const char *item)
Add the specified item to the given Visitor_Shop object's inventory.
static bool _StringizeStringList(ConstStringList *list, DynamicString *output)
Convert a string list to a comma-delimited string. Useful for displaying the list.
PlaceOrderReponse Shop_PlaceOrder(Visitor_Shop *shop, ConstStringList *itemsToOrder)
Visit the specified Visitor_Shop object to try to place an order as described in the OrderVisitor obj...
static bool Shop_IsItemInStock(Visitor_Shop *shop, const char *item)
Determine if given Visitor_Shop object has the specified item in stock.
bool Village_VisitShop(Village *village, OrderVisitor *visitor)
Visit all shops in the given Village object to find the ingredients specified in the OrderVisitor obj...
PlaceOrderReponse
Represents the possible responses from the Shop_PlaceOrder() function.
@ PlaceOrderResponse_OrderIgnored
Order was ignored.
@ PlaceOrderResponse_Error
There was an error placing the order, likely an out of memory condition.
@ PlaceOrderResponse_OrderAccepted
Order was accepted.
void ConstStringList_Initialize(ConstStringList *stringList)
Initialize the given string list.
bool ConstStringList_AddStrings(ConstStringList *stringList, const char **strings, size_t numStrings)
Add an array of strings to the given string list.
bool ConstStringList_AreListsEqual(ConstStringList *left, ConstStringList *right)
Compare two strings lists to determine if they have the same contents.
bool ConstStringList_AddString(ConstStringList *stringList, const char *string)
Add a string to the given string list.
void ConstStringList_Clear(ConstStringList *stringList)
Clear the specified string list. The strings in the list are left alone, but the list itself is delet...
Declaration of the Visitor_Village class used in the Visitor Pattern.
void DynamicString_Clear(DynamicString *string)
Clear a DynamicString object, releasing any allocated memory. Resets to an empty string.
void DynamicString_Initialize(DynamicString *string)
Initialize a DynamicString object to an empty string.
bool DynamicString_Append(DynamicString *string, const char *s)
Append the specified string to the DynamicString object.
Declaration of the DynamicString structure and supporting functions to work with dynamic strings.
void MapOfInt_Clear(MapOfInt *map)
Clear the given MapOfInt object, releasing all memory associated with it. Leaves the object in an ini...
void MapOfInt_Initialize(MapOfInt *map)
Initialize the given MapOfInt structure so it is ready for use.
bool MapOfInt_Add(MapOfInt *map, const char *key, int value)
Add a key/value association to the given MapOfInt object. The MapOfInt object takes ownership of the ...
int MapOfInt_Find(MapOfInt *map, const char *key)
Find the specified key in the given MapOfInt object, returning an index into the object.
void MapOfStrings_Initialize(MapOfStrings *map)
Initialize the given MapOfStrings structure so it is ready for use.
void MapOfStrings_Clear(MapOfStrings *map)
Clear the given MapOfStrings object, releasing all memory associated with it. Leaves the object in an...
int MapOfStrings_Find(MapOfStrings *map, const char *key)
Find the specified key in the given MapOfStrings object, returning an index into the object.
Represents a list of pointers to zero-terminated strings that are to remain constant and never delete...
size_t strings_count
Number of strings in the strings list.
const char ** strings
Pointer to an array of zero-terminated string pointers. These strings are constant and will not be du...
Represents a string that can be grown dynamically.
char * string
The string that can grow.
int value
The integer value.
ConstStringList * value
The "value" that is a ConstStringList object.
MapOfStringsEntry * entries
List of MapOfStringsEntry for each mapping.
Represents a visitor used for ordering items from various shops. The user starts with an instance of ...
ConstStringList ItemsToOrder
Items to be ordered from any shop that sells the item.
ConstStringList ItemsReceived
List of items received from an order/pickup process.
Represents a collection of shops that can be visited.
Represents a shop in the village that can be visited.
const char * Name
Name of shop. Borrowed pointer.
const char * Address
Address of shop. Borrowed pointer.
MapOfInt Inventory
Maps ingredient to number of that ingredient in the shop.
struct Village * Village
Village this shop is in. Borrowed pointer.
MapOfStrings IngredientsForItems
Maps ingredient to list of items need for ingredient.