Dto 클래스 생성, 상속
Article 클래스
package com.KoreaIT.java.BasicAM.dto;
public class Article extends Dto {
public String title;
public String body;
public int hit;
public Article(int id, String regDate, String updateDate, String title, String body) {
this(id, regDate, updateDate, title, body, 0);
}
public Article(int id, String regDate, String updateDate, String title, String body, int hit) {
this.id = id;
this.regDate = regDate;
this.updateDate = updateDate;
this.title = title;
this.body = body;
this.hit = hit;
}
public void increaseHit() {
this.hit++;
}
}
// Dto 클래스에 있는 변수를 사용하기 위해 extends 해줌
Dto 클래스
package com.KoreaIT.java.BasicAM.dto;
public class Dto {
public int id;
public String regDate;
public String updateDate;
}
// Article클래스와 Member클래스에서 공통으로 사용하는 변수를 Dto 클래스로 이전해주었다.
Member 클래스
package com.KoreaIT.java.BasicAM.dto;
public class Member extends Dto {
public String loginId;
public String loginPw;
public String name;
public Member(int id, String regDate, String updateDate, String loginId, String loginPw, String name) {
this.id = id;
this.regDate = regDate;
this.updateDate = updateDate;
this.loginId = loginId;
this.loginPw = loginPw;
this.name = name;
}
}
// Dto 클래스에 있는 변수를 사용하기 위해 extends 해줌
회원가입 기능 구현, 비밀번호 체크, 아이디 중복 검사
App 클래스
package com.KoreaIT.java.BasicAM;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.KoreaIT.java.BasicAM.dto.Article;
import com.KoreaIT.java.BasicAM.dto.Member;
import com.KoreaIT.java.BasicAM.util.Util;
public class App {
public static List<Article> articles;
public static List<Member> members;
// Article리스트와 Member리스트를 사용
static {
articles = new ArrayList<>();
members = new ArrayList<>();
}
public void run() {
System.out.println("==프로그램 시작==");
makeTestData();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.printf("명령어 ) ");
String command = sc.nextLine().trim();
if (command.length() == 0) {
System.out.println("명령어를 입력해주세요");
continue;
}
if (command.equals("system exit")) {
break;
}
if (command.equals("member join")) {
int id = members.size() + 1;
String regDate = Util.getNowDateStr();
String loginId = null;
while (true) {
System.out.printf("로그인 아이디 : ");
loginId = sc.nextLine(); // 입력받은 내용을 loginId에 저장
if (isJoinableLoginId(loginId) == false) {
System.out.println("이미 사용중인 아이디입니다.");
continue;
} //isJoinableLoginId(loginId)에서 false가 반환 되었다면 "이미 사용중인 아이디입니다." 출력
break;
}
String loginPw = null;
String loginPwConfirm = null;
while (true) {
System.out.printf("로그인 비밀번호 : ");
loginPw = sc.nextLine(); //입력받은 내용을 loginPw에 저장
System.out.printf("로그인 비밀번호 확인: ");
loginPwConfirm = sc.nextLine(); //입력받은 내용을 loginPwConfirm에 저장
if (loginPw.equals(loginPwConfirm) == false) {
System.out.println("비밀번호를 다시 입력해주세요");
continue;
} //loginPw가 loginPwConfirm과 같지 않다면 "비밀번호를 다시 입력해주세요" 출력
break;
}
System.out.printf("이름 : ");
String name = sc.nextLine(); //입력받은 내용을 name에 저장
Member member = new Member(id, regDate, regDate, loginId, loginPw, name);
members.add(member); //member 리스트에 추가
System.out.printf("%d번 회원이 가입 되었습니다\n", id);
} else if (command.equals("article list")) {
if (articles.size() == 0) {
System.out.println("게시글이 없습니다");
continue;
}
System.out.println("번호 / 제목 / 조회 ");
String tempTitle = null;
for (int i = articles.size() - 1; i >= 0; i--) {
Article article = articles.get(i);
if (article.title.length() > 4) {
tempTitle = article.title.substring(0, 4);
System.out.printf("%4d / %6s / %4d\n", article.id, tempTitle + "...", article.hit);
continue;
}
System.out.printf("%4d / %6s / %4d\n", article.id, article.title, article.hit);
}
} else if (command.equals("article write")) {
int id = articles.size() + 1;
String regDate = Util.getNowDateStr();
System.out.printf("제목 : ");
String title = sc.nextLine();
System.out.printf("내용 : ");
String body = sc.nextLine();
Article article = new Article(id, regDate, regDate, title, body);
articles.add(article);
System.out.printf("%d번 글이 생성 되었습니다\n", id);
} else if (command.startsWith("article detail ")) {
String[] commandBits = command.split(" ");
int id = Integer.parseInt(commandBits[2]);
Article foundArticle = getArticleById(id);
if (foundArticle == null) {
System.out.printf("%d번 게시물은 존재하지 않습니다.\n", id);
continue;
}
foundArticle.increaseHit();
System.out.printf("번호 : %d\n", foundArticle.id);
System.out.printf("작성날짜 : %s\n", foundArticle.regDate);
System.out.printf("수정날짜 : %s\n", foundArticle.updateDate);
System.out.printf("제목 : %s\n", foundArticle.title);
System.out.printf("내용 : %s\n", foundArticle.body);
System.out.printf("조회 : %d\n", foundArticle.hit);
} else if (command.startsWith("article modify ")) {
String[] commandBits = command.split(" ");
int id = Integer.parseInt(commandBits[2]);
Article foundArticle = getArticleById(id);
if (foundArticle == null) {
System.out.printf("%d번 게시물은 존재하지 않습니다.\n", id);
continue;
}
System.out.printf("제목 : ");
String title = sc.nextLine();
System.out.printf("내용 : ");
String body = sc.nextLine();
String updateDate = Util.getNowDateStr();
foundArticle.title = title;
foundArticle.body = body;
foundArticle.updateDate = updateDate;
System.out.printf("%d번 게시물을 수정했습니다\n", id);
} else if (command.startsWith("article delete ")) {
String[] commandBits = command.split(" ");
int id = Integer.parseInt(commandBits[2]);
int foundIndex = getArticleIndexById(id);
if (foundIndex == -1) {
System.out.printf("%d번 게시물은 존재하지 않습니다.\n", id);
continue;
}
articles.remove(foundIndex);
System.out.printf("%d번 게시물을 삭제했습니다\n", id);
}
else {
System.out.println("존재하지 않는 명령어입니다");
}
}
System.out.println("==프로그램 끝==");
sc.close();
}
private boolean isJoinableLoginId(String loginId) {
int index = getMemberIndexByLoginId(loginId);
// getMemberIndexByLoginId(loginId)에서 반환받은 값 index에 저장
if (index == -1) { // 그 값이 -1과 같다면 true 반환
return true;
}
return false; // 그렇지 않다면 false 반환
}
public int getMemberIndexByLoginId(String loginId) {
int i = 0;
for (Member member : members) { //member리스트 순회
if (member.loginId.equals(loginId)) { //member의 loginId와 loginId가 같다면 i반환 >> equals에 있는 loginId는 getMemberIndexByLoginId(String loginId)의 loginId
return i;
}
i++;
}
return -1; // 그렇지 않다면 -1 반환
}
public int getArticleIndexById(int id) {
int i = 0;
for (Article article : articles) { // article 리스트 순회
if (article.id == id) { //article의 id와 id가 같다면 i값 반환 >> 후에 있는 id는 getArticleIndexById(int id)의 id
return i;
}
i++;
}
return -1; // 그렇지 않다면 -1 반환
}
public Article getArticleById(int id) {
int index = getArticleIndexById(id);
// getArticleIndexById(id)에서 반환받은 값 index에 저장
if (index != -1) { //반환받은 값이 -1이 아니라면
return articles.get(index); //article 리스트의 index번째값 반환
}
return null; // 그렇지 않다면 null
}
public static void makeTestData() {
System.out.println("테스트를 위한 데이터를 생성합니다");
articles.add(new Article(1, Util.getNowDateStr(), Util.getNowDateStr(), "제목1", "내용1", 11));
articles.add(new Article(2, Util.getNowDateStr(), Util.getNowDateStr(), "제목2", "내용2", 22));
articles.add(new Article(3, Util.getNowDateStr(), Util.getNowDateStr(), "제목3", "내용3", 33));
}
}