From 4f5dbfbdee4663c40aa3c38ed2f206b4c090476c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Schwarzm=C3=BCller?= Date: Tue, 17 Nov 2020 12:47:28 +0100 Subject: [PATCH 1/2] added code --- dirty-control-structures.js | 100 ++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 dirty-control-structures.js diff --git a/dirty-control-structures.js b/dirty-control-structures.js new file mode 100644 index 0000000..3545df5 --- /dev/null +++ b/dirty-control-structures.js @@ -0,0 +1,100 @@ +main(); + +function main() { + const transactions = [ + { + id: 't1', + type: 'PAYMENT', + status: 'OPEN', + method: 'CREDIT_CARD', + amount: '23.99', + }, + { + id: 't2', + type: 'PAYMENT', + status: 'OPEN', + method: 'PAYPAL', + amount: '100.43', + }, + { + id: 't3', + type: 'REFUND', + status: 'OPEN', + method: 'CREDIT_CARD', + amount: '10.99', + }, + { + id: 't4', + type: 'PAYMENT', + status: 'CLOSED', + method: 'PLAN', + amount: '15.99', + }, + ]; + + processTransactions(transactions); +} + +function processTransactions(transactions) { + if (transactions && transactions.length > 0) { + for (const transaction of transactions) { + if (transaction.type === 'PAYMENT') { + if (transaction.status === 'OPEN') { + if (transaction.method === 'CREDIT_CARD') { + processCreditCardPayment(transaction); + } else if (transaction.method === 'PAYPAL') { + processPayPalPayment(transaction); + } else if (transaction.method === 'PLAN') { + processPlanPayment(transaction); + } + } else { + console.log('Invalid transaction type!'); + } + } else if (transaction.type === 'REFUND') { + if (transaction.status === 'OPEN') { + if (transaction.method === 'CREDIT_CARD') { + processCreditCardRefund(transaction); + } else if (transaction.method === 'PAYPAL') { + processPayPalRefund(transaction); + } else if (transaction.method === 'PLAN') { + processPlanRefund(transaction); + } + } else { + console.log('Invalid transaction type!', transaction); + } + } else { + console.log('Invalid transaction type!', transaction); + } + } + } else { + console.log('No transactions provided!'); + } +} + +function processCreditCardPayment(transaction) { + console.log( + 'Processing credit card payment for amount: ' + transaction.amount + ); +} + +function processCreditCardRefund(transaction) { + console.log( + 'Processing credit card refund for amount: ' + transaction.amount + ); +} + +function processPayPalPayment(transaction) { + console.log('Processing PayPal payment for amount: ' + transaction.amount); +} + +function processPayPalRefund(transaction) { + console.log('Processing PayPal refund for amount: ' + transaction.amount); +} + +function processPlanPayment(transaction) { + console.log('Processing plan payment for amount: ' + transaction.amount); +} + +function processPlanRefund(transaction) { + console.log('Processing plan refund for amount: ' + transaction.amount); +} From 39d94fa0087d6f5d4ba32ddadf086525afad69a2 Mon Sep 17 00:00:00 2001 From: Benjamin Bandla Khonye <82065910+Bandla1995@users.noreply.github.com> Date: Sat, 5 Feb 2022 17:06:46 +0200 Subject: [PATCH 2/2] UPDATED THE STRUCTURE Changed the structure of the code by adding more function, and guards to reduce the loops in the code. --- dirty-control-structures.js | 98 ++++++++++++++++++++++++++----------- 1 file changed, 70 insertions(+), 28 deletions(-) diff --git a/dirty-control-structures.js b/dirty-control-structures.js index 3545df5..e1f06aa 100644 --- a/dirty-control-structures.js +++ b/dirty-control-structures.js @@ -36,41 +36,83 @@ function main() { } function processTransactions(transactions) { - if (transactions && transactions.length > 0) { + if (isEmpty(transactions)){ + showErrorMessage('No transacations provided'); + return; + } + for (const transaction of transactions) { - if (transaction.type === 'PAYMENT') { - if (transaction.status === 'OPEN') { - if (transaction.method === 'CREDIT_CARD') { - processCreditCardPayment(transaction); - } else if (transaction.method === 'PAYPAL') { - processPayPalPayment(transaction); - } else if (transaction.method === 'PLAN') { - processPlanPayment(transaction); - } - } else { - console.log('Invalid transaction type!'); - } - } else if (transaction.type === 'REFUND') { - if (transaction.status === 'OPEN') { - if (transaction.method === 'CREDIT_CARD') { - processCreditCardRefund(transaction); - } else if (transaction.method === 'PAYPAL') { - processPayPalRefund(transaction); - } else if (transaction.method === 'PLAN') { - processPlanRefund(transaction); - } - } else { - console.log('Invalid transaction type!', transaction); + processTransaction(transaction); + } + } +function isEmpty(transactions) { + return !transactions || transcations.length === 0; +} + +function showErrorMessage(message, item) { + console.log(message); + if (item) { + console.log(item); + } +} + +function processTransaction(transaction) { + if (!isOpen(transaction)) { + showErrorMessage('Invalid transaction type!'); + return; } + if(isPayment(transaction)) { + processPayment(transaction); + } else if (isRefund(transaction)) { + processRefund(transaction); } else { - console.log('Invalid transaction type!', transaction); + showErrorMessage('Invalid transaction type!', transaction); } - } - } else { - console.log('No transactions provided!'); + +} + + function isOpen(transaction) { + return transaction.status === 'OPEN'; } + +function usesCreditCard(transaction) { + return transaction.method === 'CREDIT_CARD' ; +} +function usesPayPal(transaction) { + return transaction.method === 'PAYPAL' ; +} +function usesPlan(transaction) { + return transaction.method === 'PLAN' ; +} +function isPayment(transaction) { + return transaction.type === 'PAYMENT'; +} + +function isRefund(transaction) { + return transaction.type ==='REFUND'; } +function processPayment(paymentTransaction){ + if (transaction.method === 'CREDIT_CARD') { + processCreditCardPayment(paymentTransaction); + } else if (transaction.method === 'PAYPAL') { + processPayPalPayment(paymentTransaction); + } else if (transaction.method === 'PLAN') { + processPlanPayment(paymentTransaction); + } + +} + + function processRefund(refundTransaction) { + if (transaction.method === 'CREDIT_CARD') { + processCreditCardRefund(refundTransaction); + } else if (refundTransaction.method === 'PAYPAL') { + processPayPalRefund(refundTransaction); + } else if (refundTransaction.method === 'PLAN') { + processPlanRefund(refundTransactionn); + } + } + function processCreditCardPayment(transaction) { console.log( 'Processing credit card payment for amount: ' + transaction.amount