Mr. Graciani shops
Description:
Mr. Graciani shops
Mr. Graciani owns all the shops in a town. Each new shop that opens must notify to Mr. Graciani the products it offers. Mr. Graciani keeps a record of all the shops and their products (regardless of whether they have stock or not).
When customers go shopping, they start at a random shop. In a shop, they can buy a product or ask where they can buy it. When they ask about a product, the shop contacts Mr. Graciani to recommend another shop.
Your task in this kata is to implement the three main actors of this scenario, so that customers can make all their purchases:
graciani(Shops)
is a process that keeps a list of shops. This process should attend to the following messages:{register, Shop, Items}
, whereShop
is the PID of a shop andItems
is a list of products offered by the store. It adds the shop to its record.{ask, Shop, Item}
, whereShop
is the PID of a shop andItem
is an atom representing a product. Graciani must respond toShop
with the PID of a shop that sells the productItem
:{self(), Item, AnotherShop}
. If no shops sells that product, it must respond{self(), Item, unknown}
.
shop(Graciani, Stock)
is a process that keeps the PID of Mr. Graciani and the stock of products. This process should attend to the following messages:{buy, Customer, Item}
, whereCustomer
is the PID of a customer andItem
is an atom representing a product.- If the store has stock of that product, one must be removed and the shop must respond to
Customer
with{self(), Item, ok}
. - If there is no stock of that product, the shop must respond to
Customer
with{self(), Item, out_of_stock}
. - If the store doesn't sell that product, the shop must respond to
Customer
with{self(), Item, unknown}
.
- If the store has stock of that product, one must be removed and the shop must respond to
{ask, Customer, Item}
, whereCustomer
is the PID of a customer andItem
is an atom representing a product. The shop must respond toCustomer
with the PID of a shop that sells the productItem
:{self(), Item, AnotherShop}
. If no shops sells that product, it must respond{self(), Item, unknown}
.
customer(Parent, Shop, Items)
is a process that keeps a parent process, a shop and a list of items. The customer must try to buy all the products.- Every time the customer purchases a product, it must notify the parent process
{self(), Item, Shop}
, whereShop
is the PID of the shop where the productItem
was purchased. - If a product
Item
is not sold by any shop, the process ends by notifying the parent process{self(), Item, unknown}
. - When all products have been purchased, the parent process must be notified with the message
{self(), ok}
.
- Every time the customer purchases a product, it must notify the parent process
Note: Mr. Graciani guarantees that there will always be enough products for all customers, although not necessarily in the shop that he recommends. If more than one shop offers the same product, Mr. Graciani should recommend one of them at random, so that if one does not have stock, at some point it will recommend the other.
Since you can store the shops and stocks in the processes with the format you prefer, you must implement the following functions to initialize processes:
new_graciani()
returns the PID of a new Mr. Graciani (whose record of shops is empty).new_shop(Graciani, Stock)
returns the PID of a new shop.Graciani
is the PID of Mr. Graciani.Stock
is a list of pairs{Item, Count}
. This function should take care of talking to Mr. Graciani in order to register the new shop.new_customer(Parent, Shop, Items)
returns the PID of a new customer.Parent
is the PID of the parent process.Shop
is the PID of a registered shop.Items
is a list of products.
Similar Kata:
Stats:
Created | Aug 9, 2021 |
Published | Aug 9, 2021 |
Warriors Trained | 22 |
Total Skips | 0 |
Total Code Submissions | 5 |
Total Times Completed | 3 |
Erlang Completions | 3 |
Total Stars | 6 |
% of votes with a positive feedback rating | 50% of 1 |
Total "Very Satisfied" Votes | 0 |
Total "Somewhat Satisfied" Votes | 1 |
Total "Not Satisfied" Votes | 0 |