Design Pattern Examples
Overview of object-oriented design patterns
Visitor_Shop.c File Reference

Implementation of the Visitor_Shop structure's supporting functions, Shop_Create(), Shop_Destroy(), Shop_PlaceOrder(), and Shop_PickupOrder(), as used in the Visitor Pattern. More...

#include <stdio.h>
#include <memory.h>
#include "helpers/dynamicstring.h"
#include "Visitor_Village.h"
Include dependency graph for Visitor_Shop.c:

Go to the source code of this file.

Functions

static bool _StringizeStringList (ConstStringList *list, DynamicString *output)
 Convert a string list to a comma-delimited string. Useful for displaying the list.
 
static bool Shop_DoesShopSellItem (Visitor_Shop *shop, const char *item)
 Determine if the given Visitor_Shop object sells the specified item.
 
static bool Shop_IsItemInStock (Visitor_Shop *shop, const char *item)
 Determine if given Visitor_Shop object has the specified item in stock.
 
static bool Shop_AddItemToInventory (Visitor_Shop *shop, const char *item)
 Add the specified item to the given Visitor_Shop object's inventory.
 
Visitor_ShopShop_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, address, and Village object.
 
void Shop_Destroy (Visitor_Shop *shop)
 Destroy an instance of the Visitor_Shop structure, freeing up any memory that may have been allocated to the shop. After this function returns, the pointer to the Visitor_Shop is no longer valid.
 
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 object.
 
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, this reduces the inventory for the given items that are sold by this shop.
 

Detailed Description

Implementation of the Visitor_Shop structure's supporting functions, Shop_Create(), Shop_Destroy(), Shop_PlaceOrder(), and Shop_PickupOrder(), as used in the Visitor Pattern.

Definition in file Visitor_Shop.c.

Function Documentation

◆ _StringizeStringList()

static bool _StringizeStringList ( ConstStringList list,
DynamicString output 
)
static

Convert a string list to a comma-delimited string. Useful for displaying the list.

Parameters
listThe ConstStringList object containing the strings to be stringized
outputA DynamicString object that returns the resulting string.
Returns
Returns true if the operation completed successfully; otherwise, returns false, indicating an out of memory condition (or a NULL argument).

Definition at line 25 of file Visitor_Shop.c.

References DynamicString_Append(), _ConstStringList::strings, and _ConstStringList::strings_count.

Referenced by Shop_PickupOrder(), and Shop_PlaceOrder().

◆ Shop_AddItemToInventory()

static bool Shop_AddItemToInventory ( Visitor_Shop shop,
const char *  item 
)
static

Add the specified item to the given Visitor_Shop object's inventory.

Parameters
shopThe Visitor_Shop to access.
itemThe item to add
Returns
Returns true if the item was added successfully; otherwise, returns false, indicating an out of memory condition (or a NULL argument).

Definition at line 107 of file Visitor_Shop.c.

References MapOfInt::entries, Visitor_Shop::Inventory, MapOfInt_Add(), MapOfInt_Find(), and MapOfIntEntry::value.

Referenced by Shop_PlaceOrder().

◆ Shop_Create()

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, address, and Village object.

Parameters
nameThe name of the shop.
addressThe address of the shop.
villageThe Village in which this shop exists.
Returns
Returns a Visitor_Shop object if successful; otherwise, returns NULL, indicating an out of memory condition (or NULL arguments).

Definition at line 140 of file Visitor_Shop.c.

References Visitor_Shop::Address, Visitor_Shop::IngredientsForItems, Visitor_Shop::Inventory, MapOfInt_Initialize(), MapOfStrings_Initialize(), Visitor_Shop::Name, and Visitor_Shop::Village.

Referenced by Village_Load().

◆ Shop_Destroy()

void Shop_Destroy ( Visitor_Shop shop)

Destroy an instance of the Visitor_Shop structure, freeing up any memory that may have been allocated to the shop. After this function returns, the pointer to the Visitor_Shop is no longer valid.

Parameters
shopThe Visitor_Shop object to destroy.

Definition at line 163 of file Visitor_Shop.c.

References Visitor_Shop::IngredientsForItems, Visitor_Shop::Inventory, MapOfInt_Clear(), and MapOfStrings_Clear().

Referenced by Village_Clear(), and Village_Load().

◆ Shop_DoesShopSellItem()

static bool Shop_DoesShopSellItem ( Visitor_Shop shop,
const char *  item 
)
static

Determine if the given Visitor_Shop object sells the specified item.

Parameters
shopThe Visitor_Shop to access.
itemThe item to look for
Returns
Returns true if this shop sells the item; otherwise false.

Definition at line 61 of file Visitor_Shop.c.

References Visitor_Shop::IngredientsForItems, and MapOfStrings_Find().

Referenced by Shop_PickupOrder(), and Shop_PlaceOrder().

◆ Shop_IsItemInStock()

static bool Shop_IsItemInStock ( Visitor_Shop shop,
const char *  item 
)
static

Determine if given Visitor_Shop object has the specified item in stock.

Parameters
shopThe Visitor_Shop to access.
itemThe item to look for
Returns
Returns true if the item is in stock; otherwise false.

Definition at line 82 of file Visitor_Shop.c.

References MapOfInt::entries, Visitor_Shop::Inventory, MapOfInt_Find(), and MapOfIntEntry::value.

Referenced by Shop_PickupOrder(), and Shop_PlaceOrder().

◆ Shop_PickupOrder()

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, this reduces the inventory for the given items that are sold by this shop.

Parameters
shopThe Visitor_Shop object to visit. The shop might not carry the requested ingredients, in which case, the visit is ignored.
itemsItems that were ordered, some of which may be sold by this shop.
itemsToBePickedUpList to be filled in with the item names that were picked up.
Returns
Returns true if the order was picked up successfully; otherwise, returns false, indicating an out of memory condition (or a NULL argument).

Definition at line 331 of file Visitor_Shop.c.

References _StringizeStringList(), ConstStringList_AddString(), ConstStringList_Clear(), ConstStringList_Initialize(), DynamicString_Clear(), MapOfInt::entries, Visitor_Shop::Inventory, MapOfInt_Find(), Visitor_Shop::Name, Shop_DoesShopSellItem(), Shop_IsItemInStock(), DynamicString::string, _ConstStringList::strings, _ConstStringList::strings_count, and MapOfIntEntry::value.

Referenced by OrderVisitor_VisitShop().

◆ Shop_PlaceOrder()

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 object.

Parameters
shopThe Visitor_Shop object to visit. The shop might not carry the requested ingredients, in which case, the visit is ignored.
itemsToOrderA ConstStringList object containing a list of the ingredients to order.
Returns
Returns a value from the PlaceOrderReponse enumeration indicating whether the order was accepted, ignored, or there was an error fulfilling the order (typically an out of memory condition).

Definition at line 177 of file Visitor_Shop.c.

References _StringizeStringList(), ConstStringList_AddString(), ConstStringList_AddStrings(), ConstStringList_AreListsEqual(), ConstStringList_Clear(), DynamicString_Clear(), DynamicString_Initialize(), MapOfStrings::entries, Visitor_Shop::IngredientsForItems, OrderVisitor::ItemsReceived, OrderVisitor::ItemsToOrder, MapOfStrings_Find(), Visitor_Shop::Name, OrderVisitor_Clear(), PlaceOrderResponse_Error, PlaceOrderResponse_OrderAccepted, PlaceOrderResponse_OrderIgnored, Shop_AddItemToInventory(), Shop_DoesShopSellItem(), Shop_IsItemInStock(), DynamicString::string, _ConstStringList::strings, _ConstStringList::strings_count, MapOfStringsEntry::value, Visitor_Shop::Village, and Village_VisitShop().

Referenced by OrderVisitor_VisitShop().