public void login() {
System.out.println("== 로그인 ==");
int loginIdTryCount = 0;
int loginIdTryMaxCount = 3;
String loginId = null;
String loginPw = null;
while (true) {
if (loginIdTryCount >= loginIdTryMaxCount) {
System.out.println("아이디를 확인하고 다시 시도해주세요.");
return;
}
System.out.printf("아이디 입력 : ");
loginId = sc.nextLine().trim();
if (loginId.length() == 0) {
loginIdTryCount++;
System.out.println("아이디를 입력해 주세요.");
continue;
}
boolean isLoginDup = memberService.isLoginIdDup(loginId);
if (isLoginDup == false) {
loginIdTryCount++;
System.out.printf("%s는(은) 존재하지 않는 아이디입니다.\n", loginId);
continue;
}
break;
}
Member member = memberService.getMemberByLoginId(loginId);
int loginPwTryCount = 0;
int loginPwTryMaxCount = 3;
while (true) {
if (loginPwTryCount >= loginPwTryMaxCount) {
System.out.println("비밀번호를 확인하고 다시 시도해주세요.");
return;
}
System.out.printf("비밀번호 입력 : ");
loginPw = sc.nextLine();
if (loginPw.length() == 0) {
loginPwTryCount++;
System.out.println("비밀번호를 입력해 주세요.");
continue;
}
if (member.loginPw.equals(loginPw) == false) {
loginPwTryCount++;
System.out.println("비밀번호가 일치하지 않습니다.");
continue;
}
break;
}
System.out.printf("%s님 로그인 성공\n", member.name);
}
memberService.getMemberByLoginId(loginId) 메소드를 통해 입력받은 loginId와 일치하는 회원(Member)을 찾음
찾은 회원의 비밀번호와 입력받은 비밀번호가 일치하는지 체크하고
loginId입력과 동일하게 3번 공백이거나 일치하지 않으면 return
//Map<String, Object> memberMap =DBUtil.selectRow(conn, sql);
public Member(Map<String, Object> memberMap) {
this.id = (int)memberMap.get("id");
this.regDate = (LocalDateTime) memberMap.get("regDate");
this.updateDate = (LocalDateTime) memberMap.get("updateDate");
this.loginId = (String)memberMap.get("loginId");
this.loginPw = (String)memberMap.get("loginPw");
this.name = (String)memberMap.get("name");
}
private int action(Connection conn, Scanner sc, String cmd) {
MemberController memberController = new MemberController(conn,sc);
ArticleController articleController = new ArticleController(conn,sc);
연결시마다 ( action() 을 실행할때마다 ) 새로 Controller들을 생성함
현재 controller 안에 필드로 service가 있고 service 안에 필드로 dao가있다. 각각 실행할때마다 생성되고있다.
이를 해결하기 위해 container 도입
public class Container {
public static ArticleController articleController;
public static MemberController memberController;
public static ArticleService articleService;
public static MemberService memberService;
public static ArticleDao articleDao;
public static MemberDao memberDao;
public static Session session;
public static Scanner sc;
public static Connection conn;
public static void init() {
session = new Session();
articleDao = new ArticleDao();
memberDao = new MemberDao();
articleService = new ArticleService();
memberService = new MemberService();
articleController = new ArticleController();
memberController = new MemberController();
}
}
try {
conn = DriverManager.getConnection(url, "root", "");
Container.conn = conn;
int actionResult = action(cmd);
if (actionResult == -1) {
break;
}
public static int update(Connection dbConn, SecSql sql) {
int affectedRows = 0;
PreparedStatement stmt = null;
try {
stmt = sql.getPreparedStatement(dbConn);
affectedRows = stmt.executeUpdate();
} catch (SQLException e) {
throw new SQLErrorException("SQL 예외, SQL : " + sql, e);
try {
new App().run();
} catch (SQLErrorException e) {
System.err.println(e.getMessage());
e.getOrigin().printStackTrace();
}
public class Session {
public int loginedMemberId;
public Member loginedMember;
public Session() {
loginedMemberId = -1;
}
}
public void login(String cmd) {
if(Container.session.loginedMemberId != -1) {
System.out.println("로그아웃 후 이용 가능합니다.");
return;
}
~~~~~~~~~~~~~~~~~
Container.session.loginedMember = member;
Container.session.loginedMemberId = member.id;
System.out.printf("%s님 환영합니다.\n", member.name);
break;
}
public void showProfile(String cmd) {
if(Container.session.loginedMemberId==-1) {
System.out.println("로그인 후 이용가능합니다.");
return;
}else {
System.out.printf("번호 : %d\n", Container.session.loginedMember.id);
System.out.printf("아이디 : %s\n"Container.session.loginedMember.loginId);
System.out.printf("이름 : %s\n", Container.session.loginedMember.name);
}