-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
213 lines (171 loc) · 5.82 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
'use strict';
const account1 = {
owner: '이찬희',
username: 'chanhee',
movements: [20000, 45000, -40000, 3000, -6500, -13000, 70000, 1300],
interestRate: 1.2,
pin: 1111,
};
const account2 = {
owner: '김찰리',
username: 'charlie',
movements: [50000, 3400, -15000, -79000, -32100, -1000, 85000, -3000],
interestRate: 1.5,
pin: 2222,
};
const account3 = {
owner: '스티브',
username: 'steve3333',
movements: [21000, -20000, 34000, -3000, -20000, 55000, 4400, -4600],
interestRate: 0.7,
pin: 3333,
};
const account4 = {
owner: '김사라',
username: 'sara2222',
movements: [43000, 10000, -7000, 50000, -9800],
interestRate: 1,
pin: 4444,
};
const accounts = [account1, account2, account3, account4];
/////////////////////////////////////////////////
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 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');
/////////////////////////////////////////////////
const displayMovements = function (movements, sort = false) {
containerMovements.innerHTML = '';
const movs = sort ? movements.slice().sort((a, b) => a - b) : movements;
movs.forEach(function (mov, i) {
const type = mov > 0 ? 'deposit' : 'withdrawal';
const html = `
<div class="movements__row">
<div class="movements__type movements__type--${type}">${i + 1} ${
type === 'deposit' ? '입금' : '출금'
}</div>
<div class="movements__value">${mov}₩</div>
</div>
`;
containerMovements.insertAdjacentHTML('afterbegin', html);
});
};
const calcDisplayBalance = function (acc) {
acc.balance = acc.movements.reduce((acc, mov) => acc + mov, 0);
labelBalance.textContent = `${acc.balance}₩`;
};
const calcDisplaySummary = function (acc) {
const incomes = acc.movements
.filter(mov => mov > 0)
.reduce((acc, mov) => acc + mov, 0);
labelSumIn.textContent = `${incomes}₩`;
const out = acc.movements
.filter(mov => mov < 0)
.reduce((acc, mov) => acc + mov, 0);
labelSumOut.textContent = `${Math.abs(out)}₩`;
const interest = acc.movements
.filter(mov => mov > 0)
.map(deposit => (deposit * acc.interestRate) / 100)
.filter((int, i, arr) => {
return int >= 1;
})
.reduce((acc, int) => acc + int, 0);
labelSumInterest.textContent = `${interest}₩`;
};
const updateUI = function (acc) {
// 내역 보기
displayMovements(acc.movements);
// 잔고 보기
calcDisplayBalance(acc);
// 요약화면 보기
calcDisplaySummary(acc);
};
// 이벤트 핸들러
let currentAccount;
btnLogin.addEventListener('click', function (e) {
// 자동 들어가기 방지
e.preventDefault();
currentAccount = accounts.find(
acc => acc.username === inputLoginUsername.value
);
console.log(currentAccount);
if (currentAccount?.pin === Number(inputLoginPin.value)) {
labelWelcome.textContent = `환영합니다, ${currentAccount.owner}님`;
containerApp.style.opacity = 100;
// 인풋필드 비우기
inputLoginUsername.value = inputLoginPin.value = '';
inputLoginPin.blur();
// UI업데이트
updateUI(currentAccount);
}
});
btnTransfer.addEventListener('click', function (e) {
e.preventDefault();
const amount = Number(inputTransferAmount.value);
const receiverAcc = accounts.find(
acc => acc.username === inputTransferTo.value
);
inputTransferAmount.value = inputTransferTo.value = '';
if (
amount > 0 &&
receiverAcc &&
currentAccount.balance >= amount &&
receiverAcc?.username !== currentAccount.username
) {
// 송금
currentAccount.movements.push(-amount);
receiverAcc.movements.push(amount);
// UI업데이트
updateUI(currentAccount);
}
});
btnLoan.addEventListener('click', function (e) {
e.preventDefault();
const amount = Number(inputLoanAmount.value);
if (amount > 0 && currentAccount.movements.some(mov => mov >= amount * 0.1)) {
// 내역 추가
currentAccount.movements.push(amount);
// UI업데이트
updateUI(currentAccount);
}
inputLoanAmount.value = '';
});
btnClose.addEventListener('click', function (e) {
e.preventDefault();
if (
inputCloseUsername.value === currentAccount.username &&
Number(inputClosePin.value) === currentAccount.pin
) {
const index = accounts.findIndex(
acc => acc.username === currentAccount.username
);
console.log(index);
// 계정 삭제
accounts.splice(index, 1);
// UI감추
containerApp.style.opacity = 0;
}
inputCloseUsername.value = inputClosePin.value = '';
});
let sorted = false;
btnSort.addEventListener('click', function (e) {
e.preventDefault();
displayMovements(currentAccount.movements, !sorted);
sorted = !sorted;
});