본문 바로가기
개발일지

개발일지 17일차 -class 역할 분리 응용-

by 태운콩즙 2023. 12. 18.
728x90
반응형

오늘은 지난시간에 이어서 class 역할 분리를 응용하여 회원가입 , 정보 수정 ,회원 탈퇴 , 로그아웃, 중복확인 등을 구현해보았다

package ch11_classes.ex3;

public class memberDTO {
    private  Long id;
    private String memberEamil;
    private String memberPassword;
    private String memberName;
    private String memberMobile;

    public void setId(Long id) {
        this.id = id;
    }

    public void setMemberEamil(String memberEamil) {
        this.memberEamil = memberEamil;
    }

    public void setMemberPassword(String memberPassword) {
        this.memberPassword = memberPassword;
    }

    public void setMemberName(String memberName) {
        this.memberName = memberName;
    }

    public void setMemberMobile(String memberMobile) {
        this.memberMobile = memberMobile;
    }

    public Long getId() {
        return id;
    }

    public String getMemberEamil() {
        return memberEamil;
    }

    public String getMemberPassword() {
        return memberPassword;
    }

    public String getMemberName() {
        return memberName;
    }

    public String getMemberMobile() {
        return memberMobile;
    }
    public memberDTO(){

    }

    private static Long idValue = 1L;
    public memberDTO(String memberEamil , String memberPassword , String memberName , String memberMobile){
        this.id = idValue++;
        this.memberEamil = memberEamil;
        this.memberPassword = memberPassword;
        this.memberName = memberName;
        this.memberMobile = memberMobile;
    }

    @Override
    public String toString() {
        return "memberDTO{" +
                "id=" + id +
                ", memberEamil='" + memberEamil + '\'' +
                ", memberPassword='" + memberPassword + '\'' +
                ", memberName='" + memberName + '\'' +
                ", memberMobile='" + memberMobile + '\'' +
                '}';
    }
}

기본 필드이다

회원의 이메일

비밀번호

이름

전화번호를 입력 받도록 만들었고

id 는 arrayList에 넣을것이기에 Long 타입으로 해주었다

 

 

<MAIN>

package ch11_classes.ex3;

import java.util.Scanner;

public class memberMain {
    public static void main(String[] args) {
        memberService memberService = new memberService();

        Scanner scanner = new Scanner(System.in);
        int selecNo = 0;
        boolean run = true;
        while (run){
            System.out.println("1.회원가입 | 2.로그인 | 3.회원 목록 조회 | 4.회원 정보 수정 | 5.회원탈퇴 | 6.로그아웃 | 0.종료");
            selecNo = scanner.nextInt();
            if (selecNo == 1){
                memberService.memberJoin();
            } else if (selecNo == 2) {
                memberService.memberLogin("","");

            } else if (selecNo == 3) {
                memberService.findAll();

            } else if (selecNo == 4) {
                memberService.memberUpdate();

            } else if (selecNo == 5) {
                memberService.memberDelete();
                
            } else if (selecNo == 6) {
                memberService.memberLogout();
            } else if (selecNo == 0) {
                System.out.println("종료합니다");
                break;
            }
        }
    }
}

 

각각의 번호를 입력하면 그에 맞는 메서드가 실행되게 만들어주고 0번을 입력하면 종료

 

<Service>

package ch11_classes.ex3;

import java.util.List;
import java.util.Scanner;

public class memberService {
    Scanner scanner = new Scanner(System.in);
    private static String loginEmail = null;
    static memberRepository memberRepository = new memberRepository();

    public static void findAll() {
        List<memberDTO> memberDTOList = memberRepository.findAll();
        for (memberDTO memberDTO : memberDTOList) {
            System.out.println("memberDTO = " + memberDTO);
        }// DTO 리스트를 만들고 그 안에 입력된값을 불러옴
    }

    public void memberJoin() {
        // 중복체크 결과를 담을 변수
        boolean checkResult = false;
        String memberEmali = null;
        do {
            System.out.print("이메일: ");
            memberEmali = scanner.next();
            // checkReslt 가 true 라면 사용가능(반복문 종료), checkResult 가 false 라면 사용불가(다시 이메일 입력)
            checkResult = memberRepository.emailChack(memberEmali);
            if (checkResult){
                System.out.println("사용가능한 이메일 입니다");
            }else {
                System.out.println("이미 사용중인 이메일 입니다");
            }
        }while (!checkResult);//checkResult 값이 false 라면 계속 반복되도록
        System.out.print("비밀 번호: ");
        String memberPassword = scanner.next();
        System.out.print("이름: ");
        String memberName = scanner.next();
        System.out.print("전화번호: ");
        String memberMobile = scanner.next();
        memberDTO memberDTO = new memberDTO(memberEmali, memberPassword, memberName, memberMobile);
        boolean result = memberRepository.memberJoin(memberDTO);
        if (result) {
            System.out.println("등록 성공");
        } else {
            System.out.println("등록 실패");
        }
    }

    public void memberLogin(String memberEmail, String memberPassword) {
        System.out.print("이메일: ");
        memberEmail = scanner.next();
        System.out.print("비밀번호: ");
        memberPassword = scanner.next();
       memberDTO loginResult = memberRepository.memberLogin(memberEmail, memberPassword);
        if (loginResult != null){
            System.out.println("로그인 성공");
            loginEmail= memberEmail;
        }else {
            System.out.println("로그인 실패");
        }

    }// 로그인에 성공하게 되면 loginEmali이라는 변수에 이메일을 담아주어 로그인 상태 유지



    public void memberUpdate() {
        if (loginEmail != null){
            System.out.print("수정할 전화번호: ");
            String memberMobile = scanner.next();
          boolean updateResult = memberRepository.memberUpdate(loginEmail , memberMobile);
            if (updateResult){
                System.out.println("수정 성공");

            }else {
                System.out.println("수정 실패");

            }
        }else {
            System.out.println("로그인을 실행하세요");
        }

    }

    public void memberDelete() {
        if (loginEmail != null){
            System.out.println("탈퇴하시겠습니까?");
            System.out.print("비밀 번호: ");
            String memberPassword = scanner.next();
            memberDTO memberDTO=memberRepository.memberLogin(loginEmail,memberPassword);
            if (memberDTO != null){
                boolean result = memberRepository.memberDelete(loginEmail);
                if (result){
                    System.out.println("탈퇴 되었습니다");
                }else {
                    System.out.println("탈퇴가 처리되지 않았습니다");
                }
            }
        }else {
            System.out.println("로그인 해주세요");
        }
    }

    public void memberLogout() {
        loginEmail = null;
        System.out.println("로그아웃 되었습니다");
    }
}

 

 

<Repository>

 

package ch11_classes.ex3;

import java.util.ArrayList;
import java.util.List;

public class memberRepository {
    private static List<memberDTO> memberDTOList= new ArrayList<>();

    public List<memberDTO> findAll() {
        return memberDTOList;
    }

    public boolean memberJoin(memberDTO memberDTO) {
        return memberDTOList.add(memberDTO);
    }

    public memberDTO memberLogin(String memberEmail, String memberPassword) {
        memberDTO result = null;
      for (int i = 0; i<memberDTOList.size(); i++){
          if (memberEmail.equals(memberDTOList.get(i).getMemberEamil()) && memberPassword.equals(memberDTOList.get(i).getMemberPassword())){
              result = memberDTOList.get(i);

          }
      }
        return result;
    }

    public boolean memberUpdate(String loginEmail , String memberMobile ) {
        boolean result = false;
        for (int i = 0; i < memberDTOList.size(); i++){
            if (loginEmail.equals(memberDTOList.get(i).getMemberEamil())){
                memberDTOList.get(i).setMemberMobile(memberMobile);
                result = true;
            }
        }
        return result;
    }

    public boolean memberDelete(String loginEmail) {
        boolean result = false;
        for (int i = 0; i < memberDTOList.size(); i++) {
            if (loginEmail.equals(memberDTOList.get(i).getMemberEamil())){
                memberDTOList.remove(i);
                result = true;
            }
        }
        return result;
    }

    public boolean emailChack(String memberEmali) {
        boolean result = true;
        for (int i = 0; i < memberDTOList.size(); i++) {
            if (memberEmali.equals(memberDTOList.get(i).getMemberEamil())){
                // 중복되는 이메일이 있다 => 결과를 false로 주자
                result = false;
            }
        }
        return result;
    }
}

 

 

각 상황에 따른 메서드 이다 

기존에 배웠던 회원가입 , 로그인 , 목록 출력, 정보 수정 , 정보삭제등을 활용하였다

 

이전에 배웠던 내용에 추가된 부분은 주석을 참조

728x90
반응형