Skip to content

2.1 Making a Employee Database

Farrukh Ahmad edited this page Dec 15, 2015 · 1 revision

Here I will create a simple employee database and find out the highest paid employee from the database.

  1. Create a Employee Class
  2. Add many employee to a LinkedList to track the insertion order
  3. Search max salary and store that employee object

Solution:

import java.util.LinkedList;
import java.util.List;


public class EmployeeDatabase {
public static void main(String args[])
{
    List<Employee> employees = new LinkedList<Employee>();

    Employee farrukh = new Employee(1, "Farr", 32, 600000);
    Employee arbi = new Employee(2, "arbi", 30, 600000);
    Employee munzir = new Employee(3, "munzir", 3.5f, 800000);
    Employee humaira = new Employee(4, "humaira", 0.9f, 700000);

    employees.add(farrukh);
    employees.add(arbi);
    employees.add(munzir);
    employees.add(humaira);
    
    double max = employees.get(1).getSalary();
    Employee richEmployee = employees.get(1);
   
    for(Employee employee: employees)
    {
        if(max < employee.getSalary())
        {
            max = employee.getSalary();
            richEmployee = employee; 
        }
    }
    System.out.println(richEmployee);
}
}

class Employee
{
private long id;
private String name;
private float age;
private double salary;

public Employee() {
}

public Employee(long id, String name, float age, double salary) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.salary = salary;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public float getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public double getSalary() {
    return salary;
}

public void setSalary(double salary) {
    this.salary = salary;
}

@Override
public int hashCode() {
    int hash = 7;
    hash = 41 * hash + (int) (this.id ^ (this.id >>> 32));
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Employee other = (Employee) obj;
    if (Double.doubleToLongBits(this.salary) != Double.doubleToLongBits(other.salary)) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "Employee{" + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + '}';
}

}

Clone this wiki locally