diff --git a/src/index.js b/src/index.js index ac593f4..5bf0224 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,71 @@ // 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.type = "expense"; + this.paid = paid; + } + 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 balance = 0; + for (let entry of this.entries) { + if (entry.type === "income") { + balance += entry.amount; + } else { + balance -= entry.amount; + } + } + return balance; + } + getFormattedEntries() { + const formatted = []; + this.entries.forEach((entry) => { + if (entry.type === "income") { + formatted.push( + `${entry.date} | ${entry.description} | ${entry.amount} €` + ); + } else if (entry.type === "expense") { + formatted.push( + `${entry.date} | ${entry.description} | -${entry.amount} €` + ); + } + }); + return formatted; + } +} \ No newline at end of file