0

How to display all details of employees.? If you add two employees information then why display last employee information.?

https://code.sololearn.com/c7iSVP2Qeroa/?ref=app

12th Mar 2020, 10:19 AM
Shrikant Khurd
Shrikant Khurd - avatar
3 Respuestas
+ 4
Shrikant Khurd You have to store employees information in an arraylist. According to your code previous employees data overided by latest employee data. So in this case you will always get last data. Make a class Employee with attributes id and name. You can do like this :- ---------------------------------- ArrayList<Employee> list = new ArrayList<Employee>(); Employee e = new Employee(); e.setId(1); e.setName("AJ"); list.add(e); Employee e1 = new Employee(); e1.setId(2); e1.setName("Anant"); list.add(e1); ------------------------ To display all data just iterate the list like this:- for(Employee e : list) { System.out.println(e.getId() + " " + e.getName()); }
12th Mar 2020, 10:31 AM
A͢J
A͢J - avatar
0
Please tell me logic and how to display all employees details.?
12th Mar 2020, 10:20 AM
Shrikant Khurd
Shrikant Khurd - avatar
0
create class Employee and array of this Employee objects. // create class Employee class Employee { int id; String name; Employee (int id,String name ){this.id=id; this.name =name; } } //use one Scanner for Sololearn //add Employee[] array declaration class Information { ... //Information in[]=new Information[100]; Employee employee[]=new Employee[5]; // but ArrayList is better Scanner in=new Scanner("1 Anton\n123 1 Michael\n124 2 3"); //System.in public int choice() { System.out.println("Enter your option :"); //ch=s.nextInt(); ch=in.nextInt(); ... //case 1 employee[InfoCount] = new Employee(id,name); //added InfoCount++; ... } while( ch != 3 ); //return choice(); return ch; void putData() { for(int i=0;i<InfoCount;i++) { /* System.out.println("Employee id :"+id); System.out.println("Employee name :"+name); */ System.out.println("Employee id :" +employee[i].id); System.out.println("Employee name :" +employee[i].name);
13th Mar 2020, 4:22 AM
zemiak