Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open source commit #15

Open
wants to merge 1 commit into
base: control-01-guards
Choose a base branch
from
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);
}
}