From 9ae00086cf3f99342884cca5ae1459bbc95cec95 Mon Sep 17 00:00:00 2001 From: Akhona Mjikelo Date: Fri, 14 Apr 2023 09:47:21 +0200 Subject: [PATCH 01/22] initial commit --- js/calculate-bill.js | 36 ++++++++++++++++++++++++++++++++++++ js/text-bill.js | 3 +++ 2 files changed, 39 insertions(+) diff --git a/js/calculate-bill.js b/js/calculate-bill.js index e9220be6..a1addfef 100644 --- a/js/calculate-bill.js +++ b/js/calculate-bill.js @@ -1,8 +1,44 @@ //get a reference to the calculate button +const calculateBtn = document.querySelector(".calculateBtn") +function calculateBtnClicked(){ + + // 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 Date: Fri, 14 Apr 2023 12:04:35 +0200 Subject: [PATCH 02/22] second commit --- js/text-bill.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/js/text-bill.js b/js/text-bill.js index 6908c297..690815e5 100644 --- a/js/text-bill.js +++ b/js/text-bill.js @@ -1,15 +1,35 @@ -var callsTotal = 0; -var smsTotal = 0; -// get a reference to the textbox where the bill type is to be entered +const callTotalOne = document.querySelector(".callTotalOne") +const smsTotalOne = document.querySelector(".smsTotalOne") +const totalOne = document.querySelector(".totalOne") -//get a reference to the add button +// 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.trim(); + if (billTypeEntered === "bill"){ + 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); +} + +addToBillBtn.addEventListener('click', textBillTotal); // * add nothing for invalid values that is not 'call' or 'sms'. // * display the latest total on the screen From 168d860e0401f0f94608545c0cfb200a1c7003b2 Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Wed, 19 Apr 2023 08:55:19 +0200 Subject: [PATCH 03/22] added changes --- index.html | 18 ++++++------ js/calculate-bill.js | 13 ++++++++- js/radio-bill.js | 41 +++++++++++++++++++++++++-- js/settings-bill.js | 66 ++++++++++++++++++++++++++++++++++++++++++-- js/text-bill.js | 15 ++++++++-- 5 files changed, 135 insertions(+), 18 deletions(-) diff --git a/index.html b/index.html index fe364293..03492789 100644 --- a/index.html +++ b/index.html @@ -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
@@ -199,15 +199,15 @@
Totals
- + - + - +
Call totalR34.00 R0.00
SMS totalR7.35 R0.00
Total R41.35 R0.00
diff --git a/js/calculate-bill.js b/js/calculate-bill.js index a1addfef..89d27286 100644 --- a/js/calculate-bill.js +++ b/js/calculate-bill.js @@ -20,7 +20,7 @@ function calculateBtnClicked(){ billTotal += 2.75; } else if (billItem === "sms"){ - billTotal += 0.75; + billTotal += 0.75; } @@ -29,7 +29,17 @@ function calculateBtnClicked(){ var roundedBillTotal = billTotal.toFixed(2); billTotalElement.innerHTML = roundedBillTotal; } + +if(billTotal > 30 ){ + billTotalElement.classList.add("danger") +} + +else if(billTotal > 20 ){ + billTotalElement.classList.add("warning") +} } + + calculateBtn.addEventListener('click', calculateBtnClicked); @@ -40,6 +50,7 @@ const billTotalElement = document.querySelector(".billTotal") //get a reference to the billString const billStringElement = document.querySelector(".billString"); + //create the function that will be called when the calculate button is pressed // * this function should read the string value entered - split it on a comma. // * loop over all the entries in the the resulting list 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.js b/js/settings-bill.js index cf99fc89..002395f2 100644 --- a/js/settings-bill.js +++ b/js/settings-bill.js @@ -1,18 +1,78 @@ -// get a reference to the sms or call radio buttons +const totalSettings = document.querySelector(".totalSettings"); +const billItemTypeWithSettings = document.querySelector(".billItemTypeWithSettings"); +// get a reference to the sms or call radio buttons +const callTotalSettings = document.querySelector(".callTotalSettings"); +const smsTotalSettings = document.querySelector(".smsTotalSettings"); // get refences to all the settings fields - +const callCost = document.querySelector(".callCostSetting"); +const smsCost = document.querySelector(".smsCostSetting"); +const warningLevel =document.querySelector(".warningLevelSetting"); +const criticalLevel =document.querySelector(".criticalLevelSetting"); //get a reference to the add button +const addButton = document.querySelector(".button-primary"); //get a reference to the 'Update settings' button +const updateSettingsBtn = document.querySelector(".updateSettings"); + // create a variables that will keep track of all the settings +var callCostSetting = 0; +var smsCostSetting = 0; +var warningLevelSetting = 0; +var criticalLevelSetting = 0; + +updateSettingsBtn.addEventListener("click", billSettings); + +function billSettings(){ + var billChecked = document.querySelector("input[name='billItem']:checked") + + + callCostSetting = callCost.value; + smsCostSetting = smsCost.value; + warningLevelSetting = warningLevel.value; + criticalLevelSetting = criticalLevel.value; + +} + + + + console.log(callCost.value, smsCost.value, warningLevel.value, criticalLevel.value) // create a variables that will keep track of all three totals. +var callTotal = 0; +var smsTotal = 0; +var total = callTotal + smsTotal; -//add an event listener for when the 'Update settings' button is pressed +//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); + + + + +function billType(){ + + var clickedBtn = document.querySelector("input[name='billItem']:checked"); + + if (clickedBtn){ + var billItem = clickedBtn.value + + // if(billItem = "call"){ + // callTotal += 2.75 + // } + // else if(billItem = "sms"){ + // smsTotal += 0.75 + // } + + } + callTotalSettings.innerHTML = callTotal.toFixed(2); + smsTotalSettings.innerHTML = smsTotal.toFixed(2); + + totalSettings.innerHTML = total.toFixed(2); +} //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 690815e5..e2ec7f8a 100644 --- a/js/text-bill.js +++ b/js/text-bill.js @@ -1,4 +1,5 @@ + const callTotalOne = document.querySelector(".callTotalOne") const smsTotalOne = document.querySelector(".smsTotalOne") const totalOne = document.querySelector(".totalOne") @@ -15,8 +16,10 @@ var smsTotal = 0; //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.trim(); - if (billTypeEntered === "bill"){ + // console.log(billTypeEntered) + if (billTypeEntered === "call"){ callsTotal += 2.75 } else if (billTypeEntered === "sms"){ @@ -28,8 +31,16 @@ 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 From a93b13ea29c889c97a9e591ee1574e60c16e2be7 Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Sat, 22 Apr 2023 18:14:27 +0200 Subject: [PATCH 04/22] made changes --- index.html | 6 +-- js/settings-bill.js | 102 ++++++++++++++++++++------------------------ js/text-bill.js | 3 +- 3 files changed, 51 insertions(+), 60 deletions(-) diff --git a/index.html b/index.html index 03492789..ee0d2537 100644 --- a/index.html +++ b/index.html @@ -186,14 +186,14 @@

Bill with settings

Bill type
- +
Totals
diff --git a/js/settings-bill.js b/js/settings-bill.js index 002395f2..d9afb6b1 100644 --- a/js/settings-bill.js +++ b/js/settings-bill.js @@ -1,78 +1,70 @@ - -const totalSettings = document.querySelector(".totalSettings"); -const billItemTypeWithSettings = document.querySelector(".billItemTypeWithSettings"); -// get a reference to the sms or call radio buttons -const callTotalSettings = document.querySelector(".callTotalSettings"); -const smsTotalSettings = document.querySelector(".smsTotalSettings"); -// get refences to all the settings fields const callCost = document.querySelector(".callCostSetting"); const smsCost = document.querySelector(".smsCostSetting"); -const warningLevel =document.querySelector(".warningLevelSetting"); -const criticalLevel =document.querySelector(".criticalLevelSetting"); -//get a reference to the add button -const addButton = document.querySelector(".button-primary"); - -//get a reference to the 'Update settings' button -const updateSettingsBtn = document.querySelector(".updateSettings"); +const warningLevel = document.querySelector(".warningLevelSetting"); +const criticalLevel = document.querySelector(".criticalLevelSetting"); +const totalSettings = document.querySelector(".totalSettings"); +const callTotalSettings = document.querySelector(".callTotalSettings"); +const smsTotalSettings = document.querySelector(".smsTotalSettings"); -// create a variables that will keep track of all the settings +// 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; -updateSettingsBtn.addEventListener("click", billSettings); - -function billSettings(){ - var billChecked = document.querySelector("input[name='billItem']:checked") - - - callCostSetting = callCost.value; - smsCostSetting = smsCost.value; - warningLevelSetting = warningLevel.value; - criticalLevelSetting = criticalLevel.value; - +function updateBillSettings() { + callCostSetting = callCost.value; + smsCostSetting = smsCost.value; + warningLevelSetting = warningLevel.value; + criticalLevelSetting = criticalLevel.value; } - +const updateSettingsBtn = document.querySelector(".updateSettings"); +updateSettingsBtn.addEventListener("click", updateBillSettings); - console.log(callCost.value, smsCost.value, warningLevel.value, criticalLevel.value) +function addSettingsBtn() { + const billItemType = document.querySelector( + "input[name='billItemTypeWithSettings']:checked" + ); -// create a variables that will keep track of all three totals. -var callTotal = 0; -var smsTotal = 0; -var total = callTotal + smsTotal; + if (billItemType) { + var billItem = billItemType.value; + + if (billItem == "call") { + callTotal += Number(callCostSetting); + } + if (billItem == "sms") { + smsTotal += Number(smsCostSetting); + //console.log(smsTotal); + } + } + callTotalSettings.innerHTML = callTotal.toFixed(2); + smsTotalSettings.innerHTML = smsTotal.toFixed(2); + total = callTotal + smsTotal; + + totalSettings.innerHTML = total.toFixed(2); + if (total >= criticalLevelSetting) { + totalSettings.classList.add("danger"); + } else if (total >= warningLevelSetting) { + totalSettings.classList.add("warning"); + } +} +const addButton = document.querySelector(".add-setting-btn"); +addButton.addEventListener("click", addSettingsBtn); //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); - - +///addButton.addEventListener("click",billType); +// create a variables that will keep track of all the settings -function billType(){ - - var clickedBtn = document.querySelector("input[name='billItem']:checked"); - - if (clickedBtn){ - var billItem = clickedBtn.value - - // if(billItem = "call"){ - // callTotal += 2.75 - // } - // else if(billItem = "sms"){ - // smsTotal += 0.75 - // } - - } - callTotalSettings.innerHTML = callTotal.toFixed(2); - smsTotalSettings.innerHTML = smsTotal.toFixed(2); - - totalSettings.innerHTML = total.toFixed(2); -} +//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 e2ec7f8a..39d8c1cc 100644 --- a/js/text-bill.js +++ b/js/text-bill.js @@ -16,8 +16,7 @@ var smsTotal = 0; //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.trim(); + var billTypeEntered = billTypeText.value.toLowerCase().trim(); // console.log(billTypeEntered) if (billTypeEntered === "call"){ callsTotal += 2.75 From dfcff20c56004bf6a35cd48129db578a0a8dda29 Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Mon, 24 Apr 2023 11:30:05 +0200 Subject: [PATCH 05/22] updated the settings with bill widget --- js/settings-bill.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/js/settings-bill.js b/js/settings-bill.js index d9afb6b1..44ef0cfa 100644 --- a/js/settings-bill.js +++ b/js/settings-bill.js @@ -21,6 +21,8 @@ function updateBillSettings() { smsCostSetting = smsCost.value; warningLevelSetting = warningLevel.value; criticalLevelSetting = criticalLevel.value; + setColor() + } const updateSettingsBtn = document.querySelector(".updateSettings"); @@ -46,7 +48,16 @@ function addSettingsBtn() { smsTotalSettings.innerHTML = smsTotal.toFixed(2); total = callTotal + smsTotal; + setColor() + + +} + + +function setColor(){ totalSettings.innerHTML = total.toFixed(2); + totalSettings.classList.remove('danger') + totalSettings.classList.remove('warning') if (total >= criticalLevelSetting) { totalSettings.classList.add("danger"); } else if (total >= warningLevelSetting) { From 7440d4ffdc97cc7c64eaf502b1f2ac043c6a0919 Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Mon, 24 Apr 2023 11:41:19 +0200 Subject: [PATCH 06/22] made changes --- js/calculate-bill.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/js/calculate-bill.js b/js/calculate-bill.js index 89d27286..04d816bf 100644 --- a/js/calculate-bill.js +++ b/js/calculate-bill.js @@ -28,6 +28,9 @@ function calculateBtnClicked(){ var roundedBillTotal = billTotal.toFixed(2); billTotalElement.innerHTML = roundedBillTotal; + billTotalElement.classList.remove("danger") +billTotalElement.classList.remove("warning") + } if(billTotal > 30 ){ @@ -37,6 +40,7 @@ if(billTotal > 30 ){ else if(billTotal > 20 ){ billTotalElement.classList.add("warning") } + } From ec79b1991c5e72a3486ad0104aeb99df3052d19b Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Mon, 24 Apr 2023 12:40:34 +0200 Subject: [PATCH 07/22] added changes --- js/calculate-bill.js | 73 +++++++++++++++++++------------------------- js/settings-bill.js | 8 ++++- 2 files changed, 38 insertions(+), 43 deletions(-) diff --git a/js/calculate-bill.js b/js/calculate-bill.js index 04d816bf..bc0f8a7d 100644 --- a/js/calculate-bill.js +++ b/js/calculate-bill.js @@ -1,60 +1,49 @@ //get a reference to the calculate button -const calculateBtn = document.querySelector(".calculateBtn") -function calculateBtnClicked(){ +const calculateBtn = document.querySelector(".calculateBtn"); +function calculateBtnClicked() { + // get the string entered in the textArea + var billString = billStringElement.value.toLowerCase(); + //split the string + var billItems = billString.split(","); - // 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 + //split the string - for (var i=0;i 30 ){ - billTotalElement.classList.add("danger") -} - -else if(billTotal > 20 ){ - billTotalElement.classList.add("warning") -} + //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"); + } } - -calculateBtn.addEventListener('click', calculateBtnClicked); - - +calculateBtn.addEventListener("click", calculateBtnClicked); //get a reference to the billTotal element -const billTotalElement = document.querySelector(".billTotal") +const billTotalElement = document.querySelector(".billTotal"); //get a reference to the billString const billStringElement = document.querySelector(".billString"); - //create the function that will be called when the calculate button is pressed // * this function should read the string value entered - split it on a comma. // * loop over all the entries in the the resulting list diff --git a/js/settings-bill.js b/js/settings-bill.js index 44ef0cfa..5ff19fd6 100644 --- a/js/settings-bill.js +++ b/js/settings-bill.js @@ -33,7 +33,7 @@ function addSettingsBtn() { "input[name='billItemTypeWithSettings']:checked" ); - if (billItemType) { + if (billItemType && total < criticalLevelSetting) { var billItem = billItemType.value; if (billItem == "call") { @@ -43,6 +43,7 @@ function addSettingsBtn() { smsTotal += Number(smsCostSetting); //console.log(smsTotal); } + } callTotalSettings.innerHTML = callTotal.toFixed(2); smsTotalSettings.innerHTML = smsTotal.toFixed(2); @@ -63,10 +64,15 @@ function setColor(){ } else if (total >= warningLevelSetting) { totalSettings.classList.add("warning"); } + } 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) From be6b84e71384e0624991929440f44ab5a90990cb Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Mon, 15 May 2023 11:12:31 +0200 Subject: [PATCH 08/22] added dom references --- dom-tests/calculate-bill.test.js | 29 +++++ dom-tests/index.html | 34 +++++ dom-tests/settings-bill.test.js | 208 +++++++++++++++++++++++++++++++ index.html | 2 + js/calculate-bill-factory.js | 35 ++++++ js/calculate-bill.js | 69 ++++++---- js/settings-bill-factory.js | 77 ++++++++++++ js/settings-bill.js | 54 ++++---- 8 files changed, 456 insertions(+), 52 deletions(-) create mode 100644 dom-tests/calculate-bill.test.js create mode 100644 dom-tests/index.html create mode 100644 dom-tests/settings-bill.test.js create mode 100644 js/calculate-bill-factory.js create mode 100644 js/settings-bill-factory.js 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..b75a6eb3 --- /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..70eb194d --- /dev/null +++ b/dom-tests/settings-bill.test.js @@ -0,0 +1,208 @@ +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.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.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 ee0d2537..544beddb 100644 --- a/index.html +++ b/index.html @@ -229,6 +229,8 @@
Settings
+ + diff --git a/js/calculate-bill-factory.js b/js/calculate-bill-factory.js new file mode 100644 index 00000000..84c35fb2 --- /dev/null +++ b/js/calculate-bill-factory.js @@ -0,0 +1,35 @@ +function calculateBtnClicked() { + var billTotal = 0; + + function setCallOrSms(data) { + + 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 && billTotal < 30) { + return "warning"; + } + } + + function criticalLevel() { + if (billTotal >= 30) { + return "danger"; + } + } + + return { + setCallOrSms, + warningLevel, + criticalLevel, + }; +} diff --git a/js/calculate-bill.js b/js/calculate-bill.js index bc0f8a7d..6dd07ffd 100644 --- a/js/calculate-bill.js +++ b/js/calculate-bill.js @@ -1,48 +1,65 @@ //get a reference to the calculate button const calculateBtn = document.querySelector(".calculateBtn"); -function calculateBtnClicked() { - // get the string entered in the textArea - var billString = billStringElement.value.toLowerCase(); +const billTotalElement = document.querySelector(".billTotal"); +const billStringElement = document.querySelector(".billString"); + +calculateBtn.addEventListener("click", function(){ + + + let linkToCalculate = calculateBtnClicked(); + + let stringBill = billStringElement.value; + linkToCalculate.setCallOrSms(stringBill); + + billTotalElement.classList.add(linkToCalculate.warningLevel()); + billTotalElement.classList.add(linkToCalculate.criticalLevel()); + billTotalElement.innerHTML = linkToCalculate.setCallOrSms(); + +}); + +// function calculatePhoneBill () { + + + + //get the string entered in the textArea + //var billString = billStringElement.value.toLowerCase(); //split the string - var billItems = billString.split(","); + // var billItems = billString.split(","); //split the string // a variable for the total phone bill. - var billTotal = 0; + //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; - } + // 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"); - } + // 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"); - } -} + // if (billTotal > 30) { + // billTotalElement.classList.add("danger"); + // } else if (billTotal > 20) { + // billTotalElement.classList.add("warning"); + // } +//} -calculateBtn.addEventListener("click", calculateBtnClicked); //get a reference to the billTotal element -const billTotalElement = document.querySelector(".billTotal"); //get a reference to the billString -const billStringElement = document.querySelector(".billString"); //create the function that will be called when the calculate button is pressed // * this function should read the string value entered - split it on a comma. diff --git a/js/settings-bill-factory.js b/js/settings-bill-factory.js new file mode 100644 index 00000000..93473aff --- /dev/null +++ b/js/settings-bill-factory.js @@ -0,0 +1,77 @@ +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() { + callCostTotal += theCallCost; + } + function getTotalCost() { + return callCostTotal + smsCostTotal; + } + function getTotaCalllCost() { + return callCostTotal; + } + function getTotalSmsCost() { + return smsCostTotal; + } + function sendSms() { + smsCostTotal += theSmsCost; + } + function totalClassName() { + if ( + getTotalCost() >= getWarningLevel() && + getTotalCost() < getCriticalLevel() + ) { + return "warning"; + } + if (getTotalCost() >= getCriticalLevel()) { + return "danger"; + } + } + return { + setCallCost, + getCallCost, + setSmsCost, + getSmsCost, + setWarningLevel, + getWarningLevel, + setCriticalLevel, + getCriticalLevel, + makeCall, + getTotalCost, + getTotaCalllCost, + getTotalSmsCost, + sendSms, + totalClassName, + }; +} diff --git a/js/settings-bill.js b/js/settings-bill.js index 5ff19fd6..838b9e68 100644 --- a/js/settings-bill.js +++ b/js/settings-bill.js @@ -7,20 +7,23 @@ const totalSettings = document.querySelector(".totalSettings"); const callTotalSettings = document.querySelector(".callTotalSettings"); const smsTotalSettings = document.querySelector(".smsTotalSettings"); +// 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; +// var callTotal = 0; +// var smsTotal = 0; +// var total = 0; +// var callCostSetting = 0; +// var smsCostSetting = 0; +// var warningLevelSetting = 0; +// var criticalLevelSetting = 0; function updateBillSettings() { - callCostSetting = callCost.value; - smsCostSetting = smsCost.value; - warningLevelSetting = warningLevel.value; - criticalLevelSetting = criticalLevel.value; + // 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() } @@ -33,21 +36,22 @@ function addSettingsBtn() { "input[name='billItemTypeWithSettings']:checked" ); - if (billItemType && total < criticalLevelSetting) { + //if (billItemType && settingsBill.getTotalCost() < criticalLevelSetting) { var billItem = billItemType.value; if (billItem == "call") { - callTotal += Number(callCostSetting); + settingsBill.makeCall() } if (billItem == "sms") { - smsTotal += Number(smsCostSetting); + settingsBill.sendSms() //console.log(smsTotal); } - } - callTotalSettings.innerHTML = callTotal.toFixed(2); - smsTotalSettings.innerHTML = smsTotal.toFixed(2); - total = callTotal + smsTotal; + // } + callTotalSettings.innerHTML = settingsBill.getTotaCalllCost().toFixed(2); + smsTotalSettings.innerHTML = settingsBill.getTotalSmsCost().toFixed(2); + + // total = callTotal + smsTotal; setColor() @@ -56,22 +60,20 @@ function addSettingsBtn() { function setColor(){ - totalSettings.innerHTML = total.toFixed(2); + totalSettings.innerHTML = settingsBill.getTotalCost().toFixed(2); totalSettings.classList.remove('danger') totalSettings.classList.remove('warning') - if (total >= criticalLevelSetting) { - totalSettings.classList.add("danger"); - } else if (total >= warningLevelSetting) { - totalSettings.classList.add("warning"); - } + + totalSettings.classList.add(settingsBill.totalClassName()); + } const addButton = document.querySelector(".add-setting-btn"); addButton.addEventListener("click", addSettingsBtn); -if(total > criticalLevelSetting){ +// if(total > criticalLevelSetting){ -} +// } //add an event listener for when the 'Update settings' button is pressed From 68b18e2419f636f1a56f0a54f03335ba6947c0ab Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Tue, 16 May 2023 11:53:55 +0200 Subject: [PATCH 09/22] - --- index.html | 2 +- js/calculate-bill-factory.js | 8 +++++--- js/calculate-bill.js | 19 ++++++++++++------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/index.html b/index.html index 544beddb..fa73a01d 100644 --- a/index.html +++ b/index.html @@ -58,7 +58,7 @@

