|
| 1 | +package main |
| 2 | + |
| 3 | +import "fmt" |
| 4 | + |
| 5 | +type salaryCalculator interface { |
| 6 | + calculateSalary() float64 |
| 7 | + report() |
| 8 | +} |
| 9 | + |
| 10 | +type PermanentEmployee struct { |
| 11 | + id int |
| 12 | + basicSalary float64 |
| 13 | + commission float64 |
| 14 | +} |
| 15 | + |
| 16 | +type ContractEmployee struct { |
| 17 | + id int |
| 18 | + basicSalary float64 |
| 19 | +} |
| 20 | + |
| 21 | +func (p PermanentEmployee) calculateSalary() float64 { |
| 22 | + return p.basicSalary + (p.commission/100)*p.basicSalary |
| 23 | +} |
| 24 | + |
| 25 | +func (c ContractEmployee) calculateSalary() float64 { |
| 26 | + return c.basicSalary |
| 27 | +} |
| 28 | + |
| 29 | +func (p PermanentEmployee) report() { |
| 30 | + fmt.Printf("Employee ID %d earns USD %f per month \n", p.id, p.calculateSalary()) |
| 31 | +} |
| 32 | + |
| 33 | +func (c ContractEmployee) report() { |
| 34 | + fmt.Printf("Employee ID %d earns USD %f per month \n", c.id, c.calculateSalary()) |
| 35 | +} |
| 36 | + |
| 37 | +func main() { |
| 38 | + p1 := PermanentEmployee{id: 1, basicSalary: 2300, commission: 13} |
| 39 | + p2 := PermanentEmployee{id: 2, basicSalary: 1500, commission: 18} |
| 40 | + p3 := PermanentEmployee{id: 3, basicSalary: 2300, commission: 10} |
| 41 | + c1 := ContractEmployee{id: 4, basicSalary: 500} |
| 42 | + c2 := ContractEmployee{id: 5, basicSalary: 1100} |
| 43 | + c3 := ContractEmployee{id: 6, basicSalary: 700} |
| 44 | + |
| 45 | + employees := []salaryCalculator{p1, p2, p3, c1, c2, c3} |
| 46 | + |
| 47 | + var totalSalary float64 |
| 48 | + |
| 49 | + for _, employee := range employees { |
| 50 | + totalSalary += employee.calculateSalary() |
| 51 | + } |
| 52 | + fmt.Printf("Company total salary is : USD %f", totalSalary) |
| 53 | +} |
0 commit comments