How to print the details of employess more than 1?

+1 vote
asked Aug 15, 2016 in JAVA by sudiptajava (520 points)
package com;

        import java.sql.Date;

        public class Employee {
            private String designation;
            private Date dob;
            private int employeeID;
            private String name;
            private float salary;
            public String getDesignation() {
                return designation;
            }
            public void setDesignation(String designation) {
                this.designation = designation;
            }
            public Date getDob() {
                return dob;
            }
            public void setDob(Date dob) {
                this.dob = dob;
            }
            public int getEmployeeID() {
                return employeeID;
            }
            public void setEmployeeID(int employeeID) {
                this.employeeID = employeeID;
            }
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public float getSalary() {
                return salary;
            }
            public void setSalary(float salary) {
                this.salary = salary;
            }

        }

//This Employee.java file is a separate java file which is describing the Employee.

  package com;
    import java.util.ArrayList;
    import java.sql.Date;
    import java.util.Scanner;
    import java.util.Iterator;
    public class EmployeeManagement {

        public static void main(String[] args) {
            System.out.println("ENTER THE NUMBER OF EMPLOYEE THAT YOU WANT TO ADD:");
            Scanner sc=new Scanner(System.in);
            int num=sc.nextInt();

            ArrayList<Employee> empList=new ArrayList<Employee>();

            EmployeeManagement empMngmt=new EmployeeManagement();

            empMngmt.addEmployees(num, empList);
            empMngmt.displayEmployees(empList);
            sc.close();
        }

        public void addEmployees(int num,ArrayList<Employee> emp){
            Scanner sc1=new Scanner(System.in);
            Scanner sc2=new Scanner(System.in);
            Employee em=new Employee();
            for(int i=1;i<=num;i++){
                System.out.println("Enter the Designation: ");
                String str1=sc1.nextLine();

                System.out.println("Enter the Date of Birth(YYYY-MM-DD): ");
                String str2=sc1.nextLine();
                Date dt=Date.valueOf(str2);

                System.out.println("Enter the Employee ID: ");
                int eID=sc2.nextInt();

                System.out.println("Enter the Name of Employee: ");
                String str3=sc1.nextLine();

                System.out.println("Enter the Salary: ");
                float sal=sc2.nextFloat();

                em.setDesignation(str1);
                em.setDob(dt);
                em.setEmployeeID(eID);
                em.setName(str3);
                em.setSalary(sal);

                emp.add(em);
            }

            sc1.close();
            sc2.close();
        }

        public void displayEmployees(ArrayList<Employee> emp){
            System.out.println("******EMPLOYEE DETAILS******");
            System.out.println("Number of Employees: "+emp.size());
            Iterator<Employee> iterator=emp.iterator();
            while(iterator.hasNext()){
                Employee em=iterator.next();
                System.out.println(em.getDesignation());
                System.out.println(em.getDob());
                System.out.println(em.getEmployeeID());
                System.out.println(em.getName());
                System.out.println(em.getSalary());
            }
        }
    }

//Now, from this code i am able to print the latest updated employee details but not more than 1 employee .
So how to print multiple employee list ?

1 Answer

0 votes
answered Sep 21, 2016 by smiraj (460 points)

crate the object of Employee class inside the for loop in addEmployees method and run the code again.

public void addEmployees(int num, ArrayList emp) {
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);

// if you initial object here it only set the last dataset

   for (int i = 1; i <= num; i++) {
        Employee em = new Employee();
        System.out.println("Enter the Designation: ");
        String str1 = sc1.nextLine();

        System.out.println("Enter the Date of Birth(YYYY-MM-DD): ");
        String str2 = sc1.nextLine();
        Date dt = Date.valueOf(str2);

        System.out.println("Enter the Employee ID: ");
        int eID = sc2.nextInt();

        System.out.println("Enter the Name of Employee: ");
        String str3 = sc1.nextLine();

        System.out.println("Enter the Salary: ");
        float sal = sc2.nextFloat();

        em.setDesignation(str1);
        em.setDob(dt);
        em.setEmployeeID(eID);
        em.setName(str3);
        em.setSalary(sal);

        emp.add(em);
    }

Related questions

0 votes
0 answers
0 votes
1 answer
asked Nov 7, 2015 in JAVA by Shumon (7,690 points)
0 votes
1 answer

722 questions

656 answers

179 comments

4,747 users

Welcome to IDB Scholars Forum, where you can ask questions and receive answers from other members of the community. It's 100% free.
আইডিবি স্কলারস ফোরামে আপনাকে স্বাগত - প্রোগ্রামিং, ডিজাইন, ডেভেলপমেন্ট, ব্লগিং, ইন্টারনেটসহ তথ্য প্রযুক্তির যাবতীয় বিষয় নিয়ে আলোচনার একটি মুক্ত মঞ্চ হলো এই ফোরাম । এখানে আইসিটি বিষয়ক আপনার প্রশ্ন, উত্তর এবং অভিজ্ঞতা শেয়ার করুন, আলোচনা করুন এবং সমাধান নিন। দেশের তরুণ প্রজন্মকে আইসিটিতে আগ্রহী করাই আমাদের মূল লক্ষ্য।
** ইংলিশ বা বাংলা যে কোনো ভাষায় আপনি এই ফোরামে আলোচনা করতে পারবেন। তবে বাংলাকে ইংরেজি অক্ষরে লিখবেন না।
  1. tulbadex

    110 points

    0 answers

  2. JettaQoa3381

    100 points

    0 answers

  3. clutch7conga

    100 points

    0 answers

  4. WhitneyMauer

    100 points

    0 answers

  5. pajamazipper55

    100 points

    0 answers

...