package jp.gihyo.mitoma.android.p031memberlist;

import java.util.ArrayList;

public class MemberDS{
        private ArrayList<MemberDTO> list = new ArrayList<>();
        private static MemberDS instance = null;
        private MemberDS(){
            MemberDTO dto;
            dto = new MemberDTO("G00001","佐藤太郎","090-1234-X000","satoh001@○○.co.jp");
            list.add(dto);
            dto = new MemberDTO("G00002","鈴木陽介","090-2345-9Y76","suzuko_you@×××.com");
            list.add(dto);
            dto = new MemberDTO("S00001","高橋翔一","070-1985-Z567","takahashiken@△△.ne.jp");
            list.add(dto);
            dto = new MemberDTO("G00003","渡辺咲良","033-5566-X012","wata00101@xxx.co.jp");
            list.add(dto);
            dto = new MemberDTO("S00002","山本すず","045-7894-12A4","yamamotosuzu@cccxx.com");
            list.add(dto);
            dto = new MemberDTO("G00004","堤はるみ","091-2222-2K38","haruchan@〇△□.com");
            list.add(dto);
            dto = new MemberDTO("S00003","中村あおい","012-9514-77G2","nakamura478@kyusyuxx.ne.jp");
            list.add(dto);
            dto = new MemberDTO("B00001","伊藤　剛","080-5546-45Y7","itohzou01@qqqzs.com");
            list.add(dto);
            dto = new MemberDTO("S00004","田中　実","081-65245-46S9","tanaka.z01@nigaty.com");
            list.add(dto);
            dto = new MemberDTO("B00002","小林良子","050-5124-77E3","riyou223344@zzyyxx.co.jp");
            list.add(dto);
        }
        public static MemberDS getInstance(){
            if(MemberDS.instance == null){
                MemberDS.instance = new MemberDS();
            }
            return MemberDS.instance;
        }
        public ArrayList<MemberDTO> select(){
            return this.list;
        }
        public int insert(MemberDTO dto){
            this.list.add(dto);
            return 1;
        }
        public MemberDTO select(String no){
            MemberDTO dto = null;
            int i = 0;
            for(MemberDTO dtoX:this.list){
                if(dtoX.memberNo.equals(no))break;
                i++;
            }
            if(i < list.size())dto=list.get(i);
            return dto;
        }

        public int update(MemberDTO dto){
            int retNo = 0;
            int i = 0;
            String id = dto.memberNo;
            for(MemberDTO dtoX:this.list){
                if(dtoX.memberNo.equals(id))break;
                i++;
            }
            if(i < list.size()){
                list.set(i,dto);
                retNo = 1;
            }
            return retNo;
        }
        public int delete(String no){
            int retNo = 0;
            int i = 0;
            for(MemberDTO dtoX:this.list){
                if(dtoX.memberNo.equals(no))break;
                i++;
            }
            if(i < list.size()){
                list.remove(i);
                retNo = 1;
            }
            return retNo;
        }
}
