Write code for following instructions:
- Define Employee POJO with variables id, name, salary, dept, location.
- Create an ArrayList of Employee type and store following values from arrays.
Id | Name | Salary | Dept | Location |
101 | Amar | 30000 | 20 | Hyderabad |
102 | Hareen | 35000 | 10 | Chennai |
103 | Sathya | 40000 | 20 | Bangalore |
104 | Annie | 45000 | 20 | Hyderabad |
105 | Raji | 42000 | 30 | Pune |
106 | Harsha | 50000 | 10 | Bangalore |
Employee.class:
class Employee { private int id; private String name; private double salary; private int dept; private String location; int getId(){ return this.id; } String getName(){ return this.name; } double getSalary(){ return this.salary; } int getDept(){ return this.dept; } String getLocation(){ return this.location; } void setId(int id){ this.id = id; } void setName(String name){ this.name = name; } void setSalary(double salary){ this.salary = salary; } void setDept(int dept){ this.dept = dept; } void setLocation(String location){ this.location = location; } } |
Main.java:
import java.util.*; class Main { public static void main(String[] args) { int[] ids = {101, 102, 103, 104, 105, 106}; String[] names = {“Amar”, “Hareen”, “Sathya”, “Annie”, “Raji”, “Harsha”}; double[] salaries = {30000, 35000, 40000, 45000, 42000, 50000}; int[] depts = {20, 10, 20, 20, 30, 10}; String[] locations = {“Hyderabad”, “Chennai”, “Bangalore”, “Hyderabad”, “Pune”, “Bangalore”}; List<Employee> list = new ArrayList<Employee>(); for (int i=0 ; i<=ids.length-1 ; i++) { Employee e = new Employee(); e.setId(ids[i]); e.setName(names[i]); e.setSalary(salaries[i]); e.setDept(depts[i]); e.setLocation(locations[i]); list.add(e); } System.out.println(“Details are : “); for(Employee e : list) { System.out.println(e.getId() + ” ,” + e.getName() + ” , ” + e.getSalary() + ” , ” + e.getDept() + ” , ” + e.getLocation()); } } } |
Display details using for loop:
for(int i=0 ; i<=list.size()-1 ; i++) { Employee e = list.get(i); System.out.println(e.getId() + ” ,” + e.getName() + ” , ” + e.getSalary() + ” , ” + e.getDept() + ” , ” + e.getLocation()); } |
Display details in reverse order:
for(int i=list.size()-1 ; i>=0 ; i–) { Employee e = list.get(i); System.out.println(e.getId() + ” ,” + e.getName() + ” , ” + e.getSalary() + ” , ” + e.getDept() + ” , ” + e.getLocation()); } |
Display Employee details whose ID is 103:
boolean found=false; for(Employee e : list) { if(e.getId() == 103) { System.out.println(e.getId() + ” ,” + e.getName() + ” , ” + e.getSalary() + ” , ” + e.getDept() + ” , ” + e.getLocation()); found = true; break; } } if(!found) { System.out.println(“ID 103 doesn’t exist”); } |
Display Employee details belongs to Hyderabad:
int count=0; for(Employee e : list) { if(e.getLocation().equals(“Hyderabad”)) { System.out.println(e.getId() + ” ,” + e.getName() + ” , ” + e.getSalary() + ” , ” + e.getDept() + ” , ” + e.getLocation()); count++; } } if(count==0) { System.out.println(“No employee belongs to Hyderabad”); } |
Display Employee details belongs in department 20 or 30:
int count=0; for(Employee e : list) { if(e.getDept()==20 || e.getDept()==30) { System.out.println(e.getId() + ” ,” + e.getName() + ” , ” + e.getSalary() + ” , ” + e.getDept() + ” , ” + e.getLocation()); count++; } } if(count==0) { System.out.println(“No employee belongs depts 20 or 30”); } |
Display employee details those who not belongs to Hyderabad.
int count=0; for(Employee e : list) { if(!(e.getLocation().equals(“Hyderabad”))) { System.out.println(e.getId() + ” ,” + e.getName() + ” , ” + e.getSalary() + ” , ” + e.getDept() + ” , ” + e.getLocation()); count++; } } if(count==0) { System.out.println(“No employee records founds”); } |
Display details belongs to department 20 and not belongs to Hyderabad:
int count=0; for(Employee e : list) { if(e.getDept()==20 && !(e.getLocation().equals(“Hyderabad”))) { System.out.println(e.getId() + ” ,” + e.getName() + ” , ” + e.getSalary() + ” , ” + e.getDept() + ” , ” + e.getLocation()); count++; } } if(count==0) { System.out.println(“No employee records founds”); } |
Count how many employees working in both Hyderabad and Bangalore locations:
int count=0; for(Employee e : list) { String loc = e.getLocation(); if(loc.equals(“Hyderabad”) || loc.equals(“Bangalore”)) { count++; } } System.out.println(“Count is : ” + count); |
Check the Employee with name “Amar” present or not:
boolean found=false; for(Employee e : list) { if(e.getName().equals(“Amar”)) { System.out.println(“Found with ID : ” + e.getId()); found=true; break; } } if(!found) { System.out.println(“Amar not present”); } |
Display details whose salary greater than 35000:
int count=0; for(Employee e : list) { if(e.getSalary()>35000) { System.out.println(e.getId() + ” , ” + e.getName() + ” , ” + e.getSalary() + ” , ” + e.getDept() + ” , ” + e.getLocation()); count++; } } if(count==0) { System.out.println(“No employee found”); } |
Display details whose salary between 30000 and 40000:
int count=0; for(Employee e : list) { if(e.getSalary()>30000 && e.getSalary()<40000) { System.out.println(e.getId() + ” , ” + e.getName() + ” , ” + e.getSalary() + ” , ” + e.getDept() + ” , ” + e.getLocation()); count++; } } if(count==0) { System.out.println(“No employee found”); } |
Display details whose salary below 40000 and not belongs to Hyderabad:
int count=0; for(Employee e : list) { if(e.getSalary()<40000 && !(e.getLocation().equals(“Hyderabad”))) { System.out.println(e.getId() + ” , ” + e.getName() + ” , ” + e.getSalary() + ” , ” + e.getDept() + ” , ” + e.getLocation()); count++; } } if(count==0) { System.out.println(“No employee found”); } |