forked from kishanrajput23/Awesome-Project-Collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Budget Tracking App
232 lines (199 loc) · 4.87 KB
/
Budget Tracking App
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
class Budget{
constructor(state){
this.state = state
this.add = this.add()
this.display = this.showDisp()
}
setState(newState) {
this.state = { ...this.state, ...newState }
}
add(){
btn.addEventListener('click', ()=>{
let dVal = desc.value
let aVal = Number(amount.value)
if(dVal === '') {
message('desc', dLabel)
return
}
if(isNaN(aVal) || aVal === ''){
message('amount', aLabel)
return
}
desc.value = ''
amount.value = ''
option.value === 'income' ? this.calculator('income', dVal, aVal) :
this.calculator('expense', dVal, aVal)
})
}
deleteList(){
const listClicked = document.querySelectorAll('.close')
listClicked.forEach(li => {
li.addEventListener('click', () =>{
let listName = li.parentElement.classList.value
let liName = li.parentElement.firstElementChild.textContent
this.removeList(listName, liName)
})
})
}
removeList(name, liDesc){
let sec;
let s;
if(name == 'listIncome'){
sec = this.state.income
s = 'income'
} else if(name == 'listExpenses'){
sec = this.state.expense
s = 'expense'
}
this.r(sec, liDesc, s)
}
r(liState, name, stateName){
if(stateName == 'income'){
let s1 = liState.list.filter(li => li.desc != name )
this.stateCalc(s1, totalCalc(0, s1), stateName )
} else if (stateName == 'expense'){
let s1 = liState.list.filter(li => li.desc != name )
this.stateCalc(s1, totalCalc(0, s1), 'expense')
}
this.balanceCalc()
this.showDisp()
}
calculator(title, desc, amount){
let lists;
title === 'income' ? lists = this.state.income.list : title === 'expense' ?
lists = this.state.expense.list : null
if(lists.length == 0){
let list = []
list.push(this.listCreate(amount, desc, title))
this.stateCalc(list, amount, title)
this.bPerCalc()
} else {
let total = totalCalc(amount, lists);
let newList = lists
let list = this.listCreate(amount, desc, title)
newList.push(list)
// update percent
newList.map(list => {
list.percent = percentCalc(total, list.amount)
})
this.stateCalc(newList, total, title)
this.bPerCalc()
}
}
listCreate(amount, desc, title){
let list;
if(title === 'income'){
list = {
amount: amount,
desc: desc,
percent: percentCalc(amount, amount)
}
} else {
list = {
amount: amount,
color: title === 'expense' ? {
r: numGen(),
g: numGen(),
b: numGen(),
} : null,
barPercent: 0,
desc: desc,
percent: percentCalc(amount, amount)
}
}
return list
}
bPerCalc(){
let inc = this.state.income
let x = this.state.expense
let bar = x.list
bar.map((b, i )=> {
if(x.total > inc.total){
let total = totalCalc(0, bar)
b.barPercent = percentCalc(total, b.amount)
} else if (inc.total > x.total){
b.barPercent = percentCalc(inc.total, b.amount)
}
})
this.stateCalc(bar, x.total,'expense')
this.balanceCalc()
this.showDisp()
}
stateCalc(lists, total, title){
// console.log(lists, total, title)
let state = {
total: total,
list: lists,
length: lists.length
}
if(title === 'income') this.setState({income: state})
if(title === 'expense') this.setState({expense: state})
}
balanceCalc(){
let inTotal = this.state.income.total
let exTotal = this.state.expense.total
let balance = {
balance: inTotal - exTotal,
percent: percentCalc(inTotal, inTotal - exTotal)
}
this.setState({balance: balance})
}
showDisp(){
display(this.state)
this.deleteList()
}
}
const bPerCalc = (xTot, a, iTot) => {
let n
if(xTot > iTot){
n = percentCalc(xTot, a)
} else {
n = percentCalc(iTot, a)
}
return n
}
const numGen = () => {
return Math.floor((Math.random() * 60) + 180 ) + 1
}
const totalCalc = (amount, lists) => {
let t = 0
lists.map(list => {
t += list.amount;
})
return t + amount
}
const percentCalc = (total, amount) => {
let n = Number(amount / total * 100)
return Number(n.toFixed(2))
}
class Income{
constructor(income, expense, balance){
this.income = income
this.expense = expense
this.balance = balance
}
static income(){
return {
total: 0,
list: [],
length: 0
}
}
static expense() {
return {
total: 0,
list: [],
length: 0
}
}
static balance(){
return {
percent: 0,
balance: 0
}
}
static ready(){
return new Income(this.income(), this.expense(), this.balance())
}
}
const start = new Budget(Income.ready())