본문 바로가기
Class/220802

Class 예제(1_Service)

by pms93 2022. 8. 2.
package quiz6;

import java.util.Collection;

public class MemberServiceImpl implements IMemberService {

	private MemberDAOImpl memberDao = new MemberDAOImpl();
	MemberDTO tmp = new MemberDTO();

	@Override
	public void insert(String id, String pw, String name) {
		tmp = memberDao.selectId(id);

		if (tmp != null) {
			System.out.println(id + "는 이미 등록된 정보입니다.");
			return;
		}

		MemberDTO member = new MemberDTO();
		member.setId(id);
		member.setPw(pw);
		member.setName(name);
		memberDao.insert(member);
	}

	@Override
	public void selectId(String id) {
		tmp = memberDao.selectId(id);

		if (tmp == null) {
			System.out.println(id + "는 존재하지 않습니다");
			return;
		}

		System.out.println("<< 출력 결과 >>");
		System.out.printf("ID : %s\t비밀번호 : %s\t이름 : %s\n", tmp.getId(), tmp.getPw(), tmp.getName());
	}

	@Override
	public void delete(String id) {
		tmp = memberDao.selectId(id);

		if (tmp == null) {
			System.out.println(id + "는 존재하지 않습니다");
			return;
		}

		memberDao.delete(id);
		System.out.println("삭제가 완료되었습니다.");
	}

	@Override
	public void selectAll() {
		Collection<MemberDTO> tmp2 = memberDao.selectAll();
		
		if (tmp2.isEmpty()) {
			System.out.println("회원등록 후 이용하세요...");
			return;
		}
		
		for (MemberDTO member : tmp2) {
			System.out.printf("ID : %s\t비밀번호 : %s\t이름 : %s\n", member.getId(), member.getPw(), member.getName());
		}
	}

	@Override
	public void update(String id, String currentPw, String changePw) {
		tmp = memberDao.selectId(id);
		
		if (tmp == null) {
			System.out.println(id + "는 존재하지 않습니다");
			return;
		} else if (!tmp.getPw().equals(currentPw)) {
			System.out.println(id + "의 비밀번호가 틀렸습니다");
			return;
		}

		tmp.setPw(changePw);
		memberDao.update(tmp);
		System.out.println("수정이 완료되었습니다.");
	}

}

'Class > 220802' 카테고리의 다른 글

Class 예제(1_MemberDTO)  (0) 2022.08.02
Class 예제(1_MemberDAO)  (0) 2022.08.02
Class 예제(1_MemberDAOIntefface)  (0) 2022.08.02
Class 예제(1_ServiceInterface)  (0) 2022.08.02
Class 예제(1_Main)  (0) 2022.08.02