Calculate bill

Enter bill string
- +
diff --git a/js/calculate-bill-factory.js b/js/calculate-bill-factory.js index 84c35fb2..1a55c254 100644 --- a/js/calculate-bill-factory.js +++ b/js/calculate-bill-factory.js @@ -1,7 +1,9 @@ function calculateBtnClicked() { - var billTotal = 0; + function setCallOrSms(data) { + var billTotal = 0; + var billItems = data.split(","); for (var i = 0; i < billItems.length; i++) { @@ -11,7 +13,7 @@ function calculateBtnClicked() { billTotal += 0.75; } } - return billTotal; + return billTotal } @@ -22,7 +24,7 @@ function calculateBtnClicked() { } function criticalLevel() { - if (billTotal >= 30) { + if (billTotal > 30) { return "danger"; } } diff --git a/js/calculate-bill.js b/js/calculate-bill.js index 6dd07ffd..d9125201 100644 --- a/js/calculate-bill.js +++ b/js/calculate-bill.js @@ -1,19 +1,24 @@ //get a reference to the calculate button const calculateBtn = document.querySelector(".calculateBtn"); const billTotalElement = document.querySelector(".billTotal"); -const billStringElement = document.querySelector(".billString"); +const billStringElem = document.querySelector(".billString"); + +let linkToCalculate = calculateBtnClicked(); + calculateBtn.addEventListener("click", function(){ - let linkToCalculate = calculateBtnClicked(); + + + let stringBill = billStringElem.value; + - let stringBill = billStringElement.value; - linkToCalculate.setCallOrSms(stringBill); + billTotalElement.innerHTML = linkToCalculate.setCallOrSms(stringBill).toFixed(2); - billTotalElement.classList.add(linkToCalculate.warningLevel()); - billTotalElement.classList.add(linkToCalculate.criticalLevel()); - billTotalElement.innerHTML = linkToCalculate.setCallOrSms(); + + + }); From 01f6f47c30f43c387f2ca7c6c62b8c86df3a4422 Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Wed, 17 May 2023 09:11:04 +0200 Subject: [PATCH 10/22] - --- index.html | 4 ++-- js/calculate-bill-factory.js | 2 +- js/calculate-bill.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index fa73a01d..ec5da5ed 100644 --- a/index.html +++ b/index.html @@ -230,11 +230,11 @@
Settings
- + + - diff --git a/js/calculate-bill-factory.js b/js/calculate-bill-factory.js index 1a55c254..d18f2efd 100644 --- a/js/calculate-bill-factory.js +++ b/js/calculate-bill-factory.js @@ -34,4 +34,4 @@ function calculateBtnClicked() { warningLevel, criticalLevel, }; -} +} \ No newline at end of file diff --git a/js/calculate-bill.js b/js/calculate-bill.js index d9125201..65128d22 100644 --- a/js/calculate-bill.js +++ b/js/calculate-bill.js @@ -72,4 +72,4 @@ calculateBtn.addEventListener("click", function(){ // * 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 From e80314e2553342c25c46857c01c08a40860a9232 Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Wed, 17 May 2023 09:16:59 +0200 Subject: [PATCH 11/22] intial commit --- dom-tests/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dom-tests/index.html b/dom-tests/index.html index b75a6eb3..02bb9915 100644 --- a/dom-tests/index.html +++ b/dom-tests/index.html @@ -8,7 +8,7 @@
- +

hello

From 9403c4ddd508adbeeab1431fb4366391415db247 Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Wed, 17 May 2023 09:22:29 +0200 Subject: [PATCH 12/22] intial commit --- dom-tests/index.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dom-tests/index.html b/dom-tests/index.html index 02bb9915..528f3d80 100644 --- a/dom-tests/index.html +++ b/dom-tests/index.html @@ -20,12 +20,12 @@ - + - - + + - + @@ -21,10 +21,15 @@ + + + + + - + + + From 39948158b41f6ffb05fb44417f300821a75501f6 Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Wed, 17 May 2023 09:33:09 +0200 Subject: [PATCH 14/22] intial commit --- index.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index f0715cff..f661b35d 100644 --- a/index.html +++ b/index.html @@ -229,9 +229,9 @@
Settings
- - - + + + From ca6f4e89a484207bcb33e03c351b964fd66891dc Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Wed, 17 May 2023 09:35:55 +0200 Subject: [PATCH 15/22] intial commit --- index.html | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index f661b35d..ec5da5ed 100644 --- a/index.html +++ b/index.html @@ -229,9 +229,8 @@
Settings
- - - + + From 13e7ede36feb7c52f4b8c029b8869c0f15ff6dd6 Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Wed, 17 May 2023 09:38:40 +0200 Subject: [PATCH 16/22] intial commit --- dom-tests/index.html | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/dom-tests/index.html b/dom-tests/index.html index d3d37a65..b75a6eb3 100644 --- a/dom-tests/index.html +++ b/dom-tests/index.html @@ -8,7 +8,7 @@
- + @@ -20,17 +20,12 @@ - - + - - - - - + + - - + - + - + - - + + - - + + - + - + From 3419ab7724be8e492b7202ec3a8d3332ada6ea9c Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Thu, 18 May 2023 08:48:14 +0200 Subject: [PATCH 20/22] - --- js/calculate-bill-factory.js | 4 ++-- js/calculate-bill.js | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/js/calculate-bill-factory.js b/js/calculate-bill-factory.js index d18f2efd..ea97050c 100644 --- a/js/calculate-bill-factory.js +++ b/js/calculate-bill-factory.js @@ -1,8 +1,8 @@ function calculateBtnClicked() { - + var billTotal = 0; function setCallOrSms(data) { - var billTotal = 0; + var billItems = data.split(","); diff --git a/js/calculate-bill.js b/js/calculate-bill.js index 65128d22..5f3a3b93 100644 --- a/js/calculate-bill.js +++ b/js/calculate-bill.js @@ -15,6 +15,8 @@ calculateBtn.addEventListener("click", function(){ billTotalElement.innerHTML = linkToCalculate.setCallOrSms(stringBill).toFixed(2); + billTotalElement.classList.add(linkToCalculate.warningLevel()); + billTotalElement.classList.add(linkToCalculate.criticalLevel()); From 829115e751f723fac28fa4190ac206e58a58cc3f Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Thu, 18 May 2023 09:35:56 +0200 Subject: [PATCH 21/22] - --- js/calculate-bill-factory.js | 6 +++--- js/calculate-bill.js | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/js/calculate-bill-factory.js b/js/calculate-bill-factory.js index ea97050c..a84fdae9 100644 --- a/js/calculate-bill-factory.js +++ b/js/calculate-bill-factory.js @@ -2,7 +2,7 @@ function calculateBtnClicked() { var billTotal = 0; function setCallOrSms(data) { - + billTotal = 0; var billItems = data.split(","); @@ -18,13 +18,13 @@ function calculateBtnClicked() { function warningLevel() { - if (billTotal > 20 && billTotal < 30) { + if (billTotal >= 20 && billTotal < 30) { return "warning"; } } function criticalLevel() { - if (billTotal > 30) { + if (billTotal >= 30) { return "danger"; } } diff --git a/js/calculate-bill.js b/js/calculate-bill.js index 5f3a3b93..d2a4dabd 100644 --- a/js/calculate-bill.js +++ b/js/calculate-bill.js @@ -18,6 +18,7 @@ calculateBtn.addEventListener("click", function(){ billTotalElement.classList.add(linkToCalculate.warningLevel()); billTotalElement.classList.add(linkToCalculate.criticalLevel()); + From 1b3c1bfcc3366f123a7677d1ea3cda8fbef22ed9 Mon Sep 17 00:00:00 2001 From: AkhonaMj Date: Thu, 18 May 2023 11:57:50 +0200 Subject: [PATCH 22/22] - --- dom-tests/settings-bill.test.js | 2 ++ js/calculate-bill-factory.js | 37 ++++++++++++++++++++++++--------- js/calculate-bill.js | 11 ++++++---- js/settings-bill-factory.js | 15 +++++++++++-- js/settings-bill.js | 9 +++++--- 5 files changed, 55 insertions(+), 19 deletions(-) diff --git a/dom-tests/settings-bill.test.js b/dom-tests/settings-bill.test.js index 70eb194d..7d0eeeda 100644 --- a/dom-tests/settings-bill.test.js +++ b/dom-tests/settings-bill.test.js @@ -89,6 +89,7 @@ describe("Use Values", function () { let settingsBill = BillWithSettings(); settingsBill.setCallCost(1.35); settingsBill.setSmsCost(0.85); + settingsBill.setCriticalLevel(10); settingsBill.sendSms(); @@ -102,6 +103,7 @@ describe("Use Values", function () { let settingsBill = BillWithSettings(); settingsBill.setCallCost(1.35); settingsBill.setSmsCost(0.85); + settingsBill.setCriticalLevel(10) settingsBill.sendSms(); diff --git a/js/calculate-bill-factory.js b/js/calculate-bill-factory.js index a84fdae9..0bceb707 100644 --- a/js/calculate-bill-factory.js +++ b/js/calculate-bill-factory.js @@ -3,22 +3,25 @@ function calculateBtnClicked() { 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; + 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 + } - return billTotal - } + function warningLevel() { - if (billTotal >= 20 && billTotal < 30) { + if (billTotal >= 20 ) { return "warning"; } } @@ -28,10 +31,24 @@ function calculateBtnClicked() { 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 d2a4dabd..ef9dd866 100644 --- a/js/calculate-bill.js +++ b/js/calculate-bill.js @@ -15,13 +15,16 @@ calculateBtn.addEventListener("click", function(){ billTotalElement.innerHTML = linkToCalculate.setCallOrSms(stringBill).toFixed(2); - billTotalElement.classList.add(linkToCalculate.warningLevel()); - billTotalElement.classList.add(linkToCalculate.criticalLevel()); - + + billTotalElement.classList.remove(linkToCalculate.clearWarningLevel()); + billTotalElement.classList.remove(linkToCalculate.clearCriticalLevel()); - + + billTotalElement.classList.add(linkToCalculate.warningLevel()); + billTotalElement.classList.add(linkToCalculate.criticalLevel()); + }); diff --git a/js/settings-bill-factory.js b/js/settings-bill-factory.js index 93473aff..15d4042f 100644 --- a/js/settings-bill-factory.js +++ b/js/settings-bill-factory.js @@ -33,8 +33,10 @@ function BillWithSettings() { return theCriticalLevel; } function makeCall() { + if(!reachedDanger()){ callCostTotal += theCallCost; } +} function getTotalCost() { return callCostTotal + smsCostTotal; } @@ -45,8 +47,16 @@ function BillWithSettings() { return smsCostTotal; } function sendSms() { - smsCostTotal += theSmsCost; + if(!reachedDanger()){ + smsCostTotal += theSmsCost; + } + } + + function reachedDanger(){ + return getTotalCost() >= getCriticalLevel() + } + function totalClassName() { if ( getTotalCost() >= getWarningLevel() && @@ -54,7 +64,7 @@ function BillWithSettings() { ) { return "warning"; } - if (getTotalCost() >= getCriticalLevel()) { + if (reachedDanger()){ return "danger"; } } @@ -73,5 +83,6 @@ function BillWithSettings() { getTotalSmsCost, sendSms, totalClassName, + reachedDanger }; } diff --git a/js/settings-bill.js b/js/settings-bill.js index 838b9e68..b6f2d9f5 100644 --- a/js/settings-bill.js +++ b/js/settings-bill.js @@ -36,7 +36,7 @@ function addSettingsBtn() { "input[name='billItemTypeWithSettings']:checked" ); - //if (billItemType && settingsBill.getTotalCost() < criticalLevelSetting) { + var billItem = billItemType.value; if (billItem == "call") { @@ -44,10 +44,9 @@ function addSettingsBtn() { } if (billItem == "sms") { settingsBill.sendSms() - //console.log(smsTotal); + } - // } callTotalSettings.innerHTML = settingsBill.getTotaCalllCost().toFixed(2); smsTotalSettings.innerHTML = settingsBill.getTotalSmsCost().toFixed(2); @@ -71,6 +70,10 @@ function setColor(){ const addButton = document.querySelector(".add-setting-btn"); addButton.addEventListener("click", addSettingsBtn); + + + + // if(total > criticalLevelSetting){ // }