- 게시물 최대 개수 제한 해제
- 객체 초기화 로직을 init으로 분리, 디폴트 테스트 게시물 32개로 지정
- 생성자 메서드 문법으로 init 함수 제거
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | package com.sbs.example.easytextboard; import java.util.Scanner; public class App { private Article[] articles; private int lastArticleId; private int articlesSize; public App() { articles = new Article[32]; lastArticleId = 0; articlesSize = 0; for (int i = 0; i < 32; i++) { add("제목" + (i + 1), "내용" + (i + 1)); } } // 현재 게시물 갯수 private int articlesSize() { return articlesSize; } // 게시물에 배열 번호 부여 private Article getArticle(int id) { int index = getIndexById(id); if (index == -1) { return null; } return articles[index]; } // 입력된 id가 articles[].id 와 일치하는지 여부 확인 private int getIndexById(int id) { for (int i = 0; i < articlesSize(); i++) { if (articles[i].id == id) { return i; } } return -1; } // 게시물 삭제 함수 private void remove(int id) { int index = getIndexById(id); if (index == -1) { return; } for (int i = index + 1; i < articlesSize(); i++) { articles[i - 1] = articles[i]; } articlesSize--; } // 게시물 생성 함수 private int add(String title, String body) { if (isArticlesFull()) { System.out.printf("== 배열 사이즈 증가 (%d => %d) ==\n", articles.length, articles.length * 2); Article[] newArticles = new Article[articles.length * 2]; for (int i = 0; i < articles.length; i++) { newArticles[i] = articles[i]; } articles = newArticles; } Article article = new Article(); article.id = lastArticleId + 1; article.title = title; article.body = body; articles[articlesSize] = article; articlesSize++; lastArticleId = article.id; return article.id; } // 게시물 수정 함수 private void modify(int inputedId, String title, String body) { Article article = getArticle(inputedId); article.title = title; article.body = body; } // 게시물이 꽉 찻는지 확인하는 함수 private boolean isArticlesFull() { return articlesSize == articles.length; } public void run() { Scanner sc = new Scanner(System.in); while (true) { System.out.printf("명령어 입력 :"); String command = sc.nextLine(); if (command.equals("article add")) { System.out.println("== 게시물 등록 =="); System.out.printf("제목 :"); String title = sc.nextLine(); System.out.printf("내용 :"); String body = sc.nextLine(); System.out.println("== 게시물 등록 결과 =="); System.out.printf("제목 :%s\n", title); System.out.printf("내용 :%s\n", body); int id = add(title, body); System.out.printf("%d번 게시물이 생성되었습니다.\n", id); } else if (command.equals("article list")) { System.out.println("== 게시물 리스트 =="); if (articlesSize() == 0) { System.out.println("게시물이 존재하지 않습니다."); continue; } System.out.println("번호 / 제목"); for (int i = articlesSize() - 1; i >= 0; i--) { Article article = articles[i]; System.out.printf("%d / %s\n", article.id, article.title); } } else if (command.startsWith("article detail ")) { int inputedId = Integer.parseInt(command.split(" ")[2]); System.out.println("== 게시물 상세 =="); Article article = getArticle(inputedId); if (article == null) { System.out.printf("%d번 게시물은 존재하지 않습니다.\n", inputedId); continue; } System.out.printf("번호 : %d\n", article.id); System.out.printf("제목 : %s\n", article.title); System.out.printf("내용 : %s\n", article.body); } else if (command.startsWith("article delete ")) { int inputedId = Integer.parseInt(command.split(" ")[2]); System.out.println("== 게시물 삭제 =="); Article article = getArticle(inputedId); if (article == null) { System.out.printf("%d번 게시물은 존재하지 않습니다.\n", inputedId); continue; } remove(inputedId); System.out.printf("%d번 게시물이 삭제되었습니다.\n", inputedId); } else if (command.startsWith("article modify ")) { int inputedId = Integer.parseInt(command.split(" ")[2]); System.out.println("== 게시물 수정 =="); Article article = getArticle(inputedId); if (article == null) { System.out.printf("%d번 게시물은 존재하지 않습니다.\n", inputedId); continue; } else { System.out.printf("수정할 제목 :"); String title = sc.nextLine(); System.out.printf("수정할 내용 :"); String body = sc.nextLine(); modify(inputedId, title, body); System.out.printf("%d번 게시물이 수정되었습니다.\n", inputedId); } } else if (command.startsWith("article search ")) { String articleSearch = command.split(" ")[2]; System.out.println("== 게시물 검색 =="); System.out.println("번호 / 제목"); for (int i = 0; i < articlesSize; i++) { if (articles[i].title.contains(articleSearch)) { System.out.printf("%d / %s\n", i + 1, articles[i].title); } } } else if (command.equals("system exit")) { System.out.println("== 프로그램 종료 =="); break; } else { System.out.println("== 존재하지 않는 명령어 입니다 =="); } } sc.close(); } } | cs |
반응형
'Java > Java log' 카테고리의 다른 글
2020-10-25 Java log (연습) (0) | 2020.10.25 |
---|---|
2020-10-22 Java log - Main.java (0) | 2020.10.22 |
2020-10-21 Java log (0) | 2020.10.21 |
2020-10-20 Java log (0) | 2020.10.20 |
2020-10-19 Java log (0) | 2020.10.19 |