Tes Syntax Highlighter

 Tes membuat tutorial coding


const fs = require('fs');
const path = './data.json';

// Menambah tugas baru
app.post('/todos', (req, res) => {
    const newTask = req.body;

    // Baca file JSON
    fs.readFile(path, 'utf-8', (err, data) => {
        if (err) throw err;

        // Parse data JSON
        const tasks = JSON.parse(data);

        // Tambahkan tugas baru ke array
        tasks.push(newTask);

        // Tulis kembali ke file JSON
        fs.writeFile(path, JSON.stringify(tasks, null, 2), (err) => {
            if (err) throw err;
            res.status(201).send('Tugas berhasil ditambahkan!');
        });
    });
});
Coding lainnya

class Produk:
    def __init__(self, nama, harga, stok):
        self.nama = nama
        self.harga = harga
        self.stok = stok

    def __str__(self):
        return f"{self.nama} - Harga: {self.harga} IDR, Stok: {self.stok}"

class TokoOnline:
    def __init__(self):
        self.produk_list = []

    def tambah_produk(self, produk):
        self.produk_list.append(produk)
        print(f"Produk {produk.nama} berhasil ditambahkan.")

    def hapus_produk(self, nama_produk):
        for produk in self.produk_list:
            if produk.nama == nama_produk:
                self.produk_list.remove(produk)
                print(f"Produk {nama_produk} berhasil dihapus.")
                return
        print(f"Produk {nama_produk} tidak ditemukan.")

    def lihat_produk(self):
        if not self.produk_list:
            print("Tidak ada produk tersedia.")
        for produk in self.produk_list:
            print(produk)

    def hitung_total_pembelian(self, daftar_belanja):
        total = 0
        for nama_produk, jumlah in daftar_belanja.items():
            for produk in self.produk_list:
                if produk.nama == nama_produk:
                    if jumlah > produk.stok:
                        print(f"Stok {nama_produk} tidak mencukupi.")
                        return
                    total += produk.harga * jumlah
                    break
            else:
                print(f"Produk {nama_produk} tidak ditemukan.")
                return
        return total

# Contoh penggunaan
toko = TokoOnline()

# Menambahkan produk
toko.tambah_produk(Produk("Laptop", 10000000, 5))
toko.tambah_produk(Produk("Smartphone", 5000000, 10))
toko.tambah_produk(Produk("Headphone", 500000, 20))

# Melihat produk
print("\nDaftar Produk:")
toko.lihat_produk()

# Menghitung total pembelian
daftar_belanja = {
    "Laptop": 2,
    "Smartphone": 1
}

total = toko.hitung_total_pembelian(daftar_belanja)
if total is not None:
    print(f"\nTotal Pembelian: {total} IDR")

# Menghapus produk
toko.hapus_produk("Headphone")

# Melihat produk setelah penghapusan
print("\nDaftar Produk setelah penghapusan:")
toko.lihat_produk()

Coding lainnya

productList[] = $product;
        echo "Product {$product->name} added successfully.
"; } public function removeProduct($productName) { foreach ($this->productList as $key => $product) { if ($product->name == $productName) { unset($this->productList[$key]); echo "Product {$productName} removed successfully.
"; return; } } echo "Product {$productName} not found.
"; } public function viewProducts() { if (empty($this->productList)) { echo "No products available.
"; } foreach ($this->productList as $product) { echo $product . "
"; } } public function calculateTotal($shoppingList) { $total = 0; foreach ($shoppingList as $productName => $quantity) { $found = false; foreach ($this->productList as $product) { if ($product->name == $productName) { if ($quantity > $product->stock) { echo "Stock for {$productName} is insufficient.
"; return; } $total += $product->price * $quantity; $found = true; break; } } if (!$found) { echo "Product {$productName} not found.
"; return; } } return $total; } } ?>

Comments

Popular posts from this blog

Elang Learn German

Rizky Learn French