Week 11 - OOP concepts & LLMs

Object oriented programming

Classes and objects

Encapsulation

Code style: clean code

LLMs

Tokenization

Inference

Tools

RAGs

Using LLMs in code

Practice

Assignment

Core program

Content

Let’s get practical

Exercise 1: Refactor to Classes

You are given the following code that manages a bank account using plain objects and functions:

function createAccount(owner, balance) {
  return { owner, balance, transactions: [] };
}

function deposit(account, amount) {
  if (amount <= 0) throw new Error("Deposit amount must be positive");
  account.balance += amount;
  account.transactions.push({ type: "deposit", amount, date: new Date().toISOString() });
}

function withdraw(account, amount) {
  if (amount <= 0) throw new Error("Withdrawal amount must be positive");
  if (amount > account.balance) throw new Error("Insufficient funds");
  account.balance -= amount;
  account.transactions.push({ type: "withdrawal", amount, date: new Date().toISOString() });
}

function getStatement(account) {
  return account.transactions
    .map(t => `${t.date} | ${t.type} | ${t.amount} | Balance: ${account.balance}`)
    .join("\\n");
}

// Usage
const acc = createAccount("Alice", 100);
deposit(acc, 50);
withdraw(acc, 30);
console.log(getStatement(acc));

Task

  1. Convert the code above into a BankAccount class.
  2. owner and balance should be set via the constructor. transactions is initialized as an empty array.
  3. deposit(amount), withdraw(amount), and getStatement() become methods.
  4. Make balance and transactions private using # private fields. Expose balance through a getter.
  5. The usage code should work like this:
const alice = new BankAccount("Alice", 100);
const bob = new BankAccount("Bob", 50);
alice.deposit(200);
bob.withdraw(20);
console.log(alice.balance);        // 300
console.log(alice.getStatement());
console.log(bob.balance);          // 30
console.log(bob.getStatement());

Exercise 2: Task manager

Build a small task management system using two classes.

Class Task

Class TaskManager

Exercise 3:

Exercise 4:


The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0 *https://hackyourfuture.net/*

CC BY-NC-SA 4.0 Icons

Built with ❤️ by the HackYourFuture community · Thank you, contributors

Found a mistake or have a suggestion? Let us know in the feedback form.