diff --git a/rainbowjyp/chp6/CombineFunctionIntoClass.js b/rainbowjyp/chp6/CombineFunctionIntoClass.js new file mode 100644 index 0000000..289ce25 --- /dev/null +++ b/rainbowjyp/chp6/CombineFunctionIntoClass.js @@ -0,0 +1,37 @@ +class Reading { + constructor(data) { + this._customer = data.customer; + this._quantity = data.quantity; + this._month = data.month; + this._year = data.year; + } + + get customer() { + return this._customer; + } + get quantity() { + return this._quantity; + } + get month() { + return this._month; + } + get year() { + return this._year; + } + + get baseCharge() { + return baseRate(this.month, this.year) * this.quantity; + } + + get taxableCharge() { + return Math.max(0, this.baseCharge - taxThreshold(this.year)); + } +} + +const rawReading = acquireReading(); +const aReading = new Reading(rawReading); +const taxableCharge = aReading.taxableCharge; + +function acquireReading() { + return { customer: 'ivan', quantity: 10, month: 5, year: 2017 }; +} diff --git a/rainbowjyp/chp6/extractFunction.js b/rainbowjyp/chp6/extractFunction.js new file mode 100644 index 0000000..c7d3559 --- /dev/null +++ b/rainbowjyp/chp6/extractFunction.js @@ -0,0 +1,33 @@ +function printOwing(invoice) { + printBanner(); + const outstanding = calculateOutstanding(invoice); + + recordDueDate(invoice); + + printDetails(invoice, outstanding); +} + +function calculateOutstanding(invoice) { + let result = 0; + for (const o of invoice.orders) { + result += o.amount; + } + return result; +} + +function recordDueDate(invoice) { + const today = Clock.today; + invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30); +} + +function printBanner() { + console.log('***********************'); + console.log('*** Customer Owes ***'); + console.log('***********************'); +} + +function printDetails(invoice, outstanding) { + console.log(`name: ${invoice.customer}`); + console.log(`amount: ${outstanding}`); + console.log(`due: ${invoice.dueDate.toLocaleDateString()}`); +} diff --git a/rainbowjyp/chp6/splitPhase.js b/rainbowjyp/chp6/splitPhase.js new file mode 100644 index 0000000..6ce9916 --- /dev/null +++ b/rainbowjyp/chp6/splitPhase.js @@ -0,0 +1,20 @@ +function priceOrder(product, quantity, shippingMethod) { + const priceData = calculatePricingData(product, quantity); + return applyShipping(priceData, shippingMethod); +} + +const calculatePricingData = (product, quantity) => { + const basePrice = product.basePrice * quantity; + const discount = Math.max(quantity - product.discountThreshold, 0) * product.basePrice * product.discountRate; + return { basePrice: basePrice, quantity: quantity, discount: discount }; +}; + +function applyShipping(priceData, shippingMethod) { + const shippingPerCase = + priceData.basePrice > shippingMethod.discountThreshold + ? shippingMethod.discountedFee + : shippingMethod.feePerCase; + const shippingCost = priceData.quantity * shippingPerCase; + const price = priceData.basePrice - priceData.discount + shippingCost; + return price; +}