
Write a Java program to accept an array list of Employee objects and search for perticular Employee based on Id Number (like ID,Name&Address)

import java.util.Scanner;
import java.util.ArrayList;
class Employee{
int no;
String name;
String address;
Employee(int no,String name,String address){
this.no=no;
this.name=name;
this.address=address;
}
public int getNo(){
return no;
}
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public static void main(String[] args){
ArrayList<Employee> list=new ArrayList<Employee>();
Scanner input=new Scanner(System.in);
for(int i=0;i<4;i++){
System.out.println();
System.out.println("-----Employee "+(i+1)+"-----");
System.out.println("Enter id: ");
int no=input.nextInt();
System.out.println("Enter name: ");
String name=input.next();
System.out.println("Enter address: ");
String address=input.next();
list.add(new Employee(no,name,address));
}
System.out.println(" ");
System.out.print("Enter Employee Id to search: ");
int id = input.nextInt();
for(Employee s : list){
if(id == s.getNo())
{
System.out.print(s.getNo()+" "+s.getName()+" " +s.getAddress());
}
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.