Skip to content

Instantly share code, notes, and snippets.

@HenriqueSilverio
Created December 23, 2025 16:13
Show Gist options
  • Select an option

  • Save HenriqueSilverio/0155c9c03dbcab2e3036da8cbc02bf22 to your computer and use it in GitHub Desktop.

Select an option

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.
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())
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