create a shop in typescript in typescript

index.ts
// Define a Product class
class Product {
    constructor(public id: number, public name: string, public price: number) {}
}

// Define a Shop class
class Shop {
    private products: Product[];

    constructor() {
        this.products = [];
    }

    addProduct(product: Product): void {
        this.products.push(product);
    }

    displayProducts(): void {
        this.products.forEach(product => {
            console.log(`Product ID: ${product.id}, Name: ${product.name}, Price: ${product.price}`);
        });
    }
}

// Create an instance of Shop
const myShop = new Shop();

// Add products to the shop
const product1 = new Product(1, "Shirt", 20);
const product2 = new Product(2, "Shoes", 50);

myShop.addProduct(product1);
myShop.addProduct(product2);

// Display products in the shop
myShop.displayProducts();
828 chars
37 lines

related categories

gistlibby LogSnag