Challenge: Facade Pattern
In this challenge, you have to implement the facade pattern to solve the given problem.
We'll cover the following
Problem statement
In this challenge, you have to implement a part of the online hair product ordering system. The available products are shampoo
, conditioner
, and hair serum
.
A customer who is shopping online can buy any of these. A product
object has the following properties:
-
productName
: name of the product, that is,shampoo
,conditioner
, orhair serum
. -
amount
: the number of bottles that the customer wants to buy.
The workings of the system should be as follows. A customer buys an amount
of product. If that amount
is available in the inventory, a BuyProduct
class instance should be initiated. If the amount
is not available, a PreOrderProduct
class instance should be initiated. In the inventory:
- the amount of
shampoo
is:20
- the amount of
conditioner
is:20
- the amount of
hair serum
is:1000
In the end, the customers should be displayed if they can buy that amount
of bottles or will they have to pre-order. Here’s an example:
`2 bottles of shampoo are available. Click on "buy" to purchase them.`
`2000 bottles of hair serum are not available. You can Pre-order them on the next page.`
You need to implement the facade pattern to achieve this. You’ve already been given the Inventory
, BuyingProduct
, BuyProduct
, and PreOrder
classes. Write their definitions and link them such that the output mentioned above is shown to the customer.
Input
buyProduct
method invoked with some given arguments
Output
The message displayed regarding whether the customer can buy the product or will have to pre-order it
Sample input
var customer = new BuyingProduct()
customer.buyProduct({productName: "shampoo", amount : 2})
customer.buyProduct({productName: "hair serum", amount : 2000})
Sample output
`2 bottles of shampoo are available. Click on "buy" to purchase them.`
`2000 bottles of hair serum are not available. You can Pre-order them on the next page.`
Challenge #
Take a close look and design a step-by-step solution before jumping on to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided. Good Luck!
class Inventory{//initialize the amounts of shampoo, conditioner, and hair serums//check availiablity of products//hint: define a function that checks availaibility}class BuyingProduct extends Inventory {buyProduct(product) {console.log("Define buyProduct") //remove this line before testing}}class BuyProduct{//define it such that it returns a message}class PreOrderProduct{//define it such that it returns a message}
Let’s discuss the solution in the next lesson.