diff --git a/dom-tests/calculate-bill.test.js b/dom-tests/calculate-bill.test.js new file mode 100644 index 00000000..c715c0b9 --- /dev/null +++ b/dom-tests/calculate-bill.test.js @@ -0,0 +1,29 @@ +describe("The calculate bill function", function(){ + +it(" If two calls were made , it should return 5.5", function(){ + var calculateBill = calculateBtnClicked(); +//console.log( calculateBill.setCallOrSms("sms")) +assert.equal(5.50, calculateBill.setCallOrSms("call,call")); + +}); + + +it("If two sms's were made , it should return 1.5 ", function(){ + var calculateBill = calculateBtnClicked(); + +assert.equal(1.50, calculateBill.setCallOrSms("sms,sms")); + +}); + +it("Should return a 'warning' class name if total reaches 20", function(){ + var calculateBill = calculateBtnClicked(); + assert.equal(20.50, calculateBill.setCallOrSms("call,sms,call,sms,sms,call,sms,call,sms,sms,call,sms,sms,sms")); + assert.equal("warning", calculateBill.warningLevel()); +}) +it("Should return a 'critical' class name if total reachs 30", function(){ + var calculateBill = calculateBtnClicked(); + + assert.equal(30.25, calculateBill.setCallOrSms("call,call,call,call,call,call,call,call,call,call,call")); + assert.equal("danger", calculateBill.criticalLevel()) +}) +}); diff --git a/dom-tests/index.html b/dom-tests/index.html new file mode 100644 index 00000000..09f3afe2 --- /dev/null +++ b/dom-tests/index.html @@ -0,0 +1,34 @@ + + + + + Mocha Tests + + + + +
+ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/dom-tests/settings-bill.test.js b/dom-tests/settings-bill.test.js new file mode 100644 index 00000000..7d0eeeda --- /dev/null +++ b/dom-tests/settings-bill.test.js @@ -0,0 +1,210 @@ +describe("The bill with settings function", function () { + it("should be able to set the call cost", function () { + let settingsBill = BillWithSettings(); + settingsBill.setCallCost(1.85); + assert.equal(1.85, settingsBill.getCallCost()); + + let settingsBill2 = BillWithSettings(); + settingsBill2.setCallCost(2.75); + assert.equal(2.75, settingsBill2.getCallCost()); + }); + + it("should be able to set the sms cost", function () { + let settingsBill = BillWithSettings(); + settingsBill.setSmsCost(0.85); + assert.equal(0.85, settingsBill.getSmsCost()); + + let settingsBill2 = BillWithSettings(); + settingsBill2.setSmsCost(0.75); + assert.equal(0.75, settingsBill2.getSmsCost()); + }); + + it("should be able to set the call and sms cost", function () { + let settingsBill = BillWithSettings(); + settingsBill.setCallCost(2.75); + settingsBill.setSmsCost(0.85); + assert.equal(2.75, settingsBill.getCallCost()); + assert.equal(0.85, settingsBill.getSmsCost()); + + let settingsBill2 = BillWithSettings(); + settingsBill2.setCallCost(1.75); + settingsBill2.setSmsCost(0.65); + assert.equal(1.75, settingsBill2.getCallCost()); + assert.equal(0.65, settingsBill2.getSmsCost()); + }); + + it("should be able to set the warning level", function () { + let settingsBill = BillWithSettings(); + settingsBill.setWarningLevel(20); + assert.equal(20, settingsBill.getWarningLevel()); + }); + it("should be able to set the critical level", function () { + let settingsBill = BillWithSettings(); + settingsBill.setCriticalLevel(30); + assert.equal(30, settingsBill.getCriticalLevel()); + }); + + it("should be able to set the warning and the critical level", function () { + let settingsBill = BillWithSettings(); + settingsBill.setWarningLevel(20); + settingsBill.setCriticalLevel(30); + assert.equal(20, settingsBill.getWarningLevel()); + assert.equal(30, settingsBill.getCriticalLevel()); + }); +}); + + +describe("Use Values", function () { + it("should be able to use the call cost set", function () { + let settingsBill = BillWithSettings(); + settingsBill.setCriticalLevel(10); + + + settingsBill.setCallCost(2.25); + settingsBill.setSmsCost(0.85); + + settingsBill.makeCall(); + settingsBill.makeCall(); + settingsBill.makeCall(); + + assert.equal(6.75, settingsBill.getTotalCost()); + assert.equal(6.75, settingsBill.getTotaCalllCost()); + assert.equal(0.0, settingsBill.getTotalSmsCost()); + }); + + it("should be able to use the call cost set for two calls at 1.35 each", function () { + let settingsBill = BillWithSettings(); + settingsBill.setCallCost(1.35); + settingsBill.setSmsCost(0.85); + settingsBill.setCriticalLevel(10); + + settingsBill.makeCall(); + settingsBill.makeCall(); + + assert.equal(2.7, settingsBill.getTotalCost()); + assert.equal(2.7, settingsBill.getTotaCalllCost()); + assert.equal(0.0, settingsBill.getTotalSmsCost()); + }); + it("should be able to send two smses at 0.85 each", function () { + let settingsBill = BillWithSettings(); + settingsBill.setCallCost(1.35); + settingsBill.setSmsCost(0.85); + settingsBill.setCriticalLevel(10); + + + settingsBill.sendSms(); + settingsBill.sendSms(); + + assert.equal(1.7, settingsBill.getTotalCost()); + assert.equal(0.0, settingsBill.getTotaCalllCost()); + assert.equal(1.7, settingsBill.getTotalSmsCost()); + }); + it("should be able to send two smses at 0.85 each and make one call at 1.35", function () { + let settingsBill = BillWithSettings(); + settingsBill.setCallCost(1.35); + settingsBill.setSmsCost(0.85); + settingsBill.setCriticalLevel(10) + + + settingsBill.sendSms(); + settingsBill.sendSms(); + settingsBill.makeCall(); + + assert.equal(3.05, settingsBill.getTotalCost()); + assert.equal(1.35, settingsBill.getTotaCalllCost()); + assert.equal(1.7, settingsBill.getTotalSmsCost()); + }); +}); + +describe("Warning and Critical level", function () { + + it("Should return a class name if warning level is reached", function () { + let settingsBill = BillWithSettings(); + + + settingsBill.setCallCost(1.35); + settingsBill.setSmsCost(0.85); + settingsBill.setWarningLevel(5); + settingsBill.setCriticalLevel(10); + + settingsBill.sendSms(); + settingsBill.makeCall(); + settingsBill.makeCall(); + settingsBill.makeCall(); + settingsBill.makeCall(); + assert.equal("warning", settingsBill.totalClassName()); + }); + it("Should return a class name 'critical' if critical level is reached", function () { + + let settingsBill = BillWithSettings(); + + + settingsBill.setCallCost(2.50); + settingsBill.setSmsCost(0.5); + settingsBill.setWarningLevel(5); + settingsBill.setCriticalLevel(10); + + settingsBill.sendSms(); + settingsBill.makeCall(); + settingsBill.makeCall(); + settingsBill.makeCall(); + settingsBill.makeCall(); + settingsBill.makeCall(); + // console.log(settingsBill.getTotalCost()) + + assert.equal("danger", settingsBill.totalClassName()); + + }); + + it("Should stop the total call cost from increasing when the critical level has been reached.", function () { + let settingsBill = BillWithSettings(); + + + settingsBill.setCallCost(2); + settingsBill.setSmsCost(0.5); + settingsBill.setCriticalLevel(10); + + settingsBill.sendSms(); + settingsBill.makeCall(); + settingsBill.makeCall(); + settingsBill.makeCall(); + settingsBill.makeCall(); + settingsBill.makeCall(); + assert.equal("danger", settingsBill.totalClassName()); + assert.equal(10, settingsBill.getTotaCalllCost()); + + + + }); + it("Should allow the total to increase when the critical level has been reached, then upping the critical level", function () { + let settingsBill = BillWithSettings(); + + + settingsBill.setCallCost(2.); + + settingsBill.setCallCost(2.); + settingsBill.setSmsCost(0.5); + settingsBill.setWarningLevel(8); + settingsBill.setCriticalLevel(10); + + settingsBill.sendSms(); + settingsBill.makeCall(); + settingsBill.makeCall(); + settingsBill.makeCall(); + settingsBill.makeCall(); + settingsBill.makeCall(); + assert.equal("danger", settingsBill.totalClassName()); + assert.equal(10, settingsBill.getTotaCalllCost()); + + settingsBill.setCriticalLevel(20); + assert.equal("warning", settingsBill.totalClassName()); + settingsBill.makeCall(); + settingsBill.makeCall(); + settingsBill.makeCall(); + assert.equal(16, settingsBill.getTotaCalllCost()); + + + + }); + +}); diff --git a/index.html b/index.html index fe364293..ec5da5ed 100644 --- a/index.html +++ b/index.html @@ -58,7 +58,7 @@

Calculate bill

Enter bill string
- +
@@ -95,15 +95,15 @@
Totals
- + - + - +
Call totalR27.50R0.00
SMS totalR13.75R0.00
TotalR41.25R0.00
@@ -151,15 +151,15 @@
Totals
- + - + - +
Call totalR34.50R0.00
SMS totalR3.00R0.00
TotalR37.50R0.00
@@ -186,28 +186,28 @@

Bill with settings

Bill type
- +
Totals
- + - + - +
Call totalR34.00 R0.00
SMS totalR7.35 R0.00
Total R41.35 R0.00
@@ -229,10 +229,12 @@
Settings
+ + + - diff --git a/js/calculate-bill-factory.js b/js/calculate-bill-factory.js new file mode 100644 index 00000000..0bceb707 --- /dev/null +++ b/js/calculate-bill-factory.js @@ -0,0 +1,54 @@ +function calculateBtnClicked() { + var billTotal = 0; + + function setCallOrSms(data) { + billTotal = 0; + + + + var billItems = data.split(","); + for (var i = 0; i < billItems.length; i++) { + if (billItems[i].trim().toLowerCase() === "call") { + billTotal += 2.75; + } else if (billItems[i].trim().toLowerCase() === "sms") { + billTotal += 0.75; + } + } + return billTotal + + } + + + + function warningLevel() { + if (billTotal >= 20 ) { + return "warning"; + } + } + + function criticalLevel() { + if (billTotal >= 30) { + return "danger"; + } + } + + function clearWarningLevel() { + if (billTotal < 20 ) { + return "warning"; + } + } + function clearCriticalLevel() { + if (billTotal < 30) { + return "danger"; + } + } + + return { + setCallOrSms, + warningLevel, + criticalLevel, + clearWarningLevel, + clearCriticalLevel, + + }; +} \ No newline at end of file diff --git a/js/calculate-bill.js b/js/calculate-bill.js index e9220be6..ef9dd866 100644 --- a/js/calculate-bill.js +++ b/js/calculate-bill.js @@ -1,4 +1,72 @@ //get a reference to the calculate button +const calculateBtn = document.querySelector(".calculateBtn"); +const billTotalElement = document.querySelector(".billTotal"); +const billStringElem = document.querySelector(".billString"); + +let linkToCalculate = calculateBtnClicked(); + + +calculateBtn.addEventListener("click", function(){ + + + + + let stringBill = billStringElem.value; + + + billTotalElement.innerHTML = linkToCalculate.setCallOrSms(stringBill).toFixed(2); + + + + billTotalElement.classList.remove(linkToCalculate.clearWarningLevel()); + billTotalElement.classList.remove(linkToCalculate.clearCriticalLevel()); + + + billTotalElement.classList.add(linkToCalculate.warningLevel()); + billTotalElement.classList.add(linkToCalculate.criticalLevel()); + + +}); + +// function calculatePhoneBill () { + + + + //get the string entered in the textArea + //var billString = billStringElement.value.toLowerCase(); + //split the string + // var billItems = billString.split(","); + + //split the string + + // a variable for the total phone bill. + //var billTotal = 0; + + //loop over all the bill items + + // for (var i = 0; i < billItems.length; i++) { + // var billItem = billItems[i].trim(); + // if (billItem === "call") { + // billTotal += 2.75; + // } else if (billItem === "sms") { + // billTotal += 0.75; +// } + + //round to two decimals + + // var roundedBillTotal = billTotal.toFixed(2); + // billTotalElement.innerHTML = roundedBillTotal; + // billTotalElement.classList.remove("danger"); + // billTotalElement.classList.remove("warning"); + // } + + // if (billTotal > 30) { + // billTotalElement.classList.add("danger"); + // } else if (billTotal > 20) { + // billTotalElement.classList.add("warning"); + // } +//} + //get a reference to the billTotal element @@ -10,4 +78,4 @@ // * check if it is a call or an sms and add the right amount to the overall total // * once done looping over all the entries - display the total onto the screen in the billTotal element -//link the function to a click event on the calculate button +//link the function to a click event on the calculate button \ No newline at end of file diff --git a/js/radio-bill.js b/js/radio-bill.js index 33cba4ea..71b3dcde 100644 --- a/js/radio-bill.js +++ b/js/radio-bill.js @@ -1,12 +1,47 @@ -// get a reference to the sms or call radio buttons +const billItemTypeRadio = document.querySelector(".billItemTypeRadio") +const totalTwo = document.querySelector(".totalTwo") -//get a reference to the add button +// get a reference to the sms or call radio buttons +const callTotalTwo = document.querySelector(".callTotalTwo") +const smsTotalTwo = document.querySelector(".smsTotalTwo") +//get a reference to the add button +const radioBillAddBtn = document.querySelector(".radioBillAddBtn") //create a variable that will keep track of the total bill - +var totalCall = 0; +var totalSms = 0; //add an event listener for when the add button is pressed +radioBillAddBtn.addEventListener("click", radioBillTotal); //in the event listener get the value from the billItemTypeRadio radio buttons +function radioBillTotal(){ + +var checkedRadioBtn = document.querySelector("input[name='billItemType']:checked"); + +if (checkedRadioBtn){ + var billItemType = checkedRadioBtn.value + + if(billItemType === "call"){ + totalCall += 2.75 + } + else if(billItemType === "sms"){ + totalSms += 0.75 + } + +} + // * add the appropriate value to the running total + +callTotalTwo.innerHTML = totalCall.toFixed(2); +smsTotalTwo.innerHTML = totalSms.toFixed(2); + var totalCost = totalCall + totalSms; + totalTwo.innerHTML = totalCost.toFixed(2); + if(totalCost > 50 ){ + totalTwo.classList.add("danger") + } + else if(totalCost > 30){ + totalTwo.classList.add("warning") + } +} // * add nothing for invalid values that is not 'call' or 'sms'. // * display the latest total on the screen diff --git a/js/settings-bill-factory.js b/js/settings-bill-factory.js new file mode 100644 index 00000000..15d4042f --- /dev/null +++ b/js/settings-bill-factory.js @@ -0,0 +1,88 @@ +function BillWithSettings() { + var theCallCost = 0; + var theSmsCost = 0; + var theWarningLevel = 0; + var theCriticalLevel = 0; + var callCostTotal = 0; + var smsCostTotal = 0; + + function setCallCost(callCost) { + theCallCost = callCost; + } + function getCallCost() { + return theCallCost; + } + function setSmsCost(smsCost) { + theSmsCost = smsCost; + } + function getSmsCost() { + return theSmsCost; + } + + function setWarningLevel(warningLevel) { + theWarningLevel = warningLevel; + } + function getWarningLevel() { + return theWarningLevel; + } + + function setCriticalLevel(criticalLevel) { + theCriticalLevel = criticalLevel; + } + function getCriticalLevel() { + return theCriticalLevel; + } + function makeCall() { + if(!reachedDanger()){ + callCostTotal += theCallCost; + } +} + function getTotalCost() { + return callCostTotal + smsCostTotal; + } + function getTotaCalllCost() { + return callCostTotal; + } + function getTotalSmsCost() { + return smsCostTotal; + } + function sendSms() { + if(!reachedDanger()){ + smsCostTotal += theSmsCost; + } + + } + + function reachedDanger(){ + return getTotalCost() >= getCriticalLevel() + } + + function totalClassName() { + if ( + getTotalCost() >= getWarningLevel() && + getTotalCost() < getCriticalLevel() + ) { + return "warning"; + } + if (reachedDanger()){ + return "danger"; + } + } + return { + setCallCost, + getCallCost, + setSmsCost, + getSmsCost, + setWarningLevel, + getWarningLevel, + setCriticalLevel, + getCriticalLevel, + makeCall, + getTotalCost, + getTotaCalllCost, + getTotalSmsCost, + sendSms, + totalClassName, + reachedDanger + }; +} diff --git a/js/settings-bill.js b/js/settings-bill.js index cf99fc89..b6f2d9f5 100644 --- a/js/settings-bill.js +++ b/js/settings-bill.js @@ -1,18 +1,92 @@ -// get a reference to the sms or call radio buttons +const callCost = document.querySelector(".callCostSetting"); +const smsCost = document.querySelector(".smsCostSetting"); +const warningLevel = document.querySelector(".warningLevelSetting"); +const criticalLevel = document.querySelector(".criticalLevelSetting"); -// get refences to all the settings fields +const totalSettings = document.querySelector(".totalSettings"); +const callTotalSettings = document.querySelector(".callTotalSettings"); +const smsTotalSettings = document.querySelector(".smsTotalSettings"); -//get a reference to the add button +// created a function instance +let settingsBill = BillWithSettings() +// create a variables that will keep track of all three totals. +// var callTotal = 0; +// var smsTotal = 0; +// var total = 0; +// var callCostSetting = 0; +// var smsCostSetting = 0; +// var warningLevelSetting = 0; +// var criticalLevelSetting = 0; -//get a reference to the 'Update settings' button +function updateBillSettings() { + // call the functions to set the values, be mindful to call the instantance of the factory function + settingsBill.setCallCost(Number (callCost.value)); + settingsBill.setSmsCost(Number(smsCost.value)); + settingsBill.setWarningLevel(Number(warningLevel.value)); + settingsBill.setCriticalLevel(Number(criticalLevel.value)); + setColor() + +} -// create a variables that will keep track of all the settings +const updateSettingsBtn = document.querySelector(".updateSettings"); +updateSettingsBtn.addEventListener("click", updateBillSettings); -// create a variables that will keep track of all three totals. +function addSettingsBtn() { + const billItemType = document.querySelector( + "input[name='billItemTypeWithSettings']:checked" + ); -//add an event listener for when the 'Update settings' button is pressed + var billItem = billItemType.value; + + if (billItem == "call") { + settingsBill.makeCall() + } + if (billItem == "sms") { + settingsBill.sendSms() + + } + + callTotalSettings.innerHTML = settingsBill.getTotaCalllCost().toFixed(2); + smsTotalSettings.innerHTML = settingsBill.getTotalSmsCost().toFixed(2); + + // total = callTotal + smsTotal; + + setColor() + + +} + + +function setColor(){ + totalSettings.innerHTML = settingsBill.getTotalCost().toFixed(2); + totalSettings.classList.remove('danger') + totalSettings.classList.remove('warning') + + totalSettings.classList.add(settingsBill.totalClassName()); + + +} + +const addButton = document.querySelector(".add-setting-btn"); +addButton.addEventListener("click", addSettingsBtn); + + + + +// if(total > criticalLevelSetting){ + +// } + + +//add an event listener for when the 'Update settings' button is pressed +//updateSettingsBtn.addEventListener("click",billSettings) //add an event listener for when the add button is pressed +///addButton.addEventListener("click",billType); + +// create a variables that will keep track of all the settings + +//get a reference to the add button //in the event listener get the value from the billItemTypeRadio radio buttons // * add the appropriate value to the call / sms total diff --git a/js/text-bill.js b/js/text-bill.js index d7f7c73d..39d8c1cc 100644 --- a/js/text-bill.js +++ b/js/text-bill.js @@ -1,12 +1,45 @@ -// get a reference to the textbox where the bill type is to be entered -//get a reference to the add button -//create a variable that will keep track of the total bill +const callTotalOne = document.querySelector(".callTotalOne") +const smsTotalOne = document.querySelector(".smsTotalOne") +const totalOne = document.querySelector(".totalOne") -//add an event listener for when the add button is pressed +// get a reference to the textbox where the bill type is to be entered +const billTypeText = document.querySelector(".billTypeText") +//get a reference to the add button +const addToBillBtn = document.querySelector(".addToBillBtn") +//create a variable that will keep track of the total bill +var callsTotal = 0; +var smsTotal = 0; +//add an event listener for when the add button is pressed +//addToBillBtn.addEventListener('click', addToBillBtn); //in the event listener check if the value in the bill type textbox is 'sms' or 'call' +function textBillTotal(){ + var billTypeEntered = billTypeText.value.toLowerCase().trim(); + // console.log(billTypeEntered) + if (billTypeEntered === "call"){ + callsTotal += 2.75 + } + else if (billTypeEntered === "sms"){ + smsTotal += 0.75; + } + // * add the appropriate value to the running total +callTotalOne.innerHTML = callsTotal.toFixed(2); +smsTotalOne.innerHTML = smsTotal.toFixed(2); + var totalCost = callsTotal + smsTotal; + totalOne.innerHTML = totalCost.toFixed(2); + + if(totalCost > 50){ + totalOne.classList.add("danger") + } + else if(totalCost > 30){ + totalOne.classList.add("warning") + } + +} + +addToBillBtn.addEventListener('click', textBillTotal); // * add nothing for invalid values that is not 'call' or 'sms'. -// * display the latest total on the screen +// * display the latest total on the screen \ No newline at end of file