I want to delete a particular employee whose details are entered by the user.I am using serialization in java.Deletion operation is not performing the way i want. i want to delete an employee whose details are entered by the user.but it deletes all the employees
import java.io.*;
import java.util.*;
public class SerializationDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException{
//choice provided to the user
System.out.println("Welcome To Employee Management System");
System.out.println("Enter your choice");
System.out.println("Enter 1:->show employees");
System.out.println("Enter 2:->Create new employee");
System.out.println("Enter 3:->Delete employee");
System.out.println("Enter 4:->quit");
Scanner in = new Scanner(System.in);
int choice = in.nextInt();
ArrayList<Myclass> object1 = new ArrayList<Myclass>();
//insert operation
if(choice==2){
try{
String str1;
String str2;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number of employees you want to add");
Scanner in1 = new Scanner(System.in);
int num = in1.nextInt();
//ArrayList<Myclass> object1 = new ArrayList<Myclass>();
for(int i=0;i<num;i++){
str1 = br.readLine();
str2 = br.readLine();
object1.add(new Myclass(str1,str2));
}
FileOutputStream fos = new FileOutputStream("Myfile.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object1);
oos.flush();
oos.close();
}
catch(Exception e){
System.out.println("exception");
e.printStackTrace();
System.exit(0);
}
}
//show operation
if(choice==1){
try{
FileInputStream fis = new FileInputStream("Myfile.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList object2 = (ArrayList)ois.readObject();
ois.close();
System.out.println("employee details "+" "+object2);
}
catch(Exception e){
e.printStackTrace();
}
}
if(choice==3){
System.out.println("Enter name and department of employee you want to delete");
try {
String str3;
String str4;
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
str3 = br1.readLine();
str4 = br1.readLine();
/*FileInputStream Fs = new FileInputStream("Myfile");
ObjectInputStream Os = new ObjectInputStream(Fs);
ArrayList<Myclass> object3 = (ArrayList<Myclass>)Os.readObject();*/
object1.remove(new Myclass(str3,str4));
FileOutputStream fs = new FileOutputStream("Myfile.txt");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(object1);
os.flush();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//exit operation
if(choice==4){
System.exit(0);
}
}
}
class Myclass implements Serializable{
String name;
String dname;
public Myclass(String name,String dname){
this.name = name;
this.dname = dname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return dname;
}
public void setDepartment(String dname) {
this.dname = dname;
}
public String toString(){
return "name = "+" "+name+","+" "+"department = "+" "+dname;
}
}