-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
237 lines (236 loc) · 7.65 KB
/
app.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
// setting new instance of atm.
let atm = new Atm();
// Atm object.
function Atm(){
// using let so accounts is not accessesable outside the atm.
let accounts = [];
// need to have the accounts on memory
let data = JSON.parse(localStorage.getItem("accounts"));
// this is to deal with an error that can be caused if accounts if null
if(data != null){
data.forEach(account=>{
accounts.push(account);
});
}
// currentAccount has to be empty when the page is loaded.
this.currentAccount = {};
// need later on for validating if account already exist.
this.account = {exist:false, index:null};
// need new account
this.createAccount= function(){
let newPin = document.getElementById("newAccPin").value
this.account.exist = false;
this.validate(newPin);
// pin cannot be empty
if(accounts.length == 0){
this.currentAccount = new Account(newPin);
this.updateLocalStor();
this.balanceTab();
}
// every pin must be unique
else if(this.account.exist){
this.displayMgs("msgBox","Invalid Pin")
}
else if(this.account.exist == false){
this.currentAccount = new Account(newPin);
this.clearMsg("msgBox");
this.updateLocalStor();
this.balanceTab();
}
}
//loging in
this.getAccount= function(){
this.account.exist = false;
let pin = document.getElementById("pinPut").value
this.validate(pin);
if(isNaN(parseInt(pin))){
atm.displayMgs("msgBox", "Invalid Pin!");
}
else if(this.account.exist){
let location = this.account.index;
//work around, because JSON.strigify strips all the methods, so this adds them back.
this.currentAccount = new Account(accounts[location].pin, accounts[location].balance);
this.clearMsg("msgBox");
this.balanceTab()
}
else if(this.account.exist == false){
this.displayMgs("msgBox", "Invalid Pin");
}
}
// need to check if account already exist, will also track the location if it exists.
this.validate = function(pin){
for(let [index, account] of accounts.entries()){
if(pin == account.pin){
atm.account.exist = true;
atm.account.index = index;
return;
}
}
}
// need to store changes in localStorage
this.updateLocalStor = function() {
accounts = accounts.filter( el => el.pin !== atm.currentAccount.pin );
accounts.push(this.currentAccount);
vlue = JSON.stringify(accounts);
localStorage.removeItem("accounts");
localStorage.setItem("accounts", vlue);
}
// hide different elements to have tab like feature with bottons.
this.hide = function() {
let arg = Array.from(arguments);
arg.forEach(arguemnt =>{
document.getElementById(arguemnt).style = "display:none";
});
}
// show different elements to have tab like feature with bottons.
this.show = function(){
let arg = Array.from(arguments);
arg.forEach(arguemnt =>{
document.getElementById(arguemnt).style = "display:block";
});
}
this.clearInput= function(id) {
document.getElementById(id).value = "";
};
this.displayMgs = function(id, msg) {
document.getElementById(id).innerHTML = msg;
}
this.balanceTab= function(){
this.hide("login","depositWithdrawal");
this.show("balanceDis","navbar");
this.displayMgs("title", "")
this.clearMsg("msgBox")
this.displayMgs("balanceAmount", `$${this.currentAccount.getBalance()}`)
}
// to clear message when chaning tabs.
this.clearMsg = function(id) {
document.getElementById(id).innerHTML = "";
};
}
//class that is a template for each account
class Account{
constructor(pin, balance = 0){
this.pin = pin;
this.balance = balance;
}
getBalance(){
atm.clearInput("value");
return this.balance;
}
deposit(){
let amount = document.getElementById("value").value;
document.getElementById("msgBox").style = "color:red";
amount = parseInt(amount);
if(isNaN(amount)){
atm.displayMgs("msgBox", "Enter amount")
console.log(isNaN(amount));
}
else if(amount < 0 ){
atm.displayMgs("msgBox", "Amout cannot be less then 0")
}
else if( isNaN(amount)===false) {
this.balance += amount;
atm.displayMgs("msgBox", `Your new balance is: $${this.balance}`)
document.getElementById("msgBox").style = "color:#1e2749"
atm.updateLocalStor();
atm.clearInput("value");
}
}
withdrawal(){
let amount = document.getElementById("value").value;
document.getElementById("msgBox").style = "color:red"
amount = parseInt(amount)
if(isNaN(amount)){
atm.displayMgs("msgBox", "Enter amount")
}
else if(amount < 0 ){
atm.displayMgs("msgBox", "Amout cannot be less then 0")
}
else if(amount > this.balance){
atm.displayMgs("msgBox", "insufficent funds for this transaction")
}
else{
this.balance -= parseInt(amount);
atm.displayMgs("msgBox", `Your new balance is: $${this.balance}`)
document.getElementById("msgBox").style = "color:#1e2749"
atm.updateLocalStor();
atm.clearInput("value");
}
}
changePin(){
atm.account.exist = false;
let pin = document.getElementById("value").value;
document.getElementById("msgBox").style = "color:red"
atm.validate(pin)
if(isNaN(parseInt(pin))){
atm.displayMgs("msgBox", "Pin can only be numbers");
}
else if(atm.account.exist){
atm.displayMgs("msgBox", "invalid Pin!");
}
else if(atm.account.exist == false){
atm.updateLocalStor();
this.pin = pin
atm.displayMgs("msgBox", `Your New Pin is: ${this.pin}`);
document.getElementById("msgBox").style = "color:#1e2749"
atm.updateLocalStor();
}
}
}
//change current tab
function ActiveTab(){
let buttonGroup = document.getElementById("accountMenu");
let buttons = buttonGroup.getElementsByClassName("btn");
//current tab is shaded in different color.
for(i = 0; i < buttons.length; i++){
buttons[i].addEventListener("click", function(){
let current = buttonGroup.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
let currentTab = this.value;
switch(currentTab){
case '0':{
atm.balanceTab();
break;
}
case '1':{
// sets the tab for deposit.
atm.hide("login","balanceDis","withdrawalButton", "changePinButton");
atm.show("depositButton","accountMenu","depositWithdrawal");
atm.displayMgs("title", "Deposit");
atm.clearMsg("msgBox");
atm.clearInput("value");
atm.displayMgs("label", "Enter Amount");
break;
}
case '2':{
// sets the tab for withdrawal.
atm.hide("login", "balanceDis","changePinButton","depositButton");
atm.show("depositWithdrawal","accountMenu", "withdrawalButton");
atm.show("msgBox");
atm.displayMgs("title", "Withdrawal");
atm.clearMsg("msgBox");
atm.clearInput("value");
atm.displayMgs("label", "Enter Amount");
break;
}
case '3':{
// sets the tab for changing pin.
atm.hide("login","balanceDis","depositButton","withdrawalButton");
atm.show("depositWithdrawal","accountMenu", "changePinButton");
atm.displayMgs("title", "Change Pin");
atm.clearMsg("msgBox");
atm.clearInput("value");
atm.displayMgs("label", "Enter New Pin");
break;
}
case '4':{
// so that evrything resets and will take back to login page.
location.reload()
break;
}
}
});
}
}
ActiveTab();