Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions oops/polymorphism.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//polymorphism
package main
import "fmt"
type income interface {
calculate() int
source() string
}
type fixedbilling struct {
projectname string
biddedamount int
}
type timeandmaterial struct {
projectname string
noofhours int
hourlyrate int
}
func (fb fixedbilling) calculate() int {
return fb.biddedamount
}
func (fb fixedbilling) source() string {
return fb.projectname
}
func (tm timeandmaterial) calculate() int {
return tm.noofhours * tm.hourlyrate
}
func (tm timeandmaterial) source() string {
return tm.projectname
}
func calculatenetincome(ic [] income) {
var netincome int = 0
for _,income := range ic {
fmt.Printf("%s = %d" , income.source() , income.calculate())
netincome = income.calculate()
}
fmt.Printf("\n netincome of organization = %d",netincome)
}
func main() {
project1 := fixedbilling{projectname:"project1",biddedamount:8000}
project2 :=fixedbilling{projectname:"project2",biddedamount:5000}
project3 := timeandmaterial{projectname:"project3",noofhours:9,hourlyrate:1000}
totalgenerated := [] income{project1,project2,project3}
calculatenetincome(totalgenerated)

}