Beta

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}, where Shop is the PID of a shop and Items is a list of products offered by the store. It adds the shop to its record.
    • {ask, Shop, Item}, where Shop is the PID of a shop and Item is an atom representing a product. Graciani must respond to Shop with the PID of a shop that sells the product Item: {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}, where Customer is the PID of a customer and Item 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}.
    • {ask, Customer, Item}, where Customer is the PID of a customer and Item is an atom representing a product. The shop must respond to Customer with the PID of a shop that sells the product Item: {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}, where Shop is the PID of the shop where the product Item 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}.

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.
Concurrency
Algorithms

More By Author:

Check out these other kata created by jariazavalverde

Stats:

CreatedAug 9, 2021
PublishedAug 9, 2021
Warriors Trained22
Total Skips0
Total Code Submissions5
Total Times Completed3
Erlang Completions3
Total Stars6
% of votes with a positive feedback rating50% of 1
Total "Very Satisfied" Votes0
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes0
Ad
Contributors
  • jariazavalverde Avatar
Ad