From bec02173b8f6e8c5e38cc8209df607ca63824be3 Mon Sep 17 00:00:00 2001 From: CalvinLiu123 <41094560+CalvinLiu123@users.noreply.github.com> Date: Mon, 10 Nov 2025 12:27:02 +0700 Subject: [PATCH] first commit --- README.md | 19 ++++++++++++++++++ go.mod | 3 +++ main.go | 25 ++++++++++++++++++++++++ operator/operator.go | 26 +++++++++++++++++++++++++ operator/operator_test.go | 41 +++++++++++++++++++++++++++++++++++++++ validasi/validasi.go | 19 ++++++++++++++++++ validasi/validasi_test.go | 38 ++++++++++++++++++++++++++++++++++++ 7 files changed, 171 insertions(+) create mode 100644 README.md create mode 100644 go.mod create mode 100644 main.go create mode 100644 operator/operator.go create mode 100644 operator/operator_test.go create mode 100644 validasi/validasi.go create mode 100644 validasi/validasi_test.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..37773ab --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Week_12 - Operator dan Validasi (Golang) + +Project sederhana berisi dua paket: + +- `operator` : kalkulator aritmatika sederhana (Add, Sub, Mul, Div). +- `validasi` : validasi usia produktif (18-60 tahun). + +Cara menjalankan test: + +```powershell +cd "D:\Semester 3\Pratikum Back End\Week_12" +go test ./... +``` + +Contoh menjalankan program demo: + +```powershell +go run . +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e0071c0 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module example.com/week12 + +go 1.20 diff --git a/main.go b/main.go new file mode 100644 index 0000000..d737aa0 --- /dev/null +++ b/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + + "example.com/week12/operator" + "example.com/week12/validasi" +) + +func main() { + fmt.Println("Contoh penggunaan paket operator:") + a, b := 10, 3 + fmt.Printf("%d + %d = %d\n", a, b, operator.Add(a,b)) + fmt.Printf("%d - %d = %d\n", a, b, operator.Sub(a,b)) + fmt.Printf("%d * %d = %d\n", a, b, operator.Mul(a,b)) + if v, err := operator.Div(a,b); err == nil { + fmt.Printf("%d / %d = %v\n", a, b, v) + } + + fmt.Println("\nContoh penggunaan paket validasi:") + ages := []int{17, 18, 30, 61} + for _, age := range ages { + fmt.Printf("Usia %d: produktif=%v\n", age, validasi.IsUsiaProduktif(age)) + } +} diff --git a/operator/operator.go b/operator/operator.go new file mode 100644 index 0000000..5e533ca --- /dev/null +++ b/operator/operator.go @@ -0,0 +1,26 @@ +package operator + +import "fmt" + +// Add returns the sum of a and b. +func Add(a, b int) int { + return a + b +} + +// Sub returns the difference a - b. +func Sub(a, b int) int { + return a - b +} + +// Mul returns the product of a and b. +func Mul(a, b int) int { + return a * b +} + +// Div returns the division a / b as float64. Returns an error on division by zero. +func Div(a, b int) (float64, error) { + if b == 0 { + return 0, fmt.Errorf("divide by zero") + } + return float64(a) / float64(b), nil +} diff --git a/operator/operator_test.go b/operator/operator_test.go new file mode 100644 index 0000000..8df3845 --- /dev/null +++ b/operator/operator_test.go @@ -0,0 +1,41 @@ +package operator + +import "testing" + +func TestAdd(t *testing.T) { + got := Add(2, 3) + if got != 5 { + t.Fatalf("Add(2,3) = %d; want 5", got) + } +} + +func TestSub(t *testing.T) { + got := Sub(5, 3) + if got != 2 { + t.Fatalf("Sub(5,3) = %d; want 2", got) + } +} + +func TestMul(t *testing.T) { + got := Mul(4, 3) + if got != 12 { + t.Fatalf("Mul(4,3) = %d; want 12", got) + } +} + +func TestDiv(t *testing.T) { + got, err := Div(7, 2) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if got != 3.5 { + t.Fatalf("Div(7,2) = %v; want 3.5", got) + } +} + +func TestDivByZero(t *testing.T) { + _, err := Div(1, 0) + if err == nil { + t.Fatalf("expected error when dividing by zero") + } +} diff --git a/validasi/validasi.go b/validasi/validasi.go new file mode 100644 index 0000000..32bfd14 --- /dev/null +++ b/validasi/validasi.go @@ -0,0 +1,19 @@ +package validasi + +import "fmt" + +// IsUsiaProduktif returns true if age is between 18 and 60 inclusive. +func IsUsiaProduktif(age int) bool { + return age >= 18 && age <= 60 +} + +// ValidateUsia returns nil when usia produktif, otherwise an error explaining why. +func ValidateUsia(age int) error { + if age < 0 { + return fmt.Errorf("umur tidak boleh negatif") + } + if IsUsiaProduktif(age) { + return nil + } + return fmt.Errorf("umur %d bukan usia produktif (18-60)", age) +} diff --git a/validasi/validasi_test.go b/validasi/validasi_test.go new file mode 100644 index 0000000..ac2f2e7 --- /dev/null +++ b/validasi/validasi_test.go @@ -0,0 +1,38 @@ +package validasi + +import "testing" + +func TestIsUsiaProduktif(t *testing.T) { + cases := []struct{ + age int + want bool + }{ + {17, false}, + {18, true}, + {30, true}, + {60, true}, + {61, false}, + } + + for _, c := range cases { + got := IsUsiaProduktif(c.age) + if got != c.want { + t.Fatalf("IsUsiaProduktif(%d) = %v; want %v", c.age, got, c.want) + } + } +} + +func TestValidateUsia(t *testing.T) { + if err := ValidateUsia(-1); err == nil { + t.Fatalf("expected error for negative age") + } + if err := ValidateUsia(18); err != nil { + t.Fatalf("unexpected error for age 18: %v", err) + } + if err := ValidateUsia(60); err != nil { + t.Fatalf("unexpected error for age 60: %v", err) + } + if err := ValidateUsia(61); err == nil { + t.Fatalf("expected error for age 61") + } +}