Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 80 additions & 30 deletions dirty-control-structures.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,91 @@ function main() {
}

function processTransactions(transactions) {
if (transactions && transactions.length > 0) {
if (isEmpty(transactions)) {
showErrorMessage('No transaction 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);
}
} else {
console.log('Invalid transaction type!', transaction);
}
processTransaction(transaction);
}

}



function processTransaction(transaction){
if (!isOpen(transaction)) {
showErrorMessage('No transaction provided');
return;
}
if (isPayment(transaction)) {
processPayment(transaction)
}
else if (isRefund(transaction)) {
processRefund(transaction)
} else {
console.log('No transactions provided!');
showErrorMessage('Invalid transaction type!', transaction);
}
}

function usesCreditCard(transaction){
transaction.method === 'CREDIT_CARD'
}

function usesPaypal(transaction){
transaction.method === 'PAYPAL'
}

function usesPlan(transaction){
transaction.method === 'PLAN'
}

function isRefund(){
return transaction.type === 'REFUND';
}

function isPayment(){
return transaction.type === 'PAYMENT';
}

function isOpen(transaction){
return transaction.status == 'OPEN';
}

function processPayment(transaction){
if (transaction.method === 'CREDIT_CARD') {
processCreditCardPayment(transaction);
} else if (transaction.method === 'PAYPAL') {
processPayPalPayment(transaction);
} else if (transaction.method === 'PLAN') {
processPlanPayment(transaction);
}
}

function processRefund(transaction){
if (transaction.method === 'CREDIT_CARD') {
processCreditCardRefund(transaction);
} else if (transaction.method === 'PAYPAL') {
processPayPalRefund(transaction);
} else if (transaction.method === 'PLAN') {
processPlanRefund(transaction);
}
}


function showErrorMessage(message, item){
console.log('Invalid transaction type!', transaction);
if(item){
console.log(item);
}
}

function isEmpty(transaction){
return !transactions || transactions.length == 0
}



function processCreditCardPayment(transaction) {
console.log(
'Processing credit card payment for amount: ' + transaction.amount
Expand All @@ -97,4 +147,4 @@ function processPlanPayment(transaction) {

function processPlanRefund(transaction) {
console.log('Processing plan refund for amount: ' + transaction.amount);
}
}