Java 응용 | 로그인 및 회원가입

Dalyume·2026년 7월 27일

Java 응용

목록 보기
6/15
post-thumbnail

class와 함수를 이용해서, 간단한 로그인 기능을 만들어볼까 한다.

사용자가 Id나 email을 아이디, password를 비밀번호로 맞는 값을 입력하면 로그인이 가능하도록 할 것이다.

그리고 이번엔 클래스 파일을 분리해서 진행해보겠다.


User 클래스

우선, 사용자 정보를 저장할 클래스부터 지정해주자.

class User {
    String id;
    String password;
    String name;
    String email;

    User(String id, String password, String name, String email) {
        this.id = id;
        this.password = password;
        this.name = name;
        this.email = email;
    }
}

Main 클래스


메인에서 로그인과 회원가입을 고를 클래스이다.

package com.oop2;

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

import static com.oop2.SignUp.signUp;

public class MainPage {
//    public static void users(List<User> userLists) {
//        userLists.add(new User("dalyume2876", "fireflyLover", "dalyume", "dalyume@gmail.com"));
//    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        List<User> userLists = new ArrayList<>();
//        users(userLists);
        int choose = 0;

        while (true) {
            System.out.print("로그인하시려면 1, 회원가입은 2를 눌러주세요.(0 누를 시 종료) : ");
            choose = scan.nextInt();

//            for(User user : userLists){
//                System.out.printf("id : %s, pw : %s, name : %s, email : %s", user.id, user.password, user.name, user.email);
//                System.out.println();
//            }

            if (choose == 1) {
                LoginPage.login(scan, userLists);
                break;
            } else if (choose == 2) {
                signUp(scan, userLists);
            } else if (choose == 0) {
                break;
            } else {
                System.out.println("잘못 입력되었습니다. 다시 시도하세요.");
            }
        }

    }
}

주석은 디버깅을 위해 임시로 넣어두었다.

각각 임시데이터, 임시데이터 저장, 현재 저장되어있는 리스트를 출력한다.

Login 클래스


로그인 역할을 맡은 클래스이다. Main에 있는 list를 불러와 입력한 값과 비교해 같은지 확인한다.

package com.oop2;

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

public class LoginPage {
    public static boolean securityCheck(String inputId, String inputPw, List<User> users){
        for(User user : users){
            if ((user.id.equals(inputId) || user.email.equals(inputId)) && user.password.equals(inputPw)){
                System.out.println("로그인에 성공하였습니다. 환영합니다. " + user.id + "님!");
                return true;
            }
        }

        System.out.println("잘못된 Id 또는 비밀번호입니다.");
        return false;
    }

    public static void login(Scanner scan, List<User> userLists) {
        int tryLogin = 0;
        String inputId;
        String inputPw;

        while(tryLogin != 5){
            tryLogin++;

            System.out.println("아이디와 비밀번호를 입력해주세요.");
            System.out.print("아이디 : ");
            inputId = scan.next();

            System.out.print("비밀번호 : ");
            inputPw = scan.next();

            if(securityCheck(inputId, inputPw, userLists)){
                return;
            }
        }

        System.out.println("로그인 횟수가 5회를 초과했습니다.");

    }

}

로그인 시도 횟수가 5회를 넘어가면 종료된다.

SignUp 클래스


회원가입을 시켜주는 클래스이다.

package com.oop2;

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

public class SignUp {

    static boolean duplicateCheck(String id, List<User> userList) {
        for (User user : userList) {
            if (user.id.equals(id)) {
                return false;
            }
        }
        return true;
    }

    public static void signUp(Scanner scan, List<User> userLists) {
        String id;
        String pw;
        String name;
        String email;

        while (true) {
            System.out.print("가입할 Id를 적어주세요. : ");
            id = scan.next();
            if (duplicateCheck(id, userLists)) {
                System.out.println("생성 가능한 ID 입니다.");
                break;
            } else {
                System.out.println("중복된 ID 입니다.");
            }
        }

        while (true) {
            System.out.print("등록할 비밀번호를 입력해주세요 : ");
            pw = scan.next();
            System.out.print("비밀번호를 한 번 더 입력해주세요. : ");
            if (!scan.next().equals(pw)) {
                System.out.println("비밀번호가 일치하지 않습니다. 다시 시도해주세요.");
                continue;
            }
            break;
        }

        System.out.print("사용자 이름을 입력해주세요.");
        name = scan.next();

        System.out.print("이메일을 입력해주세요. : ");
        email = scan.next();

        System.out.println("회원가입이 완료되었습니다.");
        userLists.add(new User(id, pw, name, email));
    }
}

Id : 사용가능한 id인지 Main에서 list를 가져와 중복확인을 거친 후 넘어간다.

password : 한 번 입력한 password를 다시 입력시켜서, 일치하면 넘어간다.

profile
신생아 개발자

0개의 댓글