-
Notifications
You must be signed in to change notification settings - Fork 0
/
bankaccount2.js
149 lines (116 loc) · 4.16 KB
/
bankaccount2.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
var currentSavingsAccount = 0;
var currentCheckingAccount = 0;
var savingsWithdrawBtn = document.getElementsByClassName('savingWithdrawBtn')[0];
var savingsDepositBtn = document.getElementsByClassName('savingDepositBtn')[0];
var checkingWithdrawBtn = document.getElementsByClassName('checkingWithdrawBtn')[0];
var checkingDepositBtn = document.getElementsByClassName('checkingDepositBtn')[0];
var inputSavings = document.getElementsByClassName('savingFigure')[0];
var inputChecking = document.getElementsByClassName('checkingFigure')[0];
var savingBalance = document.getElementsByClassName('balanceSavings')[0];
var checkingBalance = document.getElementsByClassName('balanceChecking')[0];
var colorBackChange1 = document.getElementsByClassName('savings')[0];
var colorBackChange2 = document.getElementsByClassName('checking')[0];
var bgRedColor = function(){
colorBackChange1.style.backgroundColor = 'rgba(206,4,34, 0.8)';
colorBackChange2.style.backgroundColor = 'rgba(206,4,34, 0.8)';
}
var bgDefaultColor = function(){
colorBackChange1.style.backgroundColor = 'rgba(255,255,255,0.8)';
colorBackChange2.style.backgroundColor = 'rgba(255,255,255,0.8)';
}
var savingWithdraw = function(){
var amountWithdraw = userInputSavings();
currentSavingsAccount = currentSavingsAccount - amountWithdraw;
savingBalanceUpdate(currentSavingsAccount);
}
var savingDeposit = function(){
var amountDeposit = userInputSavings();
currentSavingsAccount = currentSavingsAccount + amountDeposit;
savingBalanceUpdate(currentSavingsAccount);
}
var checkingWithdraw = function(){
var amountWithdraw = userInputChecking();
currentCheckingAccount = currentCheckingAccount - amountWithdraw;
checkingBalanceUpdate(currentCheckingAccount);
}
var checkingDeposit = function(){
var amountDeposit = userInputChecking();
currentCheckingAccount += amountDeposit;
checkingBalanceUpdate(currentCheckingAccount);
}
var savingToCheck = function(){
var amountWithdraw = userInputSavings();
if(amountWithdraw <= currentCheckingAccount){
currentCheckingAccount = currentCheckingAccount - amountWithdraw;
checkingTextUpdate(currentCheckingAccount);
}else{
currentSavingsAccount = 0;
}
}
var checkToSaving = function(){
var amountWithdraw = userInputChecking();
if(amountWithdraw <= currentSavingsAccount){
currentSavingsAccount = currentSavingsAccount - amountWithdraw;
savingTextUpdate(currentSavingsAccount);
}else{
currentCheckingAccount = 0;
}
}
var savingBalanceUpdate = function(amount){
var newSavingBalance = amount;
if(newSavingBalance>=0){
savingTextUpdate(newSavingBalance);
bgDefaultColor();
}else if(currentSavingsAccount<=0 && currentCheckingAccount<=0){
notEnoughFundsReset();
bgRedColor();
}else if(newSavingBalance<0 && currentCheckingAccount>0){
savingToCheck();
bgDefaultColor();
}
}
var checkingBalanceUpdate = function(amount){
var newCheckBalance = amount;
if(newCheckBalance>=0){
checkingTextUpdate(newCheckBalance);
bgDefaultColor();
}else if(currentSavingsAccount<=0 && currentCheckingAccount<=0){
notEnoughFundsReset();
bgRedColor();
}else if(newCheckBalance<0 && currentSavingsAccount>0){
checkToSaving();
bgDefaultColor();
}
}
var userInputSavings = function(){
var userInputValue = Number(inputSavings.value);
return userInputValue;
}
var userInputChecking = function(){
var userInputValue = Number(inputChecking.value);
return userInputValue;
}
var notEnoughFundsReset = function(){
savingBalance.textContent = 'Inadequate Funds';
checkingBalance.textContent = 'Inadequate Funds'
currentSavingsAccount = 0;
currentCheckingAccount = 0;
}
var checkingTextUpdate = function(newBalance){
checkingBalance.textContent = '$' + newBalance.toFixed(2);
}
var savingTextUpdate = function(newBalance){
savingBalance.textContent = '$' + newBalance.toFixed(2);
}
savingsWithdrawBtn.addEventListener('click', function(){
savingWithdraw();
});
savingsDepositBtn.addEventListener('click', function(){
savingDeposit();
});
checkingWithdrawBtn.addEventListener('click', function(){
checkingWithdraw();
});
checkingDepositBtn.addEventListener('click', function(){
checkingDeposit();
});