package jp.gihyo.mitoma.android.s0703listview;

import java.util.ArrayList;

public class FriendsDAO {
    private ArrayList<FriendsDTO> list = new ArrayList<>();
    private static FriendsDAO instance = null;
    private FriendsDAO(){
        FriendsDTO dto;
        dto = new FriendsDTO("F0001","佐藤太郎","090-1234-X000","");
        list.add(dto);
        dto = new FriendsDTO("F0002","鈴木陽介","090-2345-9Y76","");
        list.add(dto);
        dto = new FriendsDTO("S0001","高橋健一","070-1985-Z567","");
        list.add(dto);
        dto = new FriendsDTO("F0003","渡辺咲良","033-5566-X012","");
        list.add(dto);
        dto = new FriendsDTO("S0002","山本すず","045-7894-12A4","");
        list.add(dto);
        dto = new FriendsDTO("S0003","田中　実","081-65245-46S9","");
        list.add(dto);
        dto = new FriendsDTO("S0004","中村あおい","012-9514-77G2","");
        list.add(dto);
        dto = new FriendsDTO("W0001","伊藤　剛","080-5546-45Y7","");
        list.add(dto);
        dto = new FriendsDTO("F0004","堤はるみ","091-2222-2K38","");
        list.add(dto);
        dto = new FriendsDTO("W0002","小林良子","050-5124-77E3","");
        list.add(dto);
    }
    public static FriendsDAO getInstance(){
        if(FriendsDAO.instance == null){
            FriendsDAO.instance = new FriendsDAO();
        }
        return FriendsDAO.instance;
    }
    public ArrayList<FriendsDTO> select(){

        return this.list;
    }

    public FriendsDTO select(String id){
        FriendsDTO dto = null;
        int i = 0;
        for(FriendsDTO dtoX:this.list){
            if(dtoX.id.equals(id))break;
            i++;
        }
        if(i < list.size())dto=list.get(i);
        return dto;
    }
    public int insert(FriendsDTO dto){
        this.list.add(dto);
        return 1;
    }
    public int update(FriendsDTO dto){
        int retNo = 0;
        int i = 0;
        String id = dto.id;
        for(FriendsDTO dtoX:this.list){
            if(dtoX.id.equals(id))break;
            i++;
        }
        if(i < list.size()){
            list.set(i,dto);
            retNo = 1;
        }
        return retNo;
    }
    public int delete(String id){
        int retNo = 0;
        int i = 0;
        for(FriendsDTO dtoX:this.list){
            if(dtoX.id.equals(id))break;
            i++;
        }
        if(i < list.size()){
            list.remove(i);
            retNo = 1;
        }
        return retNo;
    }
}
