Skip to content

Commit bd85cc3

Browse files
committed
Create cashRegister.js
1 parent 2519ab8 commit bd85cc3

1 file changed

Lines changed: 385 additions & 0 deletions

File tree

CashRegister/cashRegister.js

Lines changed: 385 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,385 @@
1+
//Structure for maintaining cash denominations as a single unit
2+
function cashDenominationCounts(t20Count, t10Count, t5Count, t2Count,
3+
t1Count, t50cCount, t25cCount, t10cCount, t5cCount, t1cCount) {
4+
this.t20Count = t20Count;
5+
this.t10Count = t10Count;
6+
this.t5Count = t5Count;
7+
this.t2Count = t2Count;
8+
this.t1Count = t1Count;
9+
this.t50cCount = t50cCount;
10+
this.t25cCount = t25cCount;
11+
this.t10cCount = t10cCount;
12+
this.t5cCount = t5cCount;
13+
this.t1cCount = t1cCount;
14+
};
15+
16+
function getTotal(currencyCounts) {
17+
18+
return (20 * currencyCounts.t20Count
19+
+ 10 * currencyCounts.t10Count
20+
+ 5 * currencyCounts.t5Count
21+
+ 2 * currencyCounts.t2Count
22+
+ 1 * currencyCounts.t1Count
23+
+ 0.5 * currencyCounts.t50cCount
24+
+ 0.25 * currencyCounts.t25cCount
25+
+ 0.10 * currencyCounts.t10cCount
26+
+ 0.05 * currencyCounts.t5cCount
27+
+ 0.01 * currencyCounts.t1cCount);
28+
};
29+
30+
//Cash Register Object structure
31+
var cashRegister = {
32+
33+
total: 0,
34+
currentCashCount: null,
35+
36+
//Initialize Cash Register
37+
init: function (currencyCounts) {
38+
this.currentCashCount = new cashDenominationCounts(currencyCounts.t20Count, currencyCounts.t10Count, currencyCounts.t5Count, currencyCounts.t2Count, currencyCounts.t1Count, currencyCounts.t50cCount, currencyCounts.t25cCount, currencyCounts.t10cCount, currencyCounts.t5cCount, currencyCounts.t1cCount);
39+
this.total = getTotal(this.currentCashCount);
40+
},
41+
42+
//Update the Cash Register denominations with cash denominations paid by the customer
43+
updateRegisterCashDenom: function (amountPaidObj) {
44+
45+
this.currentCashCount.t20Count = this.currentCashCount.t20Count + amountPaidObj.t20Count;
46+
this.currentCashCount.t10Count = this.currentCashCount.t10Count + amountPaidObj.t10Count;
47+
this.currentCashCount.t5Count = this.currentCashCount.t5Count + amountPaidObj.t5Count;
48+
this.currentCashCount.t2Count = this.currentCashCount.t2Count + amountPaidObj.t2Count;
49+
this.currentCashCount.t1Count = this.currentCashCount.t1Count + amountPaidObj.t1Count;
50+
this.currentCashCount.t50cCount = this.currentCashCount.t50cCount + amountPaidObj.t50cCount;
51+
this.currentCashCount.t25cCount = this.currentCashCount.t25cCount + amountPaidObj.t25cCount;
52+
this.currentCashCount.t10cCount = this.currentCashCount.t10cCount + amountPaidObj.t10cCount;
53+
this.currentCashCount.t5cCount = this.currentCashCount.t5cCount + amountPaidObj.t5cCount;
54+
this.currentCashCount.t1cCount = this.currentCashCount.t1cCount + amountPaidObj.t1cCount;
55+
},
56+
57+
//Calculate the change to be provided to the customer
58+
calculateChange: function (totalBill, amountPaidObj) {
59+
var returnCashCount = new cashDenominationCounts(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
60+
if (getTotal(amountPaidObj) >= totalBill) {
61+
this.updateRegisterCashDenom(amountPaidObj);
62+
var residualCash = getTotal(amountPaidObj) - totalBill; //to calculate change to be given
63+
residualCash = residualCash.toFixed(2);
64+
while((residualCash >= 20) && (this.currentCashCount.t20Count>0)){
65+
residualCash = parseFloat(residualCash).toFixed(2);
66+
residualCash = residualCash - 20;
67+
this.currentCashCount.t20Count = this.currentCashCount.t20Count - 1;
68+
returnCashCount.t20Count++;
69+
}
70+
71+
residualCash = parseFloat(residualCash).toFixed(2);
72+
while((residualCash >= 10) && (this.currentCashCount.t10Count>0)){
73+
residualCash = parseFloat(residualCash).toFixed(2);
74+
residualCash = residualCash - 10;
75+
this.currentCashCount.t10Count = this.currentCashCount.t10Count - 1;
76+
returnCashCount.t10Count++;
77+
}
78+
79+
residualCash = parseFloat(residualCash).toFixed(2);
80+
while((residualCash >= 5) && (this.currentCashCount.t5Count>0)){
81+
residualCash = parseFloat(residualCash).toFixed(2);
82+
residualCash = residualCash - 5;
83+
this.currentCashCount.t5Count = this.currentCashCount.t5Count - 1;
84+
returnCashCount.t5Count++;
85+
}
86+
87+
residualCash = parseFloat(residualCash).toFixed(2);
88+
while((residualCash >= 2) && (this.currentCashCount.t2Count>0)){
89+
residualCash = parseFloat(residualCash).toFixed(2);
90+
residualCash = residualCash - 2;
91+
this.currentCashCount.t2Count = this.currentCashCount.t2Count - 1;
92+
returnCashCount.t2Count++;
93+
}
94+
95+
residualCash = parseFloat(residualCash).toFixed(2);
96+
while((residualCash >= 1) && (this.currentCashCount.t1Count>0)){
97+
residualCash = parseFloat(residualCash).toFixed(2);
98+
residualCash = residualCash - 1;
99+
this.currentCashCount.t1Count = this.currentCashCount.t1Count - 1;
100+
returnCashCount.t1Count++;
101+
}
102+
103+
residualCash = parseFloat(residualCash).toFixed(2);
104+
while((residualCash >= 0.5) && (this.currentCashCount.t50cCount>0)){
105+
residualCash = parseFloat(residualCash).toFixed(2);
106+
residualCash = residualCash - 0.5;
107+
this.currentCashCount.t50cCount = this.currentCashCount.t50cCount - 1;
108+
returnCashCount.t50cCount++;
109+
}
110+
111+
residualCash = parseFloat(residualCash).toFixed(2);
112+
while((residualCash >= 0.25) && (this.currentCashCount.t25cCount>0)){
113+
residualCash = parseFloat(residualCash).toFixed(2);
114+
residualCash = residualCash - 0.25;
115+
this.currentCashCount.t25cCount = this.currentCashCount.t25cCount - 1;
116+
returnCashCount.t25cCount++;
117+
}
118+
119+
residualCash = parseFloat(residualCash).toFixed(2);
120+
while((residualCash >= 0.10) && (this.currentCashCount.t10cCount>0)){
121+
residualCash = parseFloat(residualCash).toFixed(2);
122+
residualCash = residualCash - 0.10;
123+
this.currentCashCount.t10cCount = this.currentCashCount.t10cCount - 1;
124+
returnCashCount.t10cCount++;
125+
}
126+
127+
residualCash = parseFloat(residualCash).toFixed(2);
128+
while((residualCash >= 0.05) && (this.currentCashCount.t5cCount>0)){
129+
residualCash = parseFloat(residualCash).toFixed(2);
130+
residualCash = residualCash - 0.05;
131+
this.currentCashCount.t5cCount = this.currentCashCount.t5cCount - 1;
132+
returnCashCount.t5cCount++;
133+
}
134+
135+
residualCash = parseFloat(residualCash).toFixed(2);
136+
while((residualCash >= 0.01) && (this.currentCashCount.t1cCount>0)){
137+
residualCash = parseFloat(residualCash).toFixed(2);
138+
residualCash = residualCash - 0.01;
139+
this.currentCashCount.t1cCount = this.currentCashCount.t1cCount - 1;
140+
returnCashCount.t1cCount++;
141+
}
142+
143+
residualCash = parseFloat(residualCash).toFixed(2);
144+
if(residualCash != 0 ){
145+
alert("Unfortunately we do not have the necessary amount to be returned as change. Please tender the exact bill amount");
146+
return null;
147+
}
148+
149+
} else {
150+
alert("Insufficient cash provided by customer.");
151+
return null;
152+
}
153+
return returnCashCount;
154+
},
155+
156+
//Retrieve "Cash paid by the customer" cash denominations
157+
calculateChangeDenominations : function(){
158+
159+
var bill = parseFloat(document.getElementById("bill").value);
160+
var t20Count = parseInt(document.getElementById("b20Count").value);
161+
var t10Count = parseInt(document.getElementById("b10Count").value);
162+
var t5Count = parseInt(document.getElementById("b5Count").value);
163+
var t2Count = parseInt(document.getElementById("b2Count").value);
164+
var t1Count = parseInt(document.getElementById("b1Count").value);
165+
var t50cCount = parseInt(document.getElementById("b50cCount").value);
166+
var t25cCount = parseInt(document.getElementById("b25cCount").value);
167+
var t10cCount = parseInt(document.getElementById("b10cCount").value);
168+
var t5cCount = parseInt(document.getElementById("b5cCount").value);
169+
var t1cCount = parseInt(document.getElementById("b1cCount").value);
170+
171+
var returnCashCount = this.calculateChange(bill, new cashDenominationCounts(t20Count, t10Count, t5Count, t2Count,
172+
t1Count, t50cCount, t25cCount, t10cCount, t5cCount, t1cCount));
173+
if(returnCashCount != null) {
174+
this.updateReturnChangeCounts(returnCashCount);
175+
} else{
176+
toggleDivEditState("customerDenom", "enable");
177+
}
178+
},
179+
180+
// Update "To be returned to customer" cash denominations
181+
updateReturnChangeCounts:function(returnCashCount){
182+
183+
document.getElementById("r20Count").value = returnCashCount.t20Count;
184+
document.getElementById("r10Count").value = returnCashCount.t10Count;
185+
document.getElementById("r5Count").value = returnCashCount.t5Count;
186+
document.getElementById("r2Count").value = returnCashCount.t2Count;
187+
document.getElementById("r1Count").value = returnCashCount.t1Count;
188+
document.getElementById("r50cCount").value = returnCashCount.t50cCount;
189+
document.getElementById("r25cCount").value = returnCashCount.t25cCount;
190+
document.getElementById("r10cCount").value = returnCashCount.t10cCount;
191+
document.getElementById("r5cCount").value = returnCashCount.t5cCount;
192+
document.getElementById("r1cCount").value = returnCashCount.t1cCount;
193+
194+
$("#transComplete").show();
195+
highLightReturnChangeBkColor(true);
196+
197+
},
198+
199+
// Update Current Cash Register status on screen with values calculated
200+
updateRegisterDisplay : function() {
201+
202+
document.getElementById("c20Count").value = this.currentCashCount.t20Count;
203+
document.getElementById("c10Count").value = this.currentCashCount.t10Count;
204+
document.getElementById("c5Count").value = this.currentCashCount.t5Count;
205+
document.getElementById("c2Count").value = this.currentCashCount.t2Count;
206+
document.getElementById("c1Count").value = this.currentCashCount.t1Count;
207+
document.getElementById("c50cCount").value = this.currentCashCount.t50cCount;
208+
document.getElementById("c25cCount").value = this.currentCashCount.t25cCount;
209+
document.getElementById("c10cCount").value = this.currentCashCount.t10cCount;
210+
document.getElementById("c5cCount").value = this.currentCashCount.t5cCount;
211+
document.getElementById("c1cCount"). value = this.currentCashCount.t1cCount;
212+
},
213+
214+
//Reset Cash paid by the customer cash denominations and To be returned to customer cash denominations to 0
215+
resetCounts : function () {
216+
217+
document.getElementById("bill").value=0;
218+
document.getElementById("b20Count").value = 0;
219+
document.getElementById("b10Count").value = 0;
220+
document.getElementById("b5Count").value = 0;
221+
document.getElementById("b2Count").value = 0;
222+
document.getElementById("b1Count").value = 0;
223+
document.getElementById("b50cCount").value = 0;
224+
document.getElementById("b25cCount").value = 0;
225+
document.getElementById("b10cCount").value = 0;
226+
document.getElementById("b5cCount").value = 0;
227+
document.getElementById("b1cCount").value = 0;
228+
229+
document.getElementById("r20Count").value = 0;
230+
document.getElementById("r10Count").value = 0;
231+
document.getElementById("r5Count").value = 0;
232+
document.getElementById("r2Count").value = 0;
233+
document.getElementById("r1Count").value = 0;
234+
document.getElementById("r50cCount").value = 0;
235+
document.getElementById("r25cCount").value = 0;
236+
document.getElementById("r10cCount").value = 0;
237+
document.getElementById("r5cCount").value = 0;
238+
document.getElementById("r1cCount").value = 0;
239+
240+
}
241+
242+
};
243+
244+
//Called on Calculate Change
245+
function getChangeDenomCounts(){
246+
if(document.getElementById("bill").value > 0) {
247+
toggleDivEditState("customerDenom", "disable");
248+
cashRegister.calculateChangeDenominations();
249+
} else {
250+
alert("Please enter the total bill amount.");
251+
}
252+
253+
};
254+
255+
// Initialize Cash Register object with values entered on screen
256+
function initializeRegister(){
257+
258+
var c20Count = parseInt(document.getElementById("c20Count").value);
259+
var c10Count = parseInt(document.getElementById("c10Count").value);
260+
var c5Count = parseInt(document.getElementById("c5Count").value);
261+
var c2Count = parseInt(document.getElementById("c2Count").value);
262+
var c1Count = parseInt(document.getElementById("c1Count").value);
263+
var c50cCount = parseInt(document.getElementById("c50cCount").value);
264+
var c25cCount = parseInt(document.getElementById("c25cCount").value);
265+
var c10cCount = parseInt(document.getElementById("c10cCount").value);
266+
var c5cCount = parseInt(document.getElementById("c5cCount").value);
267+
var c1cCount = parseInt(document.getElementById("c1cCount").value);
268+
269+
var cashierEnteredInit = new cashDenominationCounts(c20Count, c10Count, c5Count,
270+
c2Count, c1Count, c50cCount,
271+
c25cCount, c10cCount, c5cCount,
272+
c1cCount);
273+
cashRegister.init(cashierEnteredInit);
274+
disableCashRegisterEdit();
275+
};
276+
277+
//Enable Cash Register cash denominations
278+
function enableCashRegisterEdit() {
279+
toggleCashRegisterEditState("enable");
280+
};
281+
282+
//Disable Cash Register cash denominations
283+
function disableCashRegisterEdit() {
284+
toggleCashRegisterEditState("disable");
285+
};
286+
287+
//Toggle Cash Register cash denominations between enable and disable
288+
function toggleCashRegisterEditState(state) {
289+
// var nodes = document.getElementById("cashRegisterState").getElementsByTagName('*');
290+
// for(var i = 0; i < nodes.length; i++){
291+
// if(state === "disable") {
292+
// nodes[i].disabled = true;
293+
// } else if (state === "enable"){
294+
// nodes[i].disabled = false;
295+
// }
296+
// }
297+
toggleDivEditState("cashRegisterState", state);
298+
//enable the edit button
299+
var node = document.getElementById("editRegister");
300+
node.disabled = false;
301+
};
302+
303+
//Toggle the edit state of elements in a div
304+
function toggleDivEditState(divId, state) {
305+
var nodes = document.getElementById(divId).getElementsByTagName('*');
306+
for(var i = 0; i < nodes.length; i++){
307+
if(state === "disable") {
308+
nodes[i].disabled = true;
309+
} else if (state === "enable"){
310+
nodes[i].disabled = false;
311+
}
312+
}
313+
};
314+
315+
//Update Current Cash Register status on screen
316+
function refreshRegister() {
317+
cashRegister.updateRegisterDisplay();
318+
};
319+
320+
//Set background color
321+
function setBackGroundColor(elementId, color) {
322+
323+
var elem = document.getElementById(elementId);
324+
if(elem) {
325+
elem.style.backgroundColor = color;
326+
}
327+
};
328+
329+
//Set background color of Return to customer panel
330+
function highLightReturnChangeBkColor(state) {
331+
if(state === true) {
332+
setBackGroundColor("divReturnCash", "orange");
333+
}else{
334+
setBackGroundColor("divReturnCash", "white");
335+
}
336+
};
337+
338+
//On clicking Transaction Complete button
339+
//To be returned to customer panel highlighted
340+
//Transaction Complete button hidden
341+
//To be returned to customer cash denominations reset to 0
342+
//Update Cash Register
343+
function transactionComplete() {
344+
highLightReturnChangeBkColor(false);
345+
$("#transComplete").hide();
346+
cashRegister.resetCounts();
347+
refreshRegister();
348+
toggleDivEditState("customerDenom", "enable");
349+
};
350+
351+
//On Window load(Refresh)
352+
//Hide Transaction Complete button
353+
//Set default values for denominations
354+
//Update register with default values
355+
//Disable Update and Cancel buttons for Cash Register
356+
function init() {
357+
$("#transComplete").hide();
358+
alert("The cash register will be initialized with 100 of each denomination. Please edit the register for custom values.");
359+
setDefaultRegisterCounts();
360+
refreshRegister();
361+
cashRegister.resetCounts();
362+
disableCashRegisterEdit();
363+
};
364+
365+
//Cancel button in Current Cash Register status
366+
function cancelRegisterEdit() {
367+
refreshRegister();
368+
disableCashRegisterEdit();
369+
};
370+
371+
//Set default denominations for Cash Register on Window Load (Refresh)
372+
//Function was mainly created to aid in testing of Cash Register Application - custom values can be entered
373+
function setDefaultRegisterCounts(){
374+
cashRegister.init(new cashDenominationCounts(100, 100, 100,
375+
100, 100, 100,
376+
100, 100, 100,
377+
100));
378+
379+
};
380+
381+
//reset the Total bill and the customer paid counts to zero
382+
function reset(){
383+
document.getElementById("bill").value = 0;
384+
cashRegister.resetCounts();
385+
}

0 commit comments

Comments
 (0)