Class/220801
Class 예제(2_Service)
by pms93
2022. 8. 1.
package quiz5;
import java.util.ArrayList;
public class MemberServiceImpl implements IMemberService {
MemberDAOImpl memberDao = new MemberDAOImpl();
@Override
public void insert(String id, String pw, String name) {
MemberDTO member = new MemberDTO();
int idx = memberDao.getIdx(id);
if (idx != -1) {
System.out.println("이미 등록된 정보입니다.");
return;
}
member.setId(id);
member.setName(name);
member.setPw(pw);
memberDao.insert(member);
}
@Override
public void selectId(String id) {
int idx = memberDao.getIdx(id);
if (idx == -1) {
System.out.println(id + "는 존재하지 않는 정보입니다.");
return;
}
MemberDTO tmp = memberDao.selectId(id);
System.out.printf("ID : %s\t이름 : %s\t비밀번호 : %s\n", tmp.getId(), tmp.getName(), tmp.getPw());
}
@Override
public void delete(String id) {
int idx = memberDao.getIdx(id);
if (idx == -1) {
System.out.println(id + "는 존재하지 않는 정보입니다.");
return;
}
MemberDTO tmp = new MemberDTO();
tmp.setId(id);
memberDao.delete(tmp);
}
@Override
public void selectAll() {
if (memberDao.members.isEmpty()) {
System.out.println("등록된 정보가 없습니다.");
return;
}
ArrayList<MemberDTO> printInfo = memberDao.selectAll();
System.out.println("<< 등록 정보 >>");
for (MemberDTO member : printInfo) {
System.out.printf("ID : %s\t이름 : %s\t비밀번호 : %s\n", member.getId(), member.getName(), member.getPw());
}
}
@Override
public void update(String id, String currentPw, String changePw) {
int idx = memberDao.getIdx(id);
if (idx == -1) {
System.out.println(id + "는 존재하지 않는 정보입니다.");
return;
} else if (memberDao.members.get(idx).getPw() != currentPw) {
System.out.println("비밀번호가 틀립니다.");
return;
}
MemberDTO tmp = new MemberDTO();
tmp.setPw(changePw);
memberDao.update(idx, tmp);
}
}