Created
June 6, 2018 19:35
-
-
Save jsbonso/4e5eed0ece9c01605395d12dd2bd50db to your computer and use it in GitHub Desktop.
Simple Checkout with Price Rules App using Vanilla JavaScript
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
<!DOCTYPE html> | |
<html> | |
<body> | |
<h1>Simple Checkout with Price Rules</h1> | |
<div style="padding-bottom: 25px;"> | |
<h3>Test Case 1: 3 CARI Products</h3> | |
<p>SKUs Scanned: <code>cari, cari, cari, peti </code></p> | |
<p>Total expected: $249.00</p> | |
> Result: <b style="display: inline;" id="test1" > </b> | |
</div> | |
<div style="padding-bottom: 25px;"> | |
<p>SKUs Scanned: <code>cari, homi, homi, cari, homi, homi, homi</code> </p> | |
<p>Total expected: $2718.95</p> | |
> Result: <b style="display: inline;" id="test2" > </b> | |
</div> | |
<div style="padding-bottom: 25px;"> | |
<p>SKUs Scanned: <code>heai, peti, homi</code> </p> | |
<p>Total expected: $1949.98</p> | |
> Result: <b style="display: inline;" id="test3" > </b> | |
</div> | |
<script> | |
/** | |
* Simple Pri | |
* | |
*/ | |
/** | |
* Product Listing | |
*/ | |
var products = { | |
homi : {id: "homi", name: "Home Insurance", price: 549.99}, | |
heai: {id: "heai", name: "Health Insurance", price: 1399.99}, | |
cari : {id: "cari", name: "Car Insurance", price: 109.50}, | |
peti : {id: "peti", name: "Pet Insurance", price: 30.00} | |
}; | |
/** | |
* Simple Pricing Rules | |
* @constructor | |
*/ | |
function PricingRules() { | |
this.applyPricingRule = function(items) { | |
var sum = 0; | |
var productIds = []; | |
var cariCtr = 0; | |
var heaiCtr = 0; | |
var homiCtr = 0; | |
var homiOver4 = false; | |
for (var i=0; i < items.length ;i++){ | |
productIds.push(items[i].id); | |
/** | |
* TODO: Implement each Pricing Rules in a different class/function | |
*/ | |
// Price Rule #1: 3 CARI for a price of 2 | |
if (items[i].id == 'cari'){ | |
cariCtr++; | |
if (cariCtr == 3){ | |
cariCtr = 0; // clear counter | |
continue; // Skip the 3rd CARI | |
} | |
} | |
// Price Rule #2: $50 price drop for more than 4 HOMI | |
if (items[i].id == 'homi') { | |
homiCtr++; | |
console.log('HOMI match! ' + homiCtr + ' ' + !homiOver4 ); | |
if (homiCtr == 4){ | |
console.log(' homi > 4 match!' ); | |
sum-=200; // Apply $50 discount on each 4 HOMI | |
// Succeeding HOMI to have a $0 discount | |
} else if (homiCtr > 4) { | |
console.log('Over 4 match!' ); | |
sum += (items[i].price-50); | |
continue; | |
} | |
} | |
// Price Rule #3: 3 CARI for a price of 2 | |
if (items[i].id == 'heai') { | |
heaiCtr++; | |
} | |
if (items[i].id == 'peti' && heaiCtr > 0){ | |
heaiCtr = 0; // clear counter | |
continue; | |
} | |
sum += items[i].price; | |
} | |
return sum; | |
} | |
}; | |
/** | |
* Checkout | |
* @param priceRules | |
* @constructor | |
*/ | |
function Checkout(priceRules) { | |
this.items = []; | |
this.pricingRules = priceRules; | |
this.scan = function(item) { | |
this.items.push(item); | |
}; | |
// Total price of the items without Price Rules | |
this.sumTotal = function() { | |
var sum = 0; | |
this.items.forEach(function(value, index){ | |
sum += value.price; | |
}); | |
return sum; | |
}; | |
// Total price of the items WITH Price Rules | |
this.total = function() { | |
return this.pricingRules.applyPricingRule(this.items); | |
} | |
}; | |
/** | |
* Tests | |
*/ | |
var myPricingRule = new PricingRules(); | |
var myCheckout1 = new Checkout(myPricingRule); | |
var myCheckout2 = new Checkout(myPricingRule); | |
var myCheckout3 = new Checkout(myPricingRule); | |
// Test Case 1 | |
myCheckout1.scan(products.cari); | |
myCheckout1.scan(products.cari); | |
myCheckout1.scan(products.cari); | |
myCheckout1.scan(products.peti); | |
// Test Case 2 : cari, homi, homi, cari, homi, homi, homi | |
myCheckout2.scan(products.cari); | |
myCheckout2.scan(products.homi); | |
myCheckout2.scan(products.homi); | |
myCheckout2.scan(products.cari); | |
myCheckout2.scan(products.homi); | |
myCheckout2.scan(products.homi); | |
myCheckout2.scan(products.homi); | |
// Test Case 3 : heai, peti, homi | |
myCheckout3.scan(products.heai); | |
myCheckout3.scan(products.peti); | |
myCheckout3.scan(products.homi); | |
// Fetch Total | |
test1 = myCheckout1.total(); | |
test2 = myCheckout2.total(); | |
test3 = myCheckout3.total(); | |
// Show results | |
document.getElementById("test1").innerHTML = test1; | |
document.getElementById("test2").innerHTML = test2; | |
document.getElementById("test3").innerHTML = test3; | |
</script> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment