package PhoneBook.bll;

import PhoneBook.model.Record;

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

/**
 * Created by User on 19.07.2015.
 */
public class PhoneBookBin extends PhoneBook
{
    public String pathFileRepository;

    public PhoneBookBin(String pathFileRepository)throws IOException, ClassNotFoundException
    {
        this.pathFileRepository = pathFileRepository;
        load();

    }
    @Override
    public void load() throws IOException, ClassNotFoundException
    {

        File file = new File(pathFileRepository);
        int maxIndex=0;

        try(FileInputStream fileInputStream = new FileInputStream(file);
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {

            phoneBook=(Map <Integer,Record>)objectInputStream.readObject();

        }

    }

    @Override
    public void setAll(Map<Integer, Record> entity)
    {
        this.phoneBook=entity;
        save();
    }


    @Override
    public void save()
    {
        File file = new File(pathFileRepository);

        try(FileOutputStream fileOutputStream = new FileOutputStream(file);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {

            objectOutputStream.writeObject(phoneBook);


        } catch (IOException e) {
            e.printStackTrace();
        }
        //load();

    }



}
