Last active
July 24, 2016 18:22
-
-
Save ndedic/a3df4f9ef58c92e9235f77f291c784e7 to your computer and use it in GitHub Desktop.
A variation on http://martinfowler.com/articles/refactoring-video-store-js/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let AmountCalculator = { | |
'new'(days) { | |
return days * 3; | |
}, | |
'regular'(days) { | |
let amount = 2; | |
return days > 2 ? (amount + (days - 2) * 1.5) : amount; | |
}, | |
'childrens'(days) { | |
let amount = 1.5; | |
return days > 3 ? (amount + (days - 3) * 1.5) : amount; | |
} | |
}; | |
let BonusCalculator = { | |
'default': 1, | |
'new'(days) { | |
return days > 1 ? this.default + 1 : this.default; | |
}, | |
'regular'(days) { | |
return this.default; | |
}, | |
'childrens'(days) { | |
return this.default; | |
} | |
}; | |
function calculatePerMovie(days, movieId, movies) { | |
return { | |
'id': movieId, | |
'amount': AmountCalculator[movies[movieId].code](days), | |
'bonus': BonusCalculator[movies[movieId].code](days) | |
}; | |
} | |
function calculate(customer, movies) { | |
let name = customer.name; | |
let perMovie = []; | |
let total = { | |
amount: 0, | |
bonus: 0 | |
}; | |
for (let r of customer.rentals) { | |
let movieCalculation = calculatePerMovie(r.days, r.movieID, movies); | |
total.amount += movieCalculation.amount; | |
total.bonus += movieCalculation.bonus; | |
perMovie.push(movieCalculation); | |
} | |
return { name, perMovie, total }; | |
} | |
function createTextOutput(result, movies) { | |
let output = ''; | |
let amount = 0; | |
let bonus = 0; | |
output += `Rental Record for ${result.name}\n`; | |
for (let r of result.perMovie) { | |
output += `\t${movies[r.id].title}\t${r.amount}\n`; | |
amount += r.amount; | |
bonus += r.bonus; | |
} | |
output += `Amount owed is ${result.total.amount}\n`; | |
output += `You earned ${result.total.bonus} frequent renter points\n`; | |
return output; | |
} | |
function createHtmlOutput(result, movies) { | |
let output = `<h1>Rental Record for <em>${result.name}</em></h1>\n`; | |
output += '<table>\n'; | |
for (let r of result.perMovie) { | |
output += ` <tr><td>${movies[r.id].title}</td><td>${r.amount}</td></tr>\n`; | |
} | |
output += '</table>\n'; | |
output += `<p>Amount owed is <em>${result.total.amount}</em></p>\n`; | |
output += `<p>You earned <em>${result.total.bonus}</em> frequent renter points</p>\n`; | |
return output; | |
} | |
let customer = { | |
"name": "martin", | |
"rentals": [ | |
{ "movieID": "F001", "days": 3 }, | |
{ "movieID": "F002", "days": 1 } | |
] | |
}; | |
let movies = { | |
"F001": { "title": "Ran", "code": "regular" }, | |
"F002": { "title": "Trois Couleurs: Bleu", "code": "regular" } | |
}; | |
let result = calculate(customer, movies); | |
console.log(createTextOutput(result, movies)); | |
console.log(createHtmlOutput(result, movies)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment