반응형

DataFileDBConnector.java

1
2
3
4
5
6
7
8
9
10
11
12
public class DataFileDBConnector {
    
    private File dbFile;
    
    public DataFileDBConnector(String path){
        dbFile = new File(path);
    }
 
    public File getDbFile() {
        return dbFile;
    }
}
cs


MemberDAOImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class MemberDAOImpl 
extends DataFileDBConnector
implements MemberDAO{
 
    public MemberDAOImpl(String path) {
        super(path);
    }
 
    @Override//회원가입
    public void create(Member member) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(getDbFile(), true));
            
        bw.write(member.getId() + "," + member.getPassword());
        bw.newLine();
        bw.close();
 
    }
 
    @Override//id로 회원 검색
    public Member read(String id) throws IOException,FileNotFoundException {
        Member member = new Member();
        
        BufferedReader br = new BufferedReader(new FileReader(getDbFile()));
        
        String data = null;
        while((data = br.readLine()) != null){
            String[] values = data.split(",");
            String readId = values[0];
            String readPassword = values[1];
            
            if(readId.equals(id)){
                member.setId(readId);
                member.setPassword(readPassword);
                br.close();
                return member;
            }
        }
        
        br.close();
        return null;
    }
 
}
cs


MemberServiceImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class MemberServiceImpl 
implements MemberService{
 
    private MemberDAO dao = new MemberDAOImpl("member.txt");
    private Integer result = null;
    
    @Override
    public Integer create(Member member) {//회원가입
        Member data = new Member();
        try{
            data = dao.read(member.getId());
            if(data != null){// 사용중인 아이디 0
                result = 0
            }
            else {// 가입성공 1
                dao.create(member);
                result = 1;
            }
        }catch (IOException e) {// File 저장 에러 2
            result = 2;
        }
        return result;
    }
 
    @Override
    public Integer check(String id, String password) {
        Member member = new Member();
        try{
            member = dao.read(id);
            if(member == null){// 아이디 없음 4
                return 4;
            }else if(member.getPassword().equals(password)){// 비밀번호 틀림 1
                return 1;
            }else{// 로그인 성공 0
                return 0;
            }
        }catch (FileNotFoundException e) { // 파일 찾을 수 없음 2
            return 2;
            
        }catch (IOException e) { // 파일 읽기 오류 3
            return 3;    
        }
    }
}
cs


반응형

'IT > 언어' 카테고리의 다른 글

BSON encode/decode  (0) 2014.05.15
BSON 이해하기  (0) 2014.05.14
[펌][WEB] ajax 크로스도메인 설정~~  (0) 2013.12.18
log4j 에서 ibatis 쿼리 로그 남기는 방법  (0) 2013.11.26
spring 강좌 (2)  (0) 2013.11.26

+ Recent posts