728x90
기존 구현기능 : 회원가입, 로그인, 로그아웃
추가 구현기능 : 회원리스트
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Scanner;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
DB db = new DB();
MemberDao memberDao = new MemberDao(db);
MemberService memberService = new MemberService(memberDao);
MemberController memberController = new MemberController(scanner, memberService);
help();
while(true) {
System.out.printf("명령어) ${} ");
String command = scanner.nextLine();
if(command.equals("1")) {
help();
}
else if(command.equals("2")) {
memberController.doCommandJoin();
}
else if(command.equals("3")) {
memberController.doCommandLogin();
}
else if(command.equals("4")) {
memberController.doCommandLogout();
}
else if(command.equals("5")) {
memberController.doCommandMemberList();
}
else if(command.equals("8")) {
System.out.println("===게시판종료===");
break;
}
else {
System.out.printf("'%s'는 존재하지 않는 명령어 입니다. 다시 입력하세요.\n", command);
continue;
}
}
}
static void help() {
System.out.println("===게시판시작===");
System.out.println("<명령어 목록>");
System.out.println("1) help");
System.out.println("2) join");
System.out.println("3) login");
System.out.println("4) logout");
System.out.println("5) MemberList");
System.out.println("8) exit");
}
}
class MemberController{
Member loginedMember;
String userName;
String userId;
String userPw;
Scanner scanner;
MemberService memberService;
MemberController(Scanner scanner, MemberService memberService) {
this.scanner = scanner;
this.memberService = memberService;
}
void doCommandMemberList() {
System.out.println("번호 | 이름 | 아이디");
memberService.printMemberList();
}
void doCommandLogout() {
if(loginedMember==null) {
System.out.println("이미 로그아웃된 상태입니다.");
} else {
System.out.printf("로그아웃하시겠습니까(y/n)? ");
String LogoutCheck = scanner.nextLine();
if(LogoutCheck.equals("y")) {
loginedMember=null;
System.out.println("정상적으로 로그아웃 됐습니다.");
}
else if(LogoutCheck.equals("n")) {
System.out.println("로그아웃을 취소합니다.");
return;
}
else {
System.out.println("잘못된 명령어입니다.");
}
}
}
void doCommandLogin() {
if(loginedMember==null
) {
System.out.printf("로그인 아이디: ");
String loginId = scanner.nextLine();
System.out.printf("로그인 비밀번호: ");
String loginPw = scanner.nextLine();
loginedMember = memberService.CheckJoinedMember(loginId, loginPw);
if(loginedMember != null) {
System.out.println("로그인 성공");
}
if(loginedMember == null) {
System.out.println("로그인 실패. 일치하는 정보가 없습니다.");
}
}
else{
System.out.println("이미 로그인되셨습니다.");
}
}
void doCommandJoin() {
if(loginedMember==null) {
System.out.println("+ 회원가입 시작 +");
while(true) {
System.out.printf("이름: ");
userName = scanner.nextLine();
if(userName.length()<2) {
System.out.println("이름은 2자 이상이어야 합니다.");
continue;
}
break;
}
while(true) {
System.out.printf("아이디: ");
userId = scanner.nextLine();
if(userId.length()<2) {
System.out.println("아이디는 2자 이상이어야 합니다.");
continue;
}
if(memberService.isUsedUserId(userId)) {
continue;
}
break;
}
while(true) {
boolean PwValid = true;
while(true) {
System.out.printf("비밀번호: ");
userPw = scanner.nextLine();
if(userPw.length()<2) {
System.out.println("비밀번호는 2자 이상이어야 합니다.");
continue;
}
break;
}
while(true) {
System.out.printf("비밀번호 확인: ");
String userPwCheck = scanner.nextLine();
if(userPwCheck.equals(userPw)==false) {
System.out.println("일치하지 않습니다. 다시 입력해주세요.");
PwValid = false;
break;
}
break;
}
if(PwValid) {
break;
}
}
memberService.join(userName, userId, userPw);
}
else {
System.out.println("로그아웃을 먼저 진행하세요.");
}
}
}
class MemberService{
MemberDao memberDao;
MemberService(MemberDao memberDao) {
this.memberDao = memberDao;
}
void printMemberList() {
memberDao.printMemberList();
}
Member CheckJoinedMember(String loginId, String loginPw) {
Member member = memberDao.getJoinedMember(loginId, loginPw);
return member;
}
boolean isUsedUserId(String userId) {
Member member = memberDao.getMemberByUserId(userId);
if(member==null) {
return false;
}
else {
System.out.println("이미 가입된 아이디입니다. 다시 입력해주세요.");
return true;
}
}
void join(String userName, String userId, String userPw) {
if(isUsedUserId(userId)==false) {
memberDao.add(userName, userId, userPw);
}
}
}
class Member{
public int id;
public String regDate;
public String userName;
public String userId;
public String userPw;
public Member() {
super();
}
public Member(int id, String regDate, String userName, String userId, String userPw) {
super();
this.id = id;
this.regDate = regDate;
this.userName = userName;
this.userId = userId;
this.userPw = userPw;
}
}
class MemberDao{
DB db;
MemberDao(DB db) {
super();
this.db = db;
}
void printMemberList() {
db.printMemberList();
}
Member getJoinedMember(String loginId, String loginPw) {
return db.getJoinedMember(loginId, loginPw);
}
void add(String userName, String userId, String userPw) {
System.out.println("새로운 회원의 정보를 저장합니다.");
int id = getLastMemberId()+1;
String regDate = Util.getNowDate();
Member member = new Member(id, regDate, userName, userId, userPw);
db.add(member);
setNewLastMemberId(id);
}
void setNewLastMemberId(int id) {
db.setNewLastMemberId(id);
}
Member getMemberByUserId(String userId) {
return db.getMemberByUserId(userId);
}
int getLastMemberId() {
return db.getLastMemberId();
}
}
class DB{
void printMemberList() {
List<Member> members = getMembers();
for(Member member : members) {
System.out.printf(" %d | %s | %s\n", member.id, member.userName,member.userId);
}
}
Member getJoinedMember(String loginId, String loginPw) {
List<Member> members = getMembers();
for(Member member : members) {
if(member.userId.equals(loginId)) {
if(member.userPw.equals(loginPw)) {
return member;
}
}
}
return null;
}
Member getMemberByUserId(String joinUserId) {
List<Member> members = getMembers();
for( Member member : members) {
if(member.userId.equals(joinUserId)) {
return member;
}
}
return null;
}
List<Member> getMembers() {
List<Member> members = new ArrayList<>();
for(int i = 1; i<=getLastMemberId(); i++) {
String filePath = "member/"+ i + ".txt";
Member member = readMemberFromJSonFile(filePath);
if(member !=null) {
members.add(member);
}
}
return members;
}
Member readMemberFromJSonFile(String filePath) {
ObjectMapper mapper = new ObjectMapper();
Member member = null;
try {
member = mapper.readValue(new File(filePath), Member.class);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return member;
}
int getLastMemberId(){
int lastId = 0 ;
String filePath = "member/lastId.txt";
if(isFileExist(filePath)) {
lastId = Integer.parseInt(getFileContents(filePath));
} else {
lastId = 0;
writeFileContents(filePath, lastId + "");
}
return lastId;
}
boolean isFileExist(String filePath) {
File f = new File(filePath);
if(f.isFile()) {
return true;
} else {
return false;
}
}
String getFileContents(String filePath) {
String lastIdStr = null;
try {
FileInputStream fileStream = null;
fileStream = new FileInputStream(filePath);
byte[ ] readBuffer = new byte[fileStream.available()];
while (fileStream.read( readBuffer ) != -1){}
lastIdStr = new String(readBuffer);
fileStream.close();
} catch (Exception e) {
e.getStackTrace();
}
return lastIdStr;
}
void writeFileContents(String filePath, String lastId) {
BufferedOutputStream bs = null;
try {
bs = new BufferedOutputStream(new FileOutputStream(filePath));
bs.write(lastId.getBytes());
} catch (Exception e) {
e.getStackTrace();
}finally {
try {
bs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
void setNewLastMemberId(int NewLastMemberId) {
String filePath = "member/lastId.txt";
writeFileContents(filePath, NewLastMemberId+"");
}
void add(Member member) {
String filePath = "member/"+member.id+".txt";
ObjectMapper mapper = new ObjectMapper();
try {
mapper.writeValue(new File(filePath), member);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Util {
static String getNowDate() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat Date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = Date.format(cal.getTime());
return dateStr;
}
}
728x90
반응형
'IT관련 > JAVA_자유 노트' 카테고리의 다른 글
MailService 코드 (0) | 2020.07.22 |
---|---|
[수업창작] 20-05-30 (0) | 2020.05.30 |
PHP로 게시판 만들기 (0) | 2020.05.26 |
txt파일 저장/불러오기/존재유무 코드 (0) | 2020.05.25 |
[수업창작] 20-05-23(MVC구조 회원가입) (0) | 2020.05.23 |
댓글