-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
421 lines (380 loc) · 13.6 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
'use strict';
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// BANKIST APP
// Elements
const labelWelcome = document.querySelector('.welcome');
const labelDate = document.querySelector('.date');
const labelBalance = document.querySelector('.balance__value');
const labelSumIn = document.querySelector('.summary__value--in');
const labelSumOut = document.querySelector('.summary__value--out');
const labelSumInterest = document.querySelector('.summary__value--interest');
const labelTimer = document.querySelector('.timer');
const containerApp = document.querySelector('.app');
const containerMovements = document.querySelector('.movements');
const btnLogin = document.querySelector('.login__btn');
const btnTransfer = document.querySelector('.form__btn--transfer');
const btnLoan = document.querySelector('.form__btn--loan');
const btnClose = document.querySelector('.form__btn--close');
const btnSort = document.querySelector('.btn--sort');
const inputLoginUsername = document.querySelector('.login__input--user');
const inputLoginPin = document.querySelector('.login__input--pin');
const inputTransferTo = document.querySelector('.form__input--to');
const inputTransferAmount = document.querySelector('.form__input--amount');
const inputLoanAmount = document.querySelector('.form__input--loan-amount');
const inputCloseUsername = document.querySelector('.form__input--user');
const inputClosePin = document.querySelector('.form__input--pin');
// -----------------------------------------------------
// Finds out initials of a name to match the entered username with the actual username's initials
const initials = function (str)
{
let result = "";
const arr = str.split(' ');
arr.forEach(function(ele)
{
result+=ele[0];
})
return result.toLowerCase();
}
// compares the entered username and pin with the actual ones
const valid = function (obj,pin,name)
{
if(obj.pin===pin && initials(obj.owner) === name)
return true;
}
// Finds out addition of elements
const add = function (array)
{
let s=0;
array.forEach(function(item)
{
s+=item;
})
return (s.toFixed(2));
}
// tracks movements
const track = function (money,i)
{
let html,str;
if(money<0)
str = "withdrawal";
else
str = "deposit";
html = `<div class="movements__row">
<div class="movements__type movements__type--${str}">${i+1} ${str}</div>
<div class="movements__value">${money.toFixed(2)}€</div>
</div>`;
containerMovements.innerHTML=html+containerMovements.innerHTML;
}
// Calculates total deposited money
const inMoney = function (arr)
{
let sum = 0;
arr.forEach(function(ele)
{
if(ele > 0)
sum=sum+ele;
})
return sum.toFixed(2) ;
}
// Calculates total withdrawal money
const outMoney = function (arr)
{
let sum = 0;
arr.forEach(function(ele)
{
if(ele < 0)
sum=sum+ele;
})
return Math.abs(sum).toFixed(2) ;
}
let logoutTimer;
const timerFun = function()
{
const tick = function()
{
let mins = Math.floor(time/60);
let sec = time%60;
labelTimer.textContent=`${mins}`.padStart(2,0)+":"+`${sec}`.padStart(2,0);
if(time===0)
{
clearInterval(logoutTimer);
containerApp.style.opacity="0%";
labelWelcome.textContent="Login to get started";
}
time--;
}
let time=300;
logoutTimer = setInterval(tick,1000);
}
let usernameValue,pinValue,activeAcc,currentBalance;
// Callback function for login button
const login = function()
{
usernameValue = inputLoginUsername.value;
pinValue = Number(inputLoginPin.value);
for(const item of accounts)
{
if(valid(item,pinValue,usernameValue))
{
clearInterval(logoutTimer);
timerFun();
activeAcc=item; //stores present account object
containerMovements.innerHTML="";
if(containerApp.classList.contains("hidden"))
containerApp.classList.remove("hidden");
containerApp.style.opacity = "100%";
labelWelcome.textContent = "Welcome, "+item.owner[0].toUpperCase()+item.owner.slice(1,item.owner.indexOf(' '));
currentBalance = add(item.movements);
labelBalance.textContent=`${currentBalance}€`;
labelSumIn.textContent=`${inMoney(item.movements)}€`;
labelSumOut.textContent=`${outMoney(item.movements)}€`;
interestCalculation();
item.movements.forEach(track);
const locale = navigator.language;
const date = new Date();
const options = {
hour : "numeric",
minute : "numeric",
day : "numeric",
month : "long",
year : "numeric",
weekday : "long"
}
labelDate.textContent=new Intl.DateTimeFormat(locale,options).format(date);
break;
}
}
inputLoginUsername.value="";
inputLoginPin.value="";
inputLoginPin.blur();
};
// actions after login button is clicked
btnLogin.addEventListener("click",login);
document.addEventListener("keydown",function(e)
{
if(e.key==="Enter")
login();
});
// Transfer callback function
const transferMoney = function()
{
const transferToValue = inputTransferTo.value;
const transferAmountValue = Number(inputTransferAmount.value);
for(const item of accounts)
{
if(transferToValue===initials(item.owner) && transferToValue!==initials(activeAcc.owner) && transferAmountValue<=currentBalance && transferAmountValue>0)
{
console.log(activeAcc) ;
console.log(item);
containerMovements.innerHTML=""; //resets the innerhtml of the movements
activeAcc.movements.push(0-transferAmountValue); //pushes the withdrawal in the active-acc movements array
item.movements.push(transferAmountValue); //pushes the deposited account in the recipent
currentBalance=add(activeAcc.movements);
labelBalance.textContent=`${currentBalance}€`;
activeAcc.movements.forEach(track);
labelSumOut.textContent = `${outMoney(activeAcc.movements)}€`;
inputTransferAmount.value = "";
inputTransferTo.value = "";
inputTransferAmount.blur();
//Resetting the timer
clearInterval(logoutTimer);
timerFun();
}
}
};
// transfer operations
btnTransfer.addEventListener("click", transferMoney);
document.addEventListener("keydown",function(e)
{
if(e.key==="Enter")
transferMoney();
});
// Loan callback function
const takeLoan = function()
{
const loanAmt = Number(Math.floor(inputLoanAmount.value));
clearInterval(logoutTimer);
timerFun();
// Loan should be sanctioned only if there is at least one deposited amount which is more than 10% of the loan amount && loan amount > 0
if((activeAcc.movements).some(item=>item>=(0.1*loanAmt)) && loanAmt>0)
{
activeAcc.movements.push(loanAmt);
const giveLoan = setTimeout(function()
{
containerMovements.innerHTML="";
activeAcc.movements.forEach(track);
labelBalance.textContent=`${add(activeAcc.movements)}€`;
labelSumIn.textContent = `${inMoney(activeAcc.movements)}€`;
interestCalculation();
inputLoanAmount.value="";
inputLoanAmount.blur();
},3000);
}
}
// Loan operations
btnLoan.addEventListener("click",takeLoan);
document.addEventListener("keydown",function(e)
{
if(e.key==="Enter")
{
takeLoan();
e.preventDefault();
}
});
// close callback function
const closeAcc = function()
{
clearInterval(logoutTimer);
timerFun();
if(initials(activeAcc.owner)===inputCloseUsername.value && activeAcc.pin===Number(inputClosePin.value))
{
containerApp.style.opacity="0%";
accounts.splice(accounts.indexOf(activeAcc),1);
inputCloseUsername.value="";
inputClosePin.value = "";
inputClosePin.blur();
labelWelcome.textContent="Login to get started";
inputLoginUsername.value="";
inputLoginPin="";
}
}
// close operations
btnClose.addEventListener("click",closeAcc);
document.addEventListener("keydown",function(e)
{
if(e.key==="Enter")
{
closeAcc();
e.preventDefault();
}
});
//interest calculation
const interestCalculation = function()
{
// filter positive values from the movements array => map a new array multiplied with the rate => filter values greater than or equal to 1 => reduce the array to the sum
const sumOfInterest = (activeAcc.movements).filter(item=>(item>0)).map(item => (item*activeAcc.interestRate/100)).filter(item=>(item>=1)).reduce((acc,item)=>acc+item,0);
labelSumInterest.textContent=`${sumOfInterest.toFixed(2)}€`;
}
// Sort transactions
let sorted = false;
btnSort.addEventListener("click",function()
{
containerMovements.innerHTML="";
if(!(sorted))
{
activeAcc.movements.slice().sort((a,b)=>a-b).forEach(track);
}
else
{
activeAcc.movements.forEach(track);
}
sorted=!(sorted);
})
//----------------------------------------------
// local database
// Local banking system variables
const btnNext = document.querySelector(".btn-next");
const btnSubmit = document.querySelector(".btn-submit");
const inputDatabaseName = document.querySelector(".database-username");
const inputDatabasePin = document.querySelector(".database-pin");
const inputDatabaseDeposit = document.querySelector(".database-deposit");
const inputDatabaseInterest = document.querySelector(".database-interest")
const localLogo = document.querySelector(".local-logo");
const databaseBox = document.querySelector(".local-database-box");
const databasePage = document.querySelector(".local-database-page");
const brand = document.querySelector(".brand");
const navLogin = document.querySelector(".navbar-login");
const detailsActual = document.querySelector(".details-actual");
const detailsBox = document.querySelector(".user-details-box");
const lightBulb = document.querySelector(".bi-lightbulb");
// Database input reveal
localLogo.addEventListener("mouseover",function()
{
localLogo.style.opacity="0%";
setTimeout(() => {localLogo.classList.add("hidden")},300);
databaseBox.style.opacity="100%";
brand.style.opacity="100%";
})
////////////////////////////
const accounts=[],usernamesArray=[],pinArray=[];
//////////////////////////
btnNext.addEventListener("click",function()
{
if(inputDatabaseName.value!=="" && +inputDatabasePin.value>=1000 && +inputDatabasePin.value<=9999)
{
accounts.push(new Object(
{
owner : inputDatabaseName.value,
pin : Number(inputDatabasePin.value),
movements : [Number(inputDatabaseDeposit.value)],
interestRate : +(inputDatabaseInterest.value)
}));
usernamesArray.push(initials(inputDatabaseName.value));
pinArray.push(inputDatabasePin.value);
inputDatabaseName.value="";
inputDatabasePin.value="";
inputDatabaseDeposit.value="";
inputDatabaseInterest.value="";
}
else
{
inputDatabaseName.value="";
inputDatabasePin.value="";
inputDatabaseDeposit.value="";
inputDatabaseInterest.value="";
}
})
btnSubmit.addEventListener("click",function()
{
if(inputDatabaseName.value!=="" && +inputDatabasePin.value>=1000 && +inputDatabasePin.value<=9999)
{
accounts.push(new Object(
{
owner : inputDatabaseName.value,
pin : Number(inputDatabasePin.value),
movements : [Number(inputDatabaseDeposit.value)],
interestRate : +(inputDatabaseInterest.value)
}));
usernamesArray.push(initials(inputDatabaseName.value));
pinArray.push(inputDatabasePin.value)
navLogin.classList.remove("hidden");
databasePage.classList.add("hidden");
lightBulb.classList.remove("hidden");
}
else
{
inputDatabaseName.value="";
inputDatabasePin.value="";
inputDatabaseInterest.value="";
inputDatabaseDeposit.value="";
}
})
// Lightbulb
const userInfo = (arr1,arr2) =>
{ detailsActual.innerHTML="";
arr1.forEach((item,i) =>
{
detailsActual.innerHTML+=`<div class="username-and-pin"><p class="username">${item}</p><p class="pin">${arr2[i]}</p></div>`
})
}
let glow = false;
lightBulb.addEventListener("click",function()
{
// Make it glow
if(!glow)
{
lightBulb.innerHTML='<path d="M2 6a6 6 0 1 1 10.174 4.31c-.203.196-.359.4-.453.619l-.762 1.769A.5.5 0 0 1 10.5 13h-5a.5.5 0 0 1-.46-.302l-.761-1.77a1.964 1.964 0 0 0-.453-.618A5.984 5.984 0 0 1 2 6zm3 8.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1l-.224.447a1 1 0 0 1-.894.553H6.618a1 1 0 0 1-.894-.553L5.5 15a.5.5 0 0 1-.5-.5z"/>';
lightBulb.style.fill="#E9B824";
detailsBox.style.opacity="100%";
userInfo(usernamesArray,pinArray);
}
// Does not make it glow
else
{
lightBulb.innerHTML='<path d="M2 6a6 6 0 1 1 10.174 4.31c-.203.196-.359.4-.453.619l-.762 1.769A.5.5 0 0 1 10.5 13a.5.5 0 0 1 0 1 .5.5 0 0 1 0 1l-.224.447a1 1 0 0 1-.894.553H6.618a1 1 0 0 1-.894-.553L5.5 15a.5.5 0 0 1 0-1 .5.5 0 0 1 0-1 .5.5 0 0 1-.46-.302l-.761-1.77a1.964 1.964 0 0 0-.453-.618A5.984 5.984 0 0 1 2 6zm6-5a5 5 0 0 0-3.479 8.592c.263.254.514.564.676.941L5.83 12h4.342l.632-1.467c.162-.377.413-.687.676-.941A5 5 0 0 0 8 1z"/>';
lightBulb.style.fill="black";
detailsBox.style.opacity="0%";
}
glow=!glow;
})