Created
June 18, 2025 07:53
-
-
Save Memii42/434ab4727c4fadd9e1b3fbf1dfd77684 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//BE 38 이소정 | |
// 2. 나이제한 시스템 (미성년자 제외) | |
import java.util.Scanner; | |
import java.util.ArrayList; | |
public class AgevalidatorPractice { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
ArrayList<Register> userList = new ArrayList<>(); | |
while (true) { | |
System.out.println("\n👤 사용자 등록 시스템(1. register, 2.list, 3.exit"); | |
System.out.print(" 번호를 입력하세요:"); | |
int instruction; | |
try { | |
instruction = Integer.parseInt(sc.nextLine().trim()); | |
} catch (NumberFormatException e) { | |
System.out.println("⚠️숫자를 입력하세요!"); | |
continue; | |
} | |
switch (instruction) { | |
case 1: | |
System.out.print("이름 입력:"); | |
String name = sc.nextLine().trim(); | |
System.out.print("나이 입력:"); | |
int age; | |
try { | |
age = Integer.parseInt(sc.nextLine().trim()); | |
// 18세 미만 검사 | |
if (age < 18) { | |
throw new UnderageException("❌18세 미만은 등록 할 수 없습니다."); | |
} | |
Register user = new Register(name, age); | |
userList.add(user); | |
System.out.println("✅사용자 등록 완료!"); | |
} catch (NumberFormatException e) { | |
System.out.println("⚠️숫자만 입력하세요!"); | |
} catch (UnderageException ue) { | |
System.out.println(ue.getMessage()); | |
} | |
break; | |
case 2: | |
System.out.println("[🗒️ 등록된 사용자 목록]"); | |
if(userList.isEmpty()){ | |
System.out.println("❌등록된 사용자가 없습니다. "); | |
}else { | |
for (Register userN : userList) { | |
System.out.println(userN.name + "(나이: " + userN.age + " 살)"); | |
} | |
} | |
break; | |
case 3: | |
System.out.println("👋 프로그램을 종료합니다."); | |
sc.close(); | |
return; | |
default: | |
System.out.println("1~3 사이의 번호를 입력해주세요."); | |
} | |
} | |
} | |
public static class UnderageException extends Exception{ | |
public UnderageException(String message) { | |
super(message); | |
} | |
} | |
public static class Register { | |
String name; | |
int age; | |
public Register(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment