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]