Take an id form the user and check if that employee with given employee id is existing or not, if it exists show Employee information .
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
// i am trying to display the perticular employee details but am unable to display anything // is there any way i can do that
class Employee{
int empid;
String name;
float basicSal;
public Employee(int empid, String name, float basicSal) {
super();
this.empid = empid;
this.name = name;
this.basicSal = basicSal;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getBasicSal() {
return basicSal;
}
public void setBasicSal(float basicSal) {
this.basicSal = basicSal;
}
}
public class EmployeeExecuter {
public static void main(String[] args) {
ArrayList<Employee> empl = new ArrayList<>(5);
Employee ob2 = new Employee(2,"bat",3000);
Employee ob3 = new Employee(3,"cat",4000);
Employee ob4 = new Employee(4,"dog",5000);
Employee ob5 = new Employee(5,"rat",6000);
empl.add(ob1);
empl.add(ob2);
empl.add(ob3);
empl.add(ob4);
empl.add(ob5);
Scanner sc = new Scanner(System.in);
System.out.println("Enter empid :");
int id = sc.nextInt();
if(empl.contains(id)) {
Iterator<Employee> sit = empl.iterator();
while(sit.hasNext()) {
Employee emps= sit.next();
System.out.println(emps.getEmpid() " " emps.getName() " " emps.getBasicSal());
}
}
}
}
CodePudding user response:
empl.contains(id)
always return false b/c id is not an Employee object.
How to fix:
Change if(empl.contains(id))
into
for (Employee e : empl) {
if(e.getEmpid() == id) {
System.out.println(emps.getEmpid() " " emps.getName() " " emps.getBasicSal());
}
}
CodePudding user response:
Try this
for(Employee emp:empl){
if(emp.getEmpid()==id){
System.out.println(emp.getEmpid() " " emp.getName() " " emp.getBasicSal());
break;
}
}
CodePudding user response:
Sounds like a student assignment. How do you solve this problem as a person?
Assume you have a sheet of paper with employee information listed on it this list of employees is not sorted. Each line on the sheet contains the info for a single employee. The natural approach is to check each record from the top of the list to the bottom and check for the given ID.
It works the same for a program. You iterate through the list looking for the ID.
In pseudocode...
function findEmploye (List employees, ID id)
for each Employee E in List of Employee
if E.id = ID then return E
end-for
return null
end-function
In Java, using for-each loop...
public Employee findEmployee(List<Employee> employees, int id) {
for (Employee e : employees) {
if (e.getId() == id) {
return e;
}
}
return null;
}
In Java, using the Stream API...
public Employee findEmployee(List<Employee> employees, int id) {
return employees.filter(e -> e.getId() == id).findAny().get();
}
CodePudding user response:
doesnt have a getters of the employeeID
CodePudding user response:
public class EmployeeExecuter {
public static void main(String[] args) {
ArrayList<Employee> empl = new ArrayList<>(5);
Employee ob2 = new Employee(2,"bat",3000);
Employee ob3 = new Employee(3,"cat",4000);
Employee ob4 = new Employee(4,"dog",5000);
Employee ob5 = new Employee(5,"rat",6000);
empl.add(ob1);
empl.add(ob2);
empl.add(ob3);
empl.add(ob4);
empl.add(ob5);
Scanner sc = new Scanner(System.in);
System.out.println("Enter empid :");
int id = sc.nextInt();
empl.filter(e -> e.getId() == id).findAny().get().forEach(System.out::println);
} }
easy and fast way