Golang program to find total bonus paid to a particular employee


There can be some situations in which you have a list of employees and you need to find out the bonus paid to a particular employee. Go language allows you to perform the task easily, in this article we are going to find out the total bonus paid to a particular employee using a fixed bonus amount as well as calculating individual bonus percentage.

Algorithm

  • Create a struct Employee with the attributes Name and BonusPercent.

  • Calculate the total bonus using the function calculateTotalBonus, float64: Set the variable totalBonus to 0.0.

  • Iterate through the employees array, one by one: Check to see if emp.Name is the same as employeeName.

  • If a match is discovered: Divide emp.BonusPercent by 100 and multiply by a predetermined bonus base value to calculate the bonus.

  • Add the bonus to the variable totalBonus.

  • Return the totalBonus value. In the primary function: Create an array of Employee called employees, each of which has a name and a bonus %.

  • Set employeeName to the name of the employee for whom you wish to calculate the bonus.

  • With employees and employeeName as parameters, use the calculateTotalBonus function and save the result in the totalBonus variable.

  • Using fmt, print the employee's name and the calculated totalBonus.Printf.

Example 1: Fixed Bonus Base amount

This method involves a fixed base bonus amount, and the bonus of a particular employee is calculated by multiplying the base amount with the employee tenure.

package main

import "fmt"

type Employee struct {
   Name string
   BonusPercent float64
}

func calculateTotalBonus(employees []Employee, employeeName string) float64 {
   totalBonus := 0.0
   for _, emp := range employees {
      if emp.Name == employeeName {
         bonus := emp.BonusPercent / 100.0 * 1000.0 // Assuming a fixed bonus base amount of $1000
         totalBonus += bonus
      }
   }
   return totalBonus
}

func main() {
   employees := []Employee{
      {Name: "John", BonusPercent: 10},
      {Name: "Alice", BonusPercent: 15},
      {Name: "Bob", BonusPercent: 20},
   }

   employeeName := "Alice"
   totalBonus := calculateTotalBonus(employees, employeeName)
   fmt.Printf("Total bonus paid to %s: $%.2f\n", employeeName, totalBonus)
}

Output

Total bonus paid to Alice: $150.00

Example 2: Based on Indivisual Bonus percentage

This method involves an individual bonus percentage, and the bonus of a particular employee is calculated by multiplying the employee salary with the bonus percentage.

package main
import "fmt"

type Employee struct {
   Name string
   BonusPercent float64
}

func calculateIndividualBonus(employee Employee, baseAmount float64) float64 {
   bonus := employee.BonusPercent / 100.0 * baseAmount
   return bonus
}

func main() {
   employee := Employee{
      Name:         "John",
      BonusPercent: 15,
   }
   baseAmount := 1000.0

   individualBonus := calculateIndividualBonus(employee, baseAmount)
   fmt.Printf("Total bonus paid to %s: $%.2f\n", employee.Name, individualBonus)
}

Output

Total bonus paid to John: $150.00

Conclusion

In this article we have used the Go language to explore three distinct approaches for calculating the total bonus paid to a specific employee. Here we have explored two different approaches, one involves the use of the base bonus and other involves the bonus percentage of a particular employee.

Updated on: 06-Jul-2023

51 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements