2015 IT 웹 기반 개발자과정/JAVA
JAVA 멤버관리(아이디/비밀번호), 배열과 객체 이용하기
by 한여름밤의코딩
2015. 10. 28.
package Exam06_14;
public class Member {
String name , id , pw;
int status; // 현재 로그인상태.. 1:로그인상태 0:로그아웃
public Member(String n , String i,String pw)
{
name = n;
id = i;
this.pw = pw;
status = 0;
}
}
=============================================================================================
package Exam06_14;
import java.util.Scanner;
public class MemberService {
static Member[] UserList = new Member[100];
static Scanner scanner = new Scanner(System.in);
public int login(String id , String pw)
{
int i;
for(i=0;i<UserList.length;i++) {
if (UserList[i]==null) break;
if (UserList[i].id.equals(id) &&
UserList[i].pw.equals(pw)) {
if (UserList[i].status==1) return(0);
UserList[i].status = 1;
return(1);
}
}
return (-1);
}
public void logout(String id)
{
int i;
for(i=0;i<UserList.length;i++) {
if (UserList[i]==null) break;
if (UserList[i].id.equals(id) && UserList[i].status==1) {
UserList[i].status = 0;
System.out.println(UserList[i].name+"님이 로그아웃하셨습니다.");
}
}
}
public static void main(String[] args) {
MemberService member = new MemberService();
UserList[0] = new Member("홍길동","hong","12345");
UserList[1] = new Member("고길동","gogi","23456");
UserList[2] = new Member("테스트","test","45678");
do {
System.out.print("0:종료, 1:로그인선택 , 2:로그아웃 = ?");
int choice = scanner.nextInt();
if (choice==0) break;
if (choice==1) {
System.out.print("사용자 ID = ");
String id = scanner.next();
System.out.print("사용자 PW = ");
String pw = scanner.next();
int result = member.login(id,pw);
if (result==1) {
System.out.println("로그인되었습니다.");
} else if (result==0) {
System.out.println("이미 로그인 되어있습니다.");
} else {
System.out.println("id 또는 패스워드가 정확하지않습니다.");
}
}
if (choice==2) {
System.out.print("사용자 ID = ");
String id = scanner.next();
member.logout(id);
}
}while(true);
}
}