-2

i got overriding output on this blockcode i don't understand why when inputs are different, i try to print my arraylist but it print twice the last employeer

    public class Emplyee {

        private String name;
        private String surname;
        private int age;
        private String address;

        public Emplyee() {  
        }


        public Emplyee(String name, String surname, int age, String address) {

            this.name = name;
            this.surname = surname;
            this.age = age;
            this.address = address ;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setSurname(String surname) {
            this.surname = surname;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getName() {
            return name;
        }

        public String getSurname() {
            return surname;
        }

        public int getAge() {
            return age;
        }

        public String getAddress() {
            return address;
        }


        @Override
        public String toString() {
            return "Emplyee [Name = " + name + ", Surname = " + surname + 
                    ", Age = " + age + ", Address = " + address + "]";
        }



    }
  main class
    import java.io.*`enter code here`
    import java.util.Scanner;
    import java.util.ArrayList;
    public class Main {

        public static void main(String[] args) throws Exception {


            Emplyee emplyee = new Emplyee();

            ArrayList<Emplyee> listAll=new ArrayList<>();
            Scanner sc = new Scanner(System.in);
            System.out.println("Do you want to add an Employee?"
                    + "\nType 1 for Yes and 0 for Exit");

            while(sc.nextInt()!=0) {
                if (sc.nextInt()==1) {

                    emplyee.getName();
                    System.out.println ("Name: ");
                    emplyee.setName(sc.next());

                    emplyee.getSurname();
                    System.out.println ("Surname: ");
                    emplyee.setSurname(sc.next());

                    emplyee.getAge();
                    System.out.println ("Age: ");
                    emplyee.setAge(sc.nextInt());

                    emplyee.getAddress();
                    System.out.println ("Address: ");
                    emplyee.setAddress(sc.next());

                    listAll.add(emplyee);

                }
                // return employees one by one

                for (Emplyee employee : listAll) {
                    System.out.println(employee.toString());

                    }
                }

                //this print output in a .txt file

                PrintWriter pw = new PrintWriter("Output.txt");
                pw.println(listAll);
                pw.flush();
                pw.close();
                System.out.println("Done!");        

            }    
        }

i put twice diferent data // output is :

Emplyee [Name = hysa, Surname = sdas, Age = 1, Address = durres]

Emplyee [Name = hysa, Surname = sdas, Age = 1, Address = durres]

Hysterx
  • 5
  • 3
  • It looks like you want us to debug your code for you. SO is not really great for this purpose. If you want any traction on this question you are going to have to show your research. Start here: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –  Aug 23 '18 at 14:29
  • Compare how often you create new employees to how often you add them to the list. If you call `new Emplyee()` less then `listAll.add(emplyee);` (which you do) then the list will contain multiple references to the _same_ instance. – Thomas Aug 23 '18 at 14:32
  • What do you think the `emplyee.getName();` etc statements do? – Andy Turner Aug 23 '18 at 14:53

1 Answers1

0

The reason you keep getting duplicate entries is because, while you are creating a new Employee object, you're only doing it once. This means that you will keep modifying the same object per iteration and those changes are reflected in listAll. Make the following change in your while loop:

while(sc.nextInt()!=0) {
    if (sc.nextInt()==1) {

        // create new employee per iteration
        Emplyee emplyee = new Emplyee ( );

        emplyee.getName();
        System.out.println ("Name: ");
        emplyee.setName(sc.next());

        ...

By creating a new employee object per iteration, the list can now be populated with unique employees.

Also, emplyee.getName() isn't being used in a practical sense, so unless you plan to do something with this later, you can remove it to "speed up" your program

user3170251
  • 310
  • 1
  • 13