Java 응용 | 객체지향적 수정...

Dalyume·2026년 7월 28일

Java 응용

목록 보기
7/15
post-thumbnail

마지막에 올린, 로그인 & 회원가입 코드를 살펴보면, 코드가 좀 많이 뒤죽박죽이였다.

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

특히나 User라는 객체를 만들어놓고 정작 그 객체를 사용하지도 않았다. (백엔드 DTO도 아니고..)

그리고 로그인이나 회원가입클래스에서는 User 객체의 데이터를 직접 꺼내다 쓰기까지..아주 개판이였다.

일단 User 객체를 사용하기 위해 코드를 추가해주겠다.

User


package com.oop2;

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

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

	// 회원가입에 사용할 중복 Id 검사.
    public boolean hasSameId(String inputId){
        return id.equals(inputId);
    }

	// 로그인에 사용할 아이디 비밀번호 일치 확인
    public boolean matchesLogin(String inputId, String inputPassword){
        boolean matchesAccount = id.equals(inputId) || email.equals(inputId);
        boolean matchesPassword = password.equals(inputPassword);

        return matchesAccount && matchesPassword;
    }

	// 로그인 성공시 이름 츨력을 위한 getter
    public String getName() {
        return name;
    }
}

간단히 이정도만 추가해주겠다.

그럼 이제 다른 클래스에 에러가 신나게 터져있을태니..고쳐보도록 하자.

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.hasSameId(id)) { // 수정 된 부분
                return false;
            }
        }
        return true;
    }

수정 전에는

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

이 꼬라지였다. 어우..

LoginPage


public class LoginPage {
    public static boolean securityCheck(String inputId, String inputPw, List<User> users){
        for(User user : users){
            if (user.matchesLogin(inputId, inputPw)){ // 수정 된 부분
                System.out.println("로그인에 성공하였습니다." + user.getName() + "님!");
                return true;
            }
        }

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

얘도 뭐..

(user.id.equals(inputId) || user.email.equals(inputId)) && user.password.equals(inputPw)

조건문 안이 이렇게 되어있었다.

객체지향적 에제 코드를 만드려고 객체를 만들었더니 dto처럼 쓴거같다. 이건 스프링부트가 아닌데 말이다..ㅋㅋ

profile
신생아 개발자

0개의 댓글