Assignment

Complete these exercises after the session. All exercises use the tea data.

import { teas } from "../data/teas.js";

Exercise 1: Tea Class with Validation

Build a complete Tea class with validation and a static factory method.

class Tea {
  constructor(name, type, origin, pricePerGram, organic) {
    // Validate:
    // - name must be a non-empty string
    // - type must be one of: "green", "black", "herbal", "oolong", "white"
    // - pricePerGram must be a positive number
    // Store all properties
  }

  priceFor(grams) {
    // Return price for given weight
  }

  describe() {
    // Return "Sencha (green) from Japan - 12.00 DKK/100g [organic]"
    // Only include "[organic]" if the tea is organic
  }

  static fromObject(obj) {
    // Create a Tea from a plain object (like from the data file)
  }
}

// Test validation:
try {
  new Tea("", "green", "Japan", 0.12, true);
} catch (e) {
  console.log(e.message);
} // "Name is required"

try {
  new Tea("Test", "purple", "Japan", 0.12, true);
} catch (e) {
  console.log(e.message);
} // "Invalid type: purple"

// Test factory method:
const teaInstances = teas.map(Tea.fromObject);
console.log(teaInstances.length); // 20
console.log(teaInstances[0].describe());
// "Sencha (green) from Japan - 12.00 DKK/100g [organic]"
console.log(teaInstances[1].describe());
// "Earl Grey (black) from India - 8.00 DKK/100g"

Exercise 2: Order System

Build OrderItem and Order classes that work together.


Exercise 3: Inventory Manager ⭐

Build an Inventory class that tracks stock for multiple teas.


Exercise 4: Customer with History ⭐

Build a Customer class that tracks order history and spending.


Exercise 5: Full Tea Shop System ⭐⭐

Build a TeaShop class that orchestrates all the other classes together.


Exercise 6: Inheritance ⭐⭐

Build specialized classes using inheritance. This exercise is optional.


Submission

Create a folder week4-assignment/ with:

  • exercise1.js - Tea class with validation

  • exercise2.js - Order system

  • exercise3.js - Inventory manager

  • exercise4.js - Customer with history

  • exercise5.js - Full tea shop system

  • exercise6.js - Inheritance (optional)

Each file should be runnable with node exerciseN.js.

Last updated