-2

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;
        }
    }
bablu
  • 601
  • 1
  • 9
  • 21

2 Answers2

1

I see two issues here.

  • In choice=3, You need to de-serialize the object into object1 list before removing an object and re-serializing it.
  • You need equals() and hashCode() method in MyClass. list.remove() will not otherwise because it will not able to find the object you given from the list.
bitkot
  • 4,466
  • 2
  • 28
  • 39
  • Thank you AmitChotaliya. I implemented hashcode() and equals() and then implemented. It worked. Thank you very much. – bablu Jul 22 '14 at 10:43
0

@AmitChotaliya has explained clearly

This is a example what you can do to delete a object from ArrayList

First You need to de-serialize

FileInputStream  Fs = new FileInputStream("Myfile.txt");
ObjectInputStream Os = new ObjectInputStream(Fs);
ArrayList<Myclass> object3 = (ArrayList<Myclass>)Os.readObject();

Then remove it but this is fail-fast

for(int index = 0; i < object3.size(); i++)
{
   if(object3.get(i).getName().equals(str3) && object3.get(i).getDepartment().equals(str4))
   {
      object3.remove(index);
      break;
   }
}

But if You want to remove it with Fail Safe

Iterator itr = object3.iterator();

while(itr.hasNext()) {
   Myclass element = (Myclass) itr.next();
   if(element.getName().equals(str3) && element.getDepartment().equals(str4))
   {
       itr.remove();
       break;
   }
}
Ungapps
  • 98
  • 3
  • 14
  • Thanks @Steven, but you can not do this, ArrayList is fail-fast, so you can not iterate using for loop and call remove on it. http://stackoverflow.com/questions/17377407/what-is-fail-safe-fail-fast-iterators-in-java-how-they-are-implemented. You need to iterate using Iterator and then remove use remove. – bitkot Jul 22 '14 at 06:37
  • @AmitChotaliya Of course you can, if you don't keep iterating. He doesn't. He has a 'break' after the remove(). – user207421 Jul 22 '14 at 23:55
  • It wasn't there when I wrote that comment :) – bitkot Jul 23 '14 at 03:14