테스트데이터 생성 메서드 구현
Main 클래스
package com.KoreaIT.java.BasicAM;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static List<Article> articles;
static {
articles = new ArrayList<>();
}
public static void main(String[] args) {
System.out.println("==프로그램 시작==");
makeTestData();
// 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("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;
System.out.printf("제목 : ");
String regDate = Util.getNowDateStr();
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 = null;
for (int i = 0; i < articles.size(); i++) {
Article article = articles.get(i);
if (article.id == id) {
foundArticle = article;
break;
}
}
if (foundArticle == null) {
System.out.printf("%d번 게시물은 존재하지 않습니다.\n", id);
continue;
}
foundArticle.increaseHit(); //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 = null;
for (int i = 0; i < articles.size(); i++) {
Article article = articles.get(i);
if (article.id == id) {
foundArticle = article;
break;
}
}
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 = -1;
for (int i = 0; i < articles.size(); i++) {
Article article = articles.get(i);
if (article.id == id) {
foundIndex = i;
break;
}
}
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();
}
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));
}
// 테스트 데이터를 생성하기 위해 메서드를 구현
// 테스트 데이터를 articles에 담기 위해 아래 Article 클래스에 매개변수 7개를 사용하는 생성자 호출
}
class Article {
int id;
String regDate;
String updateDate;
String title;
String body;
int hit;
Article(int id, String regDate, String updateDate, String title, String body) {
this(id, regDate, updateDate, title, body, 0);
}
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;
}
// 조회수를 담기위해서 변수 hit 추가
void increaseHit() {
this.hit++;
}
// hit 1씩 증가(조회수)
}
리팩토링, 클래스와 패키지 정리, App 클래스로 로직 이전
Main 클래스
package com.KoreaIT.java.BasicAM;
public class Main {
public static void main(String[] args) {
new App().run();
}
}
// Main 클래스를 간소화시키기 위해서 App클래스를 생성해 run메서드 호출
// Main 클래스 간소화를 위해 Article클래스 생성
// 이때 Main 클래스에 있던 내용을 App클래스의 run메서드로 이전
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.util.Util;
public class App {
public static List<Article> articles;
static {
articles = new ArrayList<>();
}
// Java에서 Static 키워드를 사용한다는 것은 메모리에 한번 할당되어 프로그램이 종료될 때 해제되는 것을 의미 >> Static 변수는 클래스 변수로 객체를 생성하지 않고도 Static 자원에 접근이 가능
// Article 클래스의 리스트를 사용
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("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;
System.out.printf("제목 : ");
String regDate = Util.getNowDateStr();
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 = null;
for (int i = 0; i < articles.size(); i++) {
Article article = articles.get(i);
if (article.id == id) {
foundArticle = article;
break;
}
}
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 = null;
for (int i = 0; i < articles.size(); i++) {
Article article = articles.get(i);
if (article.id == id) {
foundArticle = article;
break;
}
}
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 = -1;
for (int i = 0; i < articles.size(); i++) {
Article article = articles.get(i);
if (article.id == id) {
foundIndex = i;
break;
}
}
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();
}
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));
}
}
Article 클래스
package com.KoreaIT.java.BasicAM.dto;
public class Article {
public int id;
public String regDate;
public String updateDate;
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++;
}
}
특정 게시물 찾는 메서드 구현, 중복 제거
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.util.Util;
public class App {
public static List<Article> articles;
static {
articles = 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("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;
System.out.printf("제목 : ");
String regDate = Util.getNowDateStr();
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);
// foundArticle이라는 공간에 getArticleById(id)를 넣어줌
// 여기서 id는 Integer.parseInt(commandBits[2])를 뜻함
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);
}
// foundIndex라는 공간에 getArticleIndexById(id)를 넣어줌
// 여기서 id는 Integer.parseInt(commandBits[2])를 뜻함
// public int getArticleIndexById(int id)에서 -1을 return 했다면 %d번 게시물은 존재하지 않습니다 출력
// 그렇지 않다면 articles.remove(foundIndex);를 통해 해당 id 정보 삭제
else {
System.out.println("존재하지 않는 명령어입니다");
}
}
System.out.println("==프로그램 끝==");
sc.close();
}
public int getArticleIndexById(int id) {
int i = 0;
for (Article article : articles) {
if (article.id == id) {
return i;
}
i++;
}
return -1;
}
// getArticleIndexById(int id)라는 메서드 생성
// article 배열을 순회, id는 Integer.parseInt(commandBits[2])
// id와 입력받은 id가 일치하다면 i를 return 그렇지 않다면 -1 return
public Article getArticleById(int id) {
// 방법 1
for (int i = 0; i < articles.size(); i++) {
// Article article = articles.get(i);
// if (article.id == id) {
// return article;
// }
// }
// 방법 2
// for (Article article : articles) {
// if (article.id == id) {
// return article;
// }
// }
// 방법 3(제일 간소화 시킨것 / 현재 사용중)
int index = getArticleIndexById(id);
if (index != -1) {
return articles.get(index);
}
return null;
}
// Article getArticleById(int id)라는 메서드 생성
// article 배열을 순회, id는 Integer.parseInt(commandBits[2])
// index 변수에 getArticleIndexById(id) 정보 넣어줌
// getArticleIndexById(id)에서 반환된 값이 -1이 아니라면 article리스트의 index변수에 대입된 값(id)번째 정보를 반환
// 반환된 값이 -1이라면 null return
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));
}
}