Skip to content

Commit 5c13112

Browse files
Create polymorphism.go
1 parent 46695d8 commit 5c13112

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Diff for: oops/polymorphism.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
import "fmt"
3+
type income interface {
4+
calculate() int
5+
source() string
6+
}
7+
type fixedbilling struct {
8+
projectname string
9+
biddedamount int
10+
}
11+
type timeandmaterial struct {
12+
projectname string
13+
noofhours int
14+
hourlyrate int
15+
}
16+
func (fb fixedbilling) calculate() int {
17+
return fb.biddedamount
18+
}
19+
func (fb fixedbilling) source() string {
20+
return fb.projectname
21+
}
22+
func (tm timeandmaterial) calculate() int {
23+
return tm.noofhours * tm.hourlyrate
24+
}
25+
func (tm timeandmaterial) source() string {
26+
return tm.projectname
27+
}
28+
func calculatenetincome(ic [] income) {
29+
var netincome int = 0
30+
for _,income := range ic {
31+
fmt.Printf("%s = %d" , income.source() , income.calculate())
32+
netincome = income.calculate()
33+
}
34+
fmt.Printf("\n netincome of organization = %d",netincome)
35+
}
36+
func main() {
37+
project1 := fixedbilling{projectname:"project1",biddedamount:8000}
38+
project2 :=fixedbilling{projectname:"project2",biddedamount:5000}
39+
project3 := timeandmaterial{projectname:"project3",noofhours:9,hourlyrate:1000}
40+
totalgenerated := [] income{project1,project2,project3}
41+
calculatenetincome(totalgenerated)
42+
43+
}

0 commit comments

Comments
 (0)