package lab06.v2_contactos;

import java.io.*;
import java.util.*;



public class ContactStorageBIN implements ContactsStorageInterface{
    File file;


    public ContactStorageBIN(String filename){
		this.file = new File(filename);
	}

    public boolean saveContacts(List<Contact> list) {

        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
            oos.writeObject(list);
        } 
        catch (FileNotFoundException e) {
            
        }
        catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public List<Contact> loadContacts() {

        List<Contact> contacts = new ArrayList<Contact>();

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            contacts = (List<Contact>) ois.readObject();
        } 
        
        
         catch (FileNotFoundException e) {
            
        } 
        catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
        catch (ClassNotFoundException e) {
            System.out.println("Contact class not found");
            e.printStackTrace();
        }

        return contacts;
    }
}

	

