- 검색기능 구현
- 검색관련 오류수정,게시물 상세페이지 명령어에서 숫자포멧관련 오류를 우아하게 처리
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
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.startsWith("article list ")) {
int page = 0;
try {
page = Integer.parseInt(command.split(" ")[2]);
} catch (NumberFormatException e) {
System.out.println("페이지 번호를 양의 정수로 입력해주세요.");
continue;
}
if (page <= 1) {
page = 1;
}
System.out.println("== 게시물 리스트 ==");
if (articlesSize() == 0) {
System.out.println("게시물이 존재하지 않습니다.");
continue;
}
System.out.println("번호 / 제목");
int itemsInAPage = 10;
int startPos = articlesSize() - 1;
startPos -= (page - 1) * itemsInAPage;
int endPos = startPos - (itemsInAPage - 1);
if (endPos < 0) {
endPos = 0;
}
if (startPos < 0) {
System.out.printf("%d페이지는 존재하지 않습니다.\n", page);
continue;
}
for (int i = startPos; i >= endPos; i--) {
Article article = articles[i];
System.out.printf("%d / %s\n", article.id, article.title);
}
} else if (command.startsWith("article detail ")) {
int inputedId = 0;
try {
inputedId = Integer.parseInt(command.split(" ")[2]);
} catch (NumberFormatException e) {
System.out.println("게시물 번호를 양의 정수로 입력해주세요.");
continue;
}
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 = 0;
try {
inputedId = Integer.parseInt(command.split(" ")[2]);
} catch (NumberFormatException e) {
System.out.println("게시물 번호를 양의 정수로 입력해주세요.");
continue;
}
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 = 0;
try {
inputedId = Integer.parseInt(command.split(" ")[2]);
} catch (NumberFormatException e) {
System.out.println("게시물 번호를 양의 정수로 입력해주세요.");
continue;
}
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[] commandBits = command.split(" ");
String searchKeyword = commandBits[2];
int page = 1;
try {
page = Integer.parseInt(commandBits[3]);
} catch (NumberFormatException e) {
System.out.println("페이지 번호를 양의 정수로 입력해주세요.");
continue;
}
if (commandBits.length >= 4) {
page = Integer.parseInt(commandBits[3]);
}
if (page <= 1) {
page = 1;
}
System.out.println("== 게시물 검색 ==");
int searchResultArticlesLen = 0;
for (Article article : articles) {
if (article == null) {
break;
}
if (article.title.contains(searchKeyword)) {
searchResultArticlesLen++;
}
}
Article[] searchResultArticles = new Article[searchResultArticlesLen];
int searchResultArticlesIndex = 0;
for (Article article : articles) {
if (article == null) {
break;
}
if (article.title.contains(searchKeyword)) {
searchResultArticles[searchResultArticlesIndex] = article;
searchResultArticlesIndex++;
}
}
if (searchResultArticles.length == 0) {
System.out.println("검색결과가 존재하지 않습니다.");
continue;
}
System.out.println("번호 / 제목");
int itemsInAPage = 10;
int startPos = searchResultArticles.length - 1;
startPos -= (page - 1) * itemsInAPage;
int endPos = startPos - (itemsInAPage - 1);
if (endPos < 0) {
endPos = 0;
}
if (startPos < 0) {
System.out.printf("%d페이지는 존재하지 않습니다.\n", page);
continue;
}
for (int i = startPos; i >= endPos; i--) {
Article article = searchResultArticles[i];
System.out.printf("%d / %s\n", article.id, article.title);
}
} else if (command.equals("system exit")) {
System.out.println("== 프로그램 종료 ==");
break;
} else {
System.out.println("== 존재하지 않는 명령어 입니다 ==");
}
}
sc.close();
}
}
|
cs |
반응형
'Java > Java log' 카테고리의 다른 글
2020-10-27 Java log - Member.java (0) | 2020.10.28 |
---|---|
2020-10-27 Java log (0) | 2020.10.28 |
2020-10-25 Java log (연습) (0) | 2020.10.25 |
2020-10-22 Java log - Main.java (0) | 2020.10.22 |
2020-10-22 Java log - App.java (0) | 2020.10.22 |