first commit
This commit is contained in:
commit
bec02173b8
19
README.md
Normal file
19
README.md
Normal file
@ -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 .
|
||||||
|
```
|
||||||
25
main.go
Normal file
25
main.go
Normal file
@ -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))
|
||||||
|
}
|
||||||
|
}
|
||||||
26
operator/operator.go
Normal file
26
operator/operator.go
Normal file
@ -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
|
||||||
|
}
|
||||||
41
operator/operator_test.go
Normal file
41
operator/operator_test.go
Normal file
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
19
validasi/validasi.go
Normal file
19
validasi/validasi.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
38
validasi/validasi_test.go
Normal file
38
validasi/validasi_test.go
Normal file
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user