Golang program to find which Employees should get bonus


In corporate office there may be some cases in which you have a list of employees and you need to provide them extra bonus based on the work or experience or a particular property. In this article we are going to explore a way to calculate bonus using performance based calculations and tenure based calculations.

Method 1: Performance Based Calculations

Performance is the common base to determine the employee’s bonus, in this method we are going to calculate the bonus depending on the performance of the employee.

Algorithm

  • Create the Employee struct, which has the values Name, Salary, and Projects.

  • Implement the calculateBonus function, which accepts an Employee and returns a float64 reflecting the bonus.

  • Set the bonus variable to 0.0 within the calculateBonus function.

  • Use conditional statements to calculate the employee's incentive based on the number of tasks completed.

  • Create a slice of Employee structs in the main function and populate it with employee data. Using a for loop, iterate across the workers slice.

  • Call the calculateBonus function to determine the bonus for each employee.

  • Using the fmt.Printf function, print the name of the employee and the bonus.

Example

The example given below demonstrates a way to calculate the bonus given to an employee based on their performance.

package main
import "fmt"

type Employee struct {
   Name     string
   Salary   float64
   Projects int
}

func calculateBonus(employee Employee) float64 {
   bonus := 0.0

   if employee.Projects > 10 {
      bonus = employee.Salary * 0.2
   } else if employee.Projects > 5 {
      bonus = employee.Salary * 0.1
   } else {
      bonus = employee.Salary * 0.05
   }

   return bonus
}

func main() {
   employees := []Employee{
      {Name: "Akhil Sharma", Salary: 5000.0, Projects: 12},
      {Name: "Akshay kumar", Salary: 6000.0, Projects: 7},
      {Name: "Sahil verma", Salary: 4500.0, Projects: 3},
   }

   fmt.Println("Performance-based Bonuses:")
   for _, employee := range employees {
      bonus := calculateBonus(employee)
      fmt.Printf("%s: $%.2f\n", employee.Name, bonus)
   }
}

Output

Performance-based Bonuses: 
Akhil Sharma: $1000.00 
Akshay kumar: $600.00 
Sahil verma: $225.00

Method 2: Tenure Based Bonus

Another common base to determine the employees bonus is considering an employee's tenure, in this method we are going to calculate the bonus depending on the tenure of the employee.

Algorithm

  • Create the Employee struct, which will have the fields Name, Salary, and HireDate to represent the data of an employee.

  • Implement the calculateBonus function, which has two input parameters: an Employee and the current time.

  • Subtract the HireDate from the current time to determine the employee's tenure.

  • By dividing the total number of hours by 24 and 365.25, you may convert the tenure to years.

  • In the main() function, create an array of Employee objects and fill it with the appropriate data.

  • Take the time right now in UTC and put it in the now variable. Repeat the employees array iteratively.

  • Use the calculateBonus function to calculate the bonus based on tenure and the current time for each employee. Along with the computed bonus, print the employee's name.

Example

The below example calculates the number of complete years in the tenure of an employee and then based on the tenure provides a bonus to the employees.

package main

import (
   "fmt"
   "time"
)

type Employee struct {
   Name     string
   Salary   float64
   HireDate time.Time
}

func calculateBonus(employee Employee, now time.Time) float64 {
   bonus := 0.0

   tenure := now.Sub(employee.HireDate)

   // Calculate the number of complete years in the tenure
   years := int(tenure.Hours() / 24 / 365.25)

   if years > 5 {
      bonus = employee.Salary * 0.1
   } else if years > 2 {
      bonus = employee.Salary * 0.05
   } else {
      bonus = 0.0
   }

   return bonus
}

func main() {
   employees := []Employee{
      {Name: "Akhil Sharma", Salary: 5000.0, HireDate: time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC)},
      {Name: "Akshay Kuamr", Salary: 6000.0, HireDate: time.Date(2019, time.February, 15, 0, 0, 0, 0, time.UTC)},
      {Name: "Sahil Verma", Salary: 4500.0, HireDate: time.Date(2022, time.March, 20, 0, 0, 0, 0, time.UTC)},
   }

   now := time.Now().UTC()

   fmt.Println("Tenure-based Bonuses:")
   for _, employee := range employees {
      bonus := calculateBonus(employee, now)
      fmt.Printf("%s: $%.2f\n", employee.Name, bonus)
   }
}

Output

Tenure-based Bonuses:
Akhil Sharma: $500.00
Akshay Kuamr: $300.00
Sahil Verma: $0.00

Conclusion

In this article, we have explored two distinct ways for determining employee incentives. The first approach involves the performance based bonus and the other approach involves the bonus given to an employee based on the tenure.

Updated on: 06-Jul-2023

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements