Created
December 23, 2025 16:13
-
-
Save HenriqueSilverio/0155c9c03dbcab2e3036da8cbc02bf22 to your computer and use it in GitHub Desktop.
Adapted from Adele Goldberg’s book, Smalltalk-80: The Language and its Implementation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import FinancialHistory from './FinancialHistory.mjs' | |
| const personalBudget = new FinancialHistory() | |
| console.log(personalBudget.cashOnHand()) | |
| personalBudget.receive(2200, 'salary') | |
| console.log(personalBudget.cashOnHand()) | |
| personalBudget.spend(200, 'books') | |
| console.log(personalBudget.cashOnHand()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default class FinancialHistory { | |
| #cashOnHand | |
| #incomes = new Map() | |
| #expenditures = new Map() | |
| constructor(initialBalance = 0) { | |
| this.#cashOnHand = initialBalance | |
| } | |
| cashOnHand() { | |
| return this.#cashOnHand | |
| } | |
| receive(amount, source) { | |
| this.#incomes.set(source, this.totalReceivedFrom(source) + amount) | |
| this.#cashOnHand += amount | |
| } | |
| spend(amount, reason) { | |
| this.#expenditures.set(reason, this.totalSpentFor(reason) + amount) | |
| this.#cashOnHand -= amount | |
| } | |
| totalReceivedFrom(source) { | |
| if (this.#incomes.has(source)) { | |
| return this.#incomes.get(source) | |
| } | |
| return 0 | |
| } | |
| totalSpentFor(reason) { | |
| if (this.#expenditures.has(reason)) { | |
| return this.#expenditures.get(reason) | |
| } | |
| return 0 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment