미국산 귤

[Java] 간단한 회원가입 프로그램 구현 본문

Java

[Java] 간단한 회원가입 프로그램 구현

migyul 2024. 8. 24. 23:49

오늘은 자바의 스캐너, 해쉬맵, 어레이리스트를 사용하여 간단한 회원가입 프로그램을 구현해보았다.

 

 

 

먼저 boolean을 false로 초기화 시켜둔 상태로 회원가입을 진행할지 유저에게 묻는다.

사용자의 입력이 y일 경우 진행, n일 경우 exit(0)을 통해 자바 프로그램의 실행을 종료시킨다.

이외의 값이 입력될 경우 안내 문구와 함께 다시 진행 여부를 묻는다.

boolean register = false;
Scanner sc = new Scanner(System.in);

    while (!register) {
        System.out.println("회원가입을 하시겠습니까?\nY: 진행 N: 취소");
        System.out.print(">> ");
        String register_input = sc.nextLine();

        if (register_input.equalsIgnoreCase("y")) {
            register = true;
            System.out.println("=====================");
            System.out.println("회원가입이 진행됩니다.");
            System.out.println("=====================");
        }else if (register_input.equalsIgnoreCase("n")) {
            System.out.println("=====================");
            System.out.println("회원가입이 종료됩니다.");
            System.out.println("=====================");
            System.exit(0); // 프로그램 종료 시킴
        }else {
            System.out.println("입력 값을 확인해주세요.");
        }
    }

 

 

 

그리고 여러 유저의 정보를 담을 ArrayList를 선언한다.

ArrayList users = new ArrayList();

 

 

 

그리고 각 유저의 이름, 이메일, 생년월일 등 여러 개의 정보 입력 받을 while문 내에서

각각의 정보를 키-값 한 쌍으로 저장하기 위해 HashMap을 선언한다.

while (true) {
    HashMap user = new HashMap();

 

 

 

ID, 이름, 이메일은 간단하게 입력만 받으면 되므로 이렇게 구현했다.

// ID
System.out.print("ID: ");
String username = sc.nextLine();

// 이름
System.out.print("성명: ");
String name = sc.nextLine();
            
// 이메일
System.out.print("이메일: ");
String email = sc.nextLine();

 

 

 

비밀번호는 확인 단계를 거쳐야 하므로 while문으로 구현했다.

비밀번호와 비밀번호 확인을 입력받은 후, equals()로 둘을 비교한다.

둘이 일치할 경우 반복문을 빠져나오고, 일치하지 않을 경우 비밀번호를 처음부터 입력하도록 하였다.

// PW
String password;

while (true) {
    System.out.print("PW: ");
    password = sc.nextLine();
    System.out.print("PW 확인: ");
    String password_confirm = sc.nextLine();

    if (password.equals(password_confirm)) {
        break;
    }else {
        System.out.println("=====================");
        System.out.println("패스워드가 일치하지 않습니다.");
        System.out.println("다시 입력해주세요.");
        System.out.println("=====================");
    }

}

 

 

 

생년월일은 입력받은 후, length()를 통해 6자리인지 확인한 후 아닐 경우 다시 입력하도록 하였다.

// 생년월일(6자리)
String birth_date; // 바깥에서 참조해야 하는 변수이기 때문에 외부에서 선언하는 것이 좋음

while (true) {
    System.out.println("생년월일(6자리): ");
    birth_date = sc.nextLine();
    if (birth_date.length()==6) {
        break;
    }else {
        System.out.println("=====================");
        System.out.println("생년월일 자릿수가 올바르지 않습니다.");
        System.out.println("다시 입력해주세요.");
        System.out.println("=====================");
    }
}

 

 

 

모든 입력이 끝나면 HaspMap에 각 키-값을 넣어준다.

 

user.put("username", username);
user.put("password", password);
user.put("name", name);
user.put("birth_date", birth_date);
user.put("email", email);

 

 

 

그리고 ArrayList에 유저 정보를 저장한다.

users.add(user);

 

 

 

회원가입 절차가 완료됨을 알리는 안내 문구를 출력하고, 맨처음과 같은 로직으로 다시 가입 여부를 묻는다.

System.out.println("=====================");
        System.out.println(user.get("name") + " 님, 가입을 환영합니다.");
        System.out.println("ID는 " + user.get("username") + " 입니다.");
        System.out.println("=====================");

        System.out.println("회원가입을 이이서 진행하시겠습니까?\nY: 진행 N: 취소");
        System.out.print(">> ");
        String register_again = sc.nextLine();

        if(register_again.equalsIgnoreCase("y")) {
            ;
        } else if (register_again.equalsIgnoreCase("n")) {
            System.exit(0);
        }

 

 

 

 

이렇게 간단한 회원가입 프로그램을 구현해보았다.

 

 

 

 

 

배운 점

1. ArrayList와 HashMap 각각의 자료구조가 어떻게 다른 역할을 하는지 명확하게 이해했다.

2. exit(0)이 break가 아니라 자바 프로그램 자체를 종료시킴을 알게 되었다.

 

 

 

 

 

 

 

 

 

 

전체 코드

package chapter10_1;

import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class chapter10_1 {
	
	public static void main(String[] args) {
		
		System.out.println("=====================");
		System.out.println("회원등록");
		System.out.println("=====================");

		boolean register = false;
		Scanner sc = new Scanner(System.in);
		
		while (!register) {
			System.out.println("회원가입을 하시겠습니까?\nY: 진행 N: 취소");
			System.out.print(">> ");
			String register_input = sc.nextLine();
			
			if (register_input.equalsIgnoreCase("y")) {
				register = true;
				System.out.println("=====================");
				System.out.println("회원가입이 진행됩니다.");
				System.out.println("=====================");
			}else if (register_input.equalsIgnoreCase("n")) {
				System.out.println("=====================");
				System.out.println("회원가입이 종료됩니다.");
				System.out.println("=====================");
				System.exit(0); // while문 종료 시킴
			}else {
				System.out.println("입력 값을 확인해주세요.");
			}
		}
		
		ArrayList users = new ArrayList();
		
		while (true) {
			HashMap user = new HashMap();
			
			// ID
			System.out.print("ID: ");
			String username = sc.nextLine();
			
			// PW
			String password;
			while (true) {
				System.out.print("PW: ");
				password = sc.nextLine();
				System.out.print("PW 확인: ");
				String password_confirm = sc.nextLine();
				
				if (password.equals(password_confirm)) {
					break;
				}else {
					System.out.println("=====================");
					System.out.println("패스워드가 일치하지 않습니다.");
					System.out.println("다시 입력해주세요.");
					System.out.println("=====================");
				}
				
			}
			
			// 이름
			System.out.print("성명: ");
			String name = sc.nextLine();
			
			// 생년월일(6자리)
			String birth_date; // 바깥에서 참조해야 하는 변수이기 때문에 외부에서 선언하는 것이 좋음
			
			while (true) {
				System.out.println("생년월일(6자리): ");
				birth_date = sc.nextLine();
				if (birth_date.length()==6) {
					break;
				}else {
					System.out.println("=====================");
					System.out.println("생년월일 자릿수가 올바르지 않습니다.");
					System.out.println("다시 입력해주세요.");
					System.out.println("=====================");
				}
			}
			
			// 이메일
			System.out.print("이메일: ");
			String email = sc.nextLine();
			
			
			
			user.put("username", username);
			user.put("password", password);
			user.put("name", name);
			user.put("birth_date", birth_date);
			user.put("email", email);
			
			users.add(user);
			
			System.out.println("=====================");
			System.out.println(user.get("name") + " 님, 가입을 환영합니다.");
			System.out.println("ID는 " + user.get("username") + " 입니다.");
			System.out.println("=====================");
			
			System.out.println("회원가입을 이이서 진행하시겠습니까?\nY: 진행 N: 취소");
			System.out.print(">> ");
			String register_again = sc.nextLine();
			
			if(register_again.equalsIgnoreCase("y")) {
				;
			} else if (register_again.equalsIgnoreCase("n")) {
				System.exit(0);
			}
			

		}
		
	}

}

'Java' 카테고리의 다른 글

[Java] 기본 메소드 정리  (0) 2024.08.14
[Java 기초] 문자열 리터럴과 생성자 차이 알아보기  (0) 2024.08.11