diff --git a/src/index.js b/src/index.js index ac593f4..7b7be88 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,75 @@ // Entry -class Entry {} +class Entry { + constructor(date, amount, description) { + this.date = date; + this.amount = amount; + this.description = description; + } + + getFormattedAmount() { + return `${this.amount} €`; + } +} // Income -class Income {} +class Income extends Entry { + constructor(date, amount, description) { + super(date, amount, description); + this.type = "income"; + } +} // Expense -class Expense {} +class Expense extends Entry { + constructor(date, amount, description, paid) { + super(date, amount, description); + this.paid = paid; + this.type = "expense"; + } + + getFormattedAmount() { + return `-${this.amount} €`; + } +} // Budget -class Budget {} +class Budget { + constructor() { + this.entries = []; + } + + addEntry(entry) { + this.entries.push(entry); + } + + getCurrentBalance() { + if (this.entries.length === 0) { + return 0; + } + + let totalIncome = 0; + let totalExpenses = 0; + + for (let i = 0; i < this.entries.length; i++) { + if (this.entries[i].type === "income") { + totalIncome += this.entries[i].amount; + } else if (this.entries[i].type === "expense") { + totalExpenses += this.entries[i].amount; + } + } + + return totalIncome - totalExpenses; + } + + getFormattedEntries() { + const formattedEntries = []; + + this.entries.forEach(entry => { + formattedEntries.push( + `${entry.date} | ${entry.description} | ${entry.getFormattedAmount()}` + ); + }); + + return formattedEntries; + } +}