class 바로 아래 변수 : 멤버 변수, 클래스 변수 라고 부름
전체 범위에 접근 가능
지역변수, 로컬변수 : 메서드 변수
결과를 돌려주지 않는 가장 기본적인 메서드 : main
void (공허) : 내보내는 값이 없다.
( ) : 입력값 Argument/Parameter
하나의 메서드는 하나의 기능만 수행하도로 잘 나누어야함
shift tab : 반대쪽으로 탭 가져오기
import java.util.Scanner;
public class ArrayEx02_Ans {
public static void main(String[] args) {
final int len = 10;
String [] member_id = new String[len];
String [] member_pw = new String[len];
String [] member_name = new String[len];
String [] member_bt = new String[len];
// Dummy data
for(int i=0; i<len; i++) {
member_id[i] = "member" + i;
member_pw[i] = "123456";
member_name[i] = "사용자" + i;
member_bt[i] = "20221027" + i;
}
Scanner s = new Scanner(System.in);
int nextIndex = 10;
while(true) {
System.out.println("==========메뉴=========");
System.out.print("1. 회원가입 2. 로그인 3. 회원정보 수정 4.회원 탈퇴 5.회원조회 0. 프로그램 종료 : ");
int sel = s.nextInt();
s.nextLine();
if (sel == 0) {
System.out.println("종료합니다.");
s.close();
return;
}
switch (sel) {
case 1:
if (nextIndex == len) {
System.out.println("더 이상 가입 불가능 합니다.");
break;
}
System.out.println("==== 회원 정보 등록 ====");
// 맞는 아이디를 입력할 때 까지 아이디 다시 입력하게 만드는 코드
String id = null;
while (true) {
System.out.print("아이디 (6자 이상) : ");
id = s.nextLine();
if(id.length() < 6) {
System.out.println("아이디는 6자 이상이어야 합니다.");
continue;
}
boolean isDuplicated = false;
for (int i=0; i < len; i++) {
if (id.equals(member_id[i])) {
System.out.println("이미 가입된 아이디 입니다.");
isDuplicated = true;
}
}
if(!isDuplicated) {
break;
}
}
String pw = null;
while (true) {
System.out.print("비밀번호 (6자 이상) : ");
pw = s.nextLine();
if(pw.length() < 6) {
System.out.println("비밀번호는 6자 이상이어야 합니다.");
continue;
}
else {
break;
}
}
System.out.print("이름) : ");
String name = s.nextLine();
String birth = null;
while (true) {
System.out.print("생년월일 (8자리) : ");
birth = s.nextLine();
if(birth.length() != 8) {
System.out.println("생년월일 8자로 입력하세요.");
continue;
}
else {
break;
}
}
member_id[nextIndex] = id;
member_pw[nextIndex] = pw;
member_name[nextIndex] = name;
member_bt[nextIndex] = birth;
nextIndex++;
System.out.println("== 회원 등록 완료 ==");
break;
case 2:
System.out.println("=== 로그인");
System.out.print("아이디 : ");
String login_id = s.nextLine();
System.out.print("비밀번호 : ");
String login_pw = s.nextLine();
boolean loginStatus = false;
for (int i=0; i<len; i++) {
if((loginStatus=login_id.equals(member_id[i])) && (login_pw.equals(member_pw[i]))){ // login_id가 member_id[i]와 같다면 loginStatus는 true가 됨
System.out.println("== 로그인 성공 ==");
System.out.println("아이디 : " + member_id[i]);
System.out.println("이름 : " + member_name[i]);
System.out.println("생년월일 : " + member_bt[i]);
break;
}
}
if (!loginStatus) {
System.out.println("로그인 정보가 잘못되었습니다.");
}
break;
case 3:
System.out.println("=== 정보수정");
System.out.print("아이디 : ");
login_id = s.nextLine(); // 중복선언때문에 에러 발생, String 선언만 지워주기.
System.out.print("비밀번호 : ");
login_pw = s.nextLine();
loginStatus = false;
int modify_index = 0;
for (int i=0; i<len; i++) {
if((loginStatus=login_id.equals(member_id[i])) && (login_pw.equals(member_pw[i]))){ // login_id가 member_id[i]와 같다면 loginStatus는 true가 됨
System.out.println("== 기존 정보 ==");
System.out.println("아이디 : " + member_id[i]);
System.out.println("이름 : " + member_name[i]);
System.out.println("생년월일 : " + member_bt[i]);
modify_index = i; // i 와 같은 저장공간에 있는 정보를 수정하기 위해 생성한 index
break;
}
}
if (!loginStatus) {
System.out.println("로그인 정보가 잘못되었습니다.");
break; // 로그인과 달리 스위치 문을 빠져나가게 함.
}
//for문에서 빠져나온 로그인이 된 사람에게 뜨는 창
id = null;
while (true) {
System.out.print("아이디 (6자 이상) : ");
id = s.nextLine();
if(id.length() < 6) {
System.out.println("아이디는 6자 이상이어야 합니다.");
continue;
}
boolean isDuplicated = false;
for (int i=0; i < len; i++) {
if (id.equals(member_id[i])) {
System.out.println("이미 가입된 아이디 입니다.");
isDuplicated = true;
}
}
if(!isDuplicated) {
break;
}
}
pw = null;
while (true) {
System.out.print("비밀번호 (6자 이상) : ");
pw = s.nextLine();
if(pw.length() < 6) {
System.out.println("비밀번호는 6자 이상이어야 합니다.");
continue;
}
else {
break;
}
}
System.out.print("이름) : ");
name = s.nextLine();
birth = null;
while (true) {
System.out.print("생년월일 (8자리) : ");
birth = s.nextLine();
if(birth.length() != 8) {
System.out.println("생년월일 8자로 입력하세요.");
continue;
}
else {
break;
}
}
System.out.print("수정하시겠습니까? ( 1: 예 / 2. 아니오 ) : ");
int confirm = s.nextInt();
if (confirm == 1) {
member_id[modify_index] = id;
member_pw[modify_index] = pw;
member_name[modify_index] = name;
member_bt[modify_index] = birth;
System.out.println("== 등록 정보 수정 완료 ==");
}
else {
System.out.println("정보 수정이 취소되었습니다.");
}
break;
case 4:
System.out.println("=== 회원 탈퇴");
System.out.print("아이디 : ");
login_id = s.nextLine(); // 중복선언때문에 에러 발생, String 선언만 지워주기.
System.out.print("비밀번호 : ");
login_pw = s.nextLine();
loginStatus = false;
int del_index = 0;
for (int i=0; i<len; i++) {
loginStatus = login_id.equals(member_id[i]) && login_pw.equals(member_pw[i]);
if(loginStatus) {
System.out.println("== 기존 정보 ==");
System.out.println("아이디 : " + member_id[i]);
System.out.println("이름 : " + member_name[i]);
System.out.println("생년월일 : " + member_bt[i]);
del_index = i; // i 와 같은 저장공간에 있는 정보를 수정하기 위해 생성한 index
break;
}
}
if (!loginStatus) {
System.out.println("로그인 정보가 잘못되었습니다.");
break; // 로그인과 달리 스위치 문을 빠져나가게 함.
}
System.out.print("탈퇴하시겠습니까? ( 1: 예 / 2. 아니오 ) : ");
confirm = s.nextInt();
s.nextLine();
if (confirm == 1) {
member_id[del_index] = null;
member_pw[del_index] = null;
member_name[del_index] = null;
member_bt[del_index] = null;
System.out.println("== 회원 탈퇴 완료 ==");
// 회원 데이터 사이의 빈 공간을 지워야함
// 회원 데이터 복사
String[] temp1 = new String[len];
String[] temp2 = new String[len];
String[] temp3 = new String[len];
String[] temp4 = new String[len];
for(int i=0; i<len; i++) {
temp1[i] = member_id[i];
temp2[i] = member_pw[i];
temp3[i] = member_name[i];
temp4[i] = member_bt[i];
}
// 기존 데이터 공간의 회원데이터 삭제
for (int i=0; i<len; i++) {
member_id[i] = null;
member_pw[i] = null;
member_name[i] = null;
member_bt[i] = null;
}
// 기존 데이터 공간에 null을 제외한 복사된 데이터 붙여넣기
int n = 0;
int n2 = 0;
while (n<len) {
if(temp1[n] != null) {
// System.out.println(temp[n]);
member_id[n2] = temp1[n];
member_pw[n2] = temp2[n];
member_name[n2] = temp3[n];
member_bt[n2] = temp4[n];
n2++;
}
n++;
}
for(int i=0; i<len; i++) {
System.out.println(member_id[i]+","+member_pw[i]+","+member_name[i]+","+member_bt);
}
nextIndex--;
}
else {
System.out.println("탈퇴가 취소되었습니다.");
}
break;
case 5:
System.out.println( "======== 회원정보=======");
for(int i=0; i<len; i++) {
if(member_id[i] != null)
// if(member_id[i] == null ) continue;
System.out.println(member_id[i] + " / " + member_name [i] + " / " + member_bt[i]);
}
break;
default :
System.out.println("잘못된 메뉴 번호입니다.");
}
}
}
// s.close();
}