Go course

Projects and Final ReviewProject: Warehouse Inventory

Go

Display

Theme

Projects and Final Review · Test exercise

Project: Warehouse Inventory

The second project combines structs, maps, slices (and the slices package), errors, and interfaces, this time an interface you implement yourself.

A camping gear store keeps its goods in a warehouse. The starter code provides:

  • The Item{SKU, Nama, Kategori, Harga, Stok} struct (name, category, price, stock).
  • The Gudang (warehouse) struct, which stores items in a map[string]Item keyed by SKU, along with the GudangBaru constructor.
  • The sentinel errors ErrSKUKosong, ErrSKUDuplikat, ErrTidakDitemukan, ErrStokKurang (empty SKU, duplicate SKU, not found, insufficient stock).
  • The Diskon (discount) interface with a single method: Terapkan(total int) int.

Remember: a value you get from a map is a copy. To change an item's Stok, take the item out of the map, change the field, then store it back.

Task

  1. func (g *Gudang) Tambah(it Item) error

    • Empty SKU → ErrSKUKosong. SKU already exists → an error that wraps ErrSKUDuplikat.
    • Otherwise, store the item in the map.
  2. func (g *Gudang) Jual(sku string, jumlah int) error

    • The SKU doesn't exist → an error that wraps ErrTidakDitemukan.
    • jumlah is more than the stock → an error that wraps ErrStokKurang (the stock doesn't change).
    • Otherwise, reduce the stock.
  3. func (g *Gudang) StokMenipis(batas int) []string returns the SKUs of every item with Stok <= batas, sorted in ascending order (use slices.Sort, because map order is random).

  4. Implement the Diskon interface on the two types that are already declared:

    • DiskonPersen{Persen int} → total - total*Persen/100.
    • DiskonNominal{Potongan int} → total - Potongan, but never below 0.
  5. func (g *Gudang) HitungTotal(keranjang map[string]int, d Diskon) (int, error)

    • keranjang (the cart) maps SKU → quantity. The total is the sum of Harga × jumlah for each line.
    • A SKU that doesn't exist → an error that wraps ErrTidakDitemukan.
    • If d isn't nil, return d.Terapkan(total); if it is nil, return the total as is.
    • This function does not change the stock.
main.go
package main

import (
	"errors"
	"fmt"
	"slices"
)

type Item struct {
	SKU      string
	Nama     string
	Kategori string
	Harga    int
	Stok     int
}

var (
	ErrSKUKosong      = errors.New("SKU tidak boleh kosong")
	ErrSKUDuplikat    = errors.New("SKU sudah terdaftar")
	ErrTidakDitemukan = errors.New("barang tidak ditemukan")
	ErrStokKurang     = errors.New("stok tidak cukup")
)

type Gudang struct {
	items map[string]Item
}

func GudangBaru() *Gudang {
	return &Gudang{items: make(map[string]Item)}
}

type Diskon interface {
	Terapkan(total int) int
}

type DiskonPersen struct {
	Persen int
}

type DiskonNominal struct {
	Potongan int
}

// TODO: lengkapi method Terapkan untuk DiskonPersen dan DiskonNominal.
func (d DiskonPersen) Terapkan(total int) int {
	return total
}

func (d DiskonNominal) Terapkan(total int) int {
	return total
}

func (g *Gudang) Tambah(it Item) error {
	// TODO
	return nil
}

func (g *Gudang) Jual(sku string, jumlah int) error {
	// TODO
	return nil
}

func (g *Gudang) StokMenipis(batas int) []string {
	// TODO: kumpulkan SKU lalu urutkan dengan slices.Sort
	var hasil []string
	slices.Sort(hasil)
	return hasil
}

func (g *Gudang) HitungTotal(keranjang map[string]int, d Diskon) (int, error) {
	// TODO
	return 0, nil
}

func main() {
	g := GudangBaru()
	g.Tambah(Item{"TND-02", "Tenda 2 orang", "tenda", 650000, 4})
	g.Tambah(Item{"SLP-01", "Sleeping bag", "tidur", 280000, 2})
	g.Tambah(Item{"LMP-05", "Lampu LED", "penerangan", 85000, 15})

	if err := g.Jual("SLP-01", 5); err != nil {
		fmt.Println("Gagal jual:", err)
	}
	g.Jual("TND-02", 2)
	fmt.Println("Stok menipis:", g.StokMenipis(3))

	total, err := g.HitungTotal(map[string]int{"LMP-05": 2, "TND-02": 1}, DiskonPersen{10})
	if err != nil {
		fmt.Println("Gagal hitung:", err)
	}
	fmt.Println("Total setelah diskon:", total)
}

Output

Go runtime not loaded yet

Press Run to see the output, or Submit to check your code.

Ctrl/Cmd + Enter: Run · Ctrl/Cmd + Shift + Enter: Submit · Ctrl/Cmd + S: tidy the code (gofmt)