From bb701370824715bf3e236799541534ff4232cc7f Mon Sep 17 00:00:00 2001 From: Juan Angel Lopez Diaz <102910494+Chicook@users.noreply.github.com> Date: Wed, 31 Jan 2024 21:06:17 +0100 Subject: [PATCH] Create SUGERENCIA Sugerencia --- code/Behavioral Design Patterns/SUGERENCIA | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 code/Behavioral Design Patterns/SUGERENCIA diff --git a/code/Behavioral Design Patterns/SUGERENCIA b/code/Behavioral Design Patterns/SUGERENCIA new file mode 100644 index 0000000..3641d2a --- /dev/null +++ b/code/Behavioral Design Patterns/SUGERENCIA @@ -0,0 +1,43 @@ +// Define individual approver functions +const manager = (amount) => { + if (amount <= 1000) { + console.log(`Manager approves the purchase of $${amount}.`); + return true; + } + return false; +}; + +const director = (amount) => { + if (amount <= 5000) { + console.log(`Director approves the purchase of $${amount}.`); + return true; + } + return false; +}; + +const vicePresident = (amount) => { + if (amount <= 10000) { + console.log(`Vice President approves the purchase of $${amount}.`); + return true; + } + return false; +}; + +// Set up the chain of responsibility +const chainOfResponsibility = [manager, director, vicePresident]; + +// Client +const processRequest = (amount) => { + for (const approver of chainOfResponsibility) { + if (approver(amount)) { + return; + } + } + console.log("None of the approvers can handle the request."); +}; + +// Test the chain with different purchase amounts +processRequest(800); // Manager approves the purchase of $800 +processRequest(4500); // Director approves the purchase of $4500 +processRequest(10000); // Vice President approves the purchase of $10000 +processRequest(15000); // None of the approvers can handle the request.