百聞 不如一打 [백문이 불여일타]

백번 묻는것 보다 한번 쳐보는게 낫다

Java/Java log

2020-10-20 Java log

qbj700 2020. 10. 20. 17:12

- 0번 게시물 상세보기 시 종료되는 버그 수정

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
package com.sbs.example.easytextboard;
 
import java.util.Scanner;
 
public class App {
 
    Article[] articles = new Article[5];
    int lastArticleId = 0;
 
    Article getArticle(int id) {
        if (id < 1) {
            return null;
        }
        if (id > lastArticleId) {
            return null;
        }
        return articles[id - 1];
    }
 
    public void run() {
        for (int i = 0; i < articles.length; i++) {
            articles[i] = new Article();
        }
 
        Scanner sc = new Scanner(System.in);
 
        int maxArticlesConunt = articles.length;
 
        while (true) {
 
            System.out.printf("명령어 입력 :");
            String command = sc.nextLine();
 
            if (command.equals("article add")) {
                System.out.println("== 게시물 등록 ==");
 
                if (lastArticleId >= maxArticlesConunt) {
                    System.out.println("더 이상 생성할 수 없습니다.");
                    continue;
                }
 
                int id = lastArticleId + 1;
                lastArticleId = id;
 
                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);
 
                Article article = getArticle(id);
 
                article.id = id;
                article.title = title;
                article.body = body;
 
                System.out.printf("%d번 게시물이 등록되었습니다.\n", id);
            } else if (command.equals("article list")) {
                System.out.println("== 게시물 리스트 ==");
 
                if (lastArticleId == 0) {
                    System.out.println("게시물이 존재하지 않습니다.");
                    continue;
                }
                System.out.println("번호 / 제목");
 
                for (int i = 1; i <= lastArticleId; i++) {
                    Article article = getArticle(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.equals("system exit")) {
                System.out.println("== 프로그램 종료 ==");
                break;
            } else {
                System.out.println("== 존재하지 않는 명령어 입니다. ==");
            }
        }
        sc.close();
    }
}
cs
반응형

'Java > Java log' 카테고리의 다른 글

2020-10-22 Java log - App.java  (0) 2020.10.22
2020-10-21 Java log  (0) 2020.10.21
2020-10-19 Java log  (0) 2020.10.19
2020-10-16 Java log  (0) 2020.10.17
2020-10-15 Java log - Article.java  (0) 2020.10.15