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
|
import Category from "./class-category.js";
import Transaction from "./class-transaction.js";
import User from "./class-user.js";
class BudgetApp {
#title = "Budget App";
#categoryId = 1;
#categories = [];
#transactionId = 1;
#incomes = [];
#expenses = [];
#userId = 1;
#user = {};
constructor(title, username) {
this.#title = title;
this.#user = new User(this.#userId++, username);
}
set title(string) {
this.#title = string;
}
get title() {
return this.#title;
}
set categories(array) {
this.#categories = array;
}
get categories() {
return this.#categories;
}
set incomes(array) {
this.#incomes = array;
}
get incomes() {
return this.#incomes;
}
set expenses(array) {
this.#expenses = array;
}
get expenses() {
return this.#expenses;
}
set user(username) {
this.#user = new User(this.#userId++, username);
}
get user() {
return this.#user;
}
remove(id, from) {
const index = from.findIndex((object) => object.id === id);
from.splice(index, 1);
}
addCategory(name) {
this.#categories.push(new Category(this.#categoryId++, name));
}
renameCategory(id, newName) {
const index = this.categories.findIndex((object) => object.id === id);
this.categories[index].name = newName;
}
addTransaction(transaction) {
const array =
transaction.type === "income" ? this.#incomes : this.#expenses;
array.push(
new Transaction(
this.#transactionId++,
transaction.date,
transaction.name,
transaction.type,
transaction.category,
transaction.amount
)
);
}
editTransaction(transaction) {
const array = transaction.type === "income" ? this.incomes : this.expenses;
const index = array.findIndex((object) => {
return object.id === Number(transaction.id);
});
if (index !== -1) {
array[index] = new Transaction(...Object.values(transaction));
} else {
const oldArray = array === this.incomes ? this.expenses : this.incomes;
array.push(new Transaction(...Object.values(transaction)));
this.remove(transaction.id, oldArray);
}
}
getOrderedTransactions(order) {
const transactions = [...this.expenses, ...this.incomes];
switch (order) {
case "newest":
transactions.sort((a, b) => b.date - a.date);
break;
case "oldest":
transactions.sort((a, b) => a.date - b.date);
break;
default:
break;
}
return transactions;
}
total(transaction) {
const array = transaction === "expense" ? this.#expenses : this.#incomes;
let total = 0;
array.forEach((item) => {
total += item.amount;
});
return total;
}
updateUserBudget() {
this.user.budget.spent = this.total("expense");
this.user.budget.profit = this.total("income");
}
reset() {
this.#categoryId = 1;
this.#transactionId = 1;
this.#categories = [];
this.#incomes = [];
this.#expenses = [];
this.updateUserBudget();
}
}
export default BudgetApp;
|