8using System.Collections.Generic;
96 private Dictionary<string, int>
Inventory =
new Dictionary<string, int>();
101 public string Name =
string.Empty;
171 StringBuilder output =
new StringBuilder();
173 foreach(
string item
in items)
175 if (output.Length > 0)
181 return output.ToString();
193 bool matched =
false;
195 if (left.Length == right.Length)
198 foreach (
string item
in left)
200 if (!right.Contains(item))
222 bool orderPlaced =
false;
223 List<string> outOfStockItems =
new List<string>();
224 List<string> itemsInThisShop =
new List<string>();
226 foreach (
string item
in items)
232 outOfStockItems.Add(item);
234 itemsInThisShop.Add(item);
238 if (itemsInThisShop.Count > 0)
240 Console.WriteLine(
" {0}: Received an order for {1}.",
Name,
StringizeList(itemsInThisShop.ToArray()));
243 if (outOfStockItems.Count > 0)
245 foreach (
string itemToOrder
in outOfStockItems)
249 Console.WriteLine(
" {0}: {1} out of stock, ordering ingredients to make more...",
Name, itemToOrder);
264 Console.WriteLine(
" {0}: {1} out of stock, making...",
Name, itemToOrder);
281 public void PickupOrder(
string[] items, List<string> itemsToBePickedUp)
283 foreach (
string item
in items)
290 itemsToBePickedUp.Add(item);
294 Console.WriteLine(
" Error! {0}: Item {1} is not in the inventory when it should be.",
Name, item);
299 if (itemsToBePickedUp.Count > 0)
302 StringBuilder output =
new StringBuilder();
303 foreach (
string itemToBePickedUp
in itemsToBePickedUp)
307 if (output.Length > 0)
311 output.Append(itemToBePickedUp);
315 Console.WriteLine(
" {0}: Order picked up for {1}.",
Name, output.ToString());
A visitor used for ordering items from various shops. The user starts with an instance of this class ...
List< string > ItemsReceived
List of items received from an order/pickup process.
Represents a collection of shops that can be visited.
void Accept(Visitor visitor)
The visitor will call this method on each element it wants to visit.
override void Accept(Visitor visitor)
Handle visitors to this class.
Represent a butcher shop.
override void Accept(Visitor visitor)
Handle visitors to this class.
Represent a condiment grocer.
override void Accept(Visitor visitor)
Handle visitors to this class.
Represent a maker (of things).
override void Accept(Visitor visitor)
Handle visitors to this class.
Represent a pickle grocer.
override void Accept(Visitor visitor)
Handle visitors to this class.
Represent a restaurant shop.
override void Accept(Visitor visitor)
Handle visitors to this class.
Base class that all shops must implement.
bool AreListContentsTheSame(string[] left, string[] right)
Determine if the two string lists have the same contents.
abstract void Accept(Visitor visitor)
The visitor will call this method on each element it wants to visit.
void AddItemToInventory(string item)
Add the specified item to this shop's inventory.
Dictionary< string, int > Inventory
Inventory for this shop.
Dictionary< string, string[]> IngredientsForItems
Specifies the ingredients needed for each item sold by the shop. Also, the keys are what the shop sel...
bool PlaceOrder(string[] items)
Place an order for the specified items. If the inventory is empty, replenish the inventory by visitin...
string Name
Name of the shop.
Village Village
The Village that contains this shop.
string StringizeList(string[] items)
Convert a string list to a comma-delimited string. Useful for displaying the list.
bool DoesShopSellItem(string item)
Determine if this shop sells the specified itrem.
bool IsItemInStock(string item)
Determine if this shop has the specified item in stock.
string Address
Address of the shop (could be a structure but a simple string is sufficient for this example).
void PickupOrder(string[] items, List< string > itemsToBePickedUp)
Pick up the items sold by this shop (assumes the items were ordered already). Basically,...
Represent a vegetable grocer.
override void Accept(Visitor visitor)
Handle visitors to this class.
All visitors must implement this base class and then override one or more of the VisitXXX() methods,...
virtual void VisitPickleGrocer(Visitor_PickleGrocer shop)
Let the visitor visit a Visitor_PickleGrocer shop.
virtual void VisitMaker(Visitor_Maker shop)
Let the visitor visit a Visitor_Maker shop.
virtual void VisitBaker(Visitor_Baker shop)
Let the visitor visit a Visitor_Baker shop.
virtual void VisitButcher(Visitor_Butcher shop)
Let the visitor visit a Visitor_Butcher shop.
virtual void VisitCondimentGrocer(Visitor_CondimentGrocer shop)
Let the visitor visit a Visitor_CondimentGrocer shop.
virtual void VisitVegetableGrocer(Visitor_VegetableGrocer shop)
Let the visitor visit a Visitor_VegetableGrocer shop.
virtual void VisitRestaurant(Visitor_Restaurant shop)
Let the visitor visit a Visitor_Restaurant shop.
The interface that all element classes must implement if they are to participate in the visitor patte...
void Accept(Visitor visitor)
The visitor will call this method on each element it wants to visit.
The namespace containing all Design Pattern Examples implemented in C#.