package Main;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("==프로그램 시작==");
Scanner sc = new Scanner(System.in);
int lastArticleId = 0;
List<Article> articles = new ArrayList<>();
while (true) {
System.out.print("명령어 > ");
String command = sc.nextLine().trim();
if (command.length() == 0) {
System.out.println("명령어를 입력해주세요");
continue;
}
if (command.equals("exit")) {
break;
}
if (command.equals("article list")) {
if (articles.size() == 0) {
System.out.println("게시글이 없습니다");
} else {
System.out.println(" 번호 // 제목 ");
for (int i = articles.size() - 1; i >= 0; i--) {
Article article = articles.get(i);
System.out.printf(" %d // %s \n", article.id, article.title);
}
}
} else if (command.equals("article write")) {
int id = lastArticleId + 1;
System.out.print("제목 : ");
String title = sc.nextLine();
System.out.print("내용 : ");
String body = sc.nextLine();
Article article = new Article(id, title, body);
articles.add(article);
System.out.printf("%d번글이 생성되었습니다\n", id);
lastArticleId++;
} else if (command.startsWith("article detail ")) {
String[] cmdDiv = command.split(" ");
int id = Integer.parseInt(cmdDiv[2]);
boolean found = false;
for (int i = 0; i < articles.size(); i++) {
Article article = articles.get(i);
if (article.id == id) {
found = true;
break;
}
}
if (found == false) {
System.out.printf("%d번 게시물은 존재하지 않습니다\n", id);
} else {
System.out.printf("%d번 게시물 있던데?\n", id);
}
} else {
System.out.println("존재하지 않는 명령어입니다");
}
}
System.out.println("==프로그램 끝==");
sc.close();
}
}
class Article {
int id;
String title;
String body;
Article(int id, String title, String body) {
this.id = id;
this.title = title;
this.body = body;
}
}
article detail 의 명령어에 기능을 제대로 구현해보자.
startWith과 split Integer.parseInt 를 이용해 명령어 실행과 배열의 구분까지 가능해졌다
그럼 글이 존재하는지 안하는지 배열을 뒤져볼 필요가 있다 그 코드로는
for(int i = 0 ; i < articles.size(); i++) { // i 변수가 0부터 "articles.size()-1 까지 1씩 증가하며 Article객체들중에 id가 일치하는걸 찾는것
boolean found = false; // ㅊfound 변수를 false 라는 것을 기본값으로 넣어준다 boolean의 기본값으로 설정해 준것
Article article = articles.get(i); //각 반복마다 "articles" 리스트에서 "i"번째 인덱스에 해당하는 "Article" 객체를 가져와 "article" 변수에 저장한다다.
if(article.id == id) { //만약에 id값이 맞다 찾았다면
found = true; // found 에 true 값을 저장하고
break; // 반복문을 빠져나간다
}
}
if (found == false) { //못찾았으면
System.out.printf("%d번 게시물은 존재하지 않습니다\n",id); //못찾았다는 출력문을 보내고
}else{ //찾았다면
System.out.printf("%d번 게시물 있던데요?\n",id); // 찾았다는 출력을 보낸다
}
} else{ // 엉뚱한 명령어를 입력 했을 경우.
System.out.println("존재하지 않는 명령어 입니다");
}
위의 기능에서 핵심이 되는건 반복문의 추가다
"articles"라는 ArrayList(리스트)에 저장된 "Article" 객체들 중에서 "id"값이 일치하는 객체를 찾는 작업을 수행한다.
for문에서는 "i"변수가 0부터 "articles.size()-1"까지 1씩 증가하며 반복 실행된다. 각 반복마다 "articles" 리스트에서 "i"번째 인덱스에 해당하는 "Article" 객체를 가져와 "article" 변수에 저장한다.
그리고 if문에서는 현재 가져온 "article" 객체의 "id"값과 찾으려는 "id"값이 일치하는지 비교한다. 만약 일치한다면 "found"라는 변수에 true 값을 저장하고 반복문을 빠져나온다.
즉, 위 코드는 "articles" 리스트에서 "id"값이 일치하는 "Article" 객체를 찾고, 해당 객체가 존재하는지 여부를 "found" 변수에 저장하는 것이다.
'java > java 게시판 만들기' 카테고리의 다른 글
게시판 만들기 2일차 "게시물 삭제 기능 추가(index 활용)" (0) | 2023.03.30 |
---|---|
게시판 만들기 2일차 "게시물 작성시 날짜와 시간 정보 저장" (0) | 2023.03.30 |
게시판 만들기 2일차 "게시물 상세보기 구현중" (0) | 2023.03.17 |
게시판 만들기 2일차 "게시물 목록 기능 구현" (0) | 2023.03.15 |
게시판 만들기 2일차 "게시글의 유무 판별" (0) | 2023.03.15 |