Created
February 2, 2022 09:57
-
-
Save ihsanbudiman/11dc79d749804fed9c807aab50b270b2 to your computer and use it in GitHub Desktop.
openclosed in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
type BangunDatar interface { | |
Luas() float64 | |
} | |
type Persegi struct { | |
Sisi float64 | |
} | |
func (p Persegi) Luas() float64 { | |
return p.Sisi * p.Sisi | |
} | |
type Segitiga struct { | |
Alas float64 | |
Tinggi float64 | |
} | |
func (s Segitiga) Luas() float64 { | |
return (s.Alas * s.Tinggi) / 2 | |
} | |
type Lingkaran struct { | |
Radius float64 | |
} | |
func (l Lingkaran) Luas() float64 { | |
return math.Pi * l.Radius * l.Radius | |
} | |
type Kalkulator struct{} | |
func (k Kalkulator) TotalLuas(daftarBenda ...BangunDatar) float64 { | |
totalLuas := 0.0 | |
for _, benda := range daftarBenda { | |
totalLuas += benda.Luas() | |
} | |
return totalLuas | |
} | |
func main() { | |
p := Persegi{4} | |
s := Segitiga{3, 4} | |
l := Lingkaran{3} | |
k := Kalkulator{} | |
fmt.Println(k.TotalLuas(p, s, l)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment