본문 바로가기

학습계획

-한만큼

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

/*= 명령어 리스트 ==
help : 명령어 리스트
list : 게시물 리스팅
add : 게시물 추가
exit : 종료
detail ${게시물 번호} : 게시물 상세보기
게시판) list[엔터]
== 게시물 리스트 ==
게시물이 존재하지 않습니다.
게시판) add[엔터]
== 게시물 추가 ==
제목 : 1번글 제목입니다.[엔터]
내용 : 1번글 내용입니다.[엔터]
1번 글이 생성되었습니다.
게시판) list[엔터]
== 게시물 리스트 ==
번호 | 날짜                | 제목
1    | 2020-05-06 12:12:12 | 1번글 제목입니다.
게시판) detail 2
== 게시물 상세 ==
해당 게시물은 존재하지 않습니다.
게시판) detail 1
== 게시물 상세 ==
번호 : 1
날짜 : 2020-05-06 12:12:12
제목 : 1번글 제목입니다.
내용 : 1번글 내용입니다.
게시판) exit
== 게시판 종료 ==*/
public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		Board board = new Board();

		board.setScanner(scanner);

		board.start();

		scanner.close();

	}
}

class Board {
	Scanner scanner;

	Article[] articles;

	int articlesLastIndex;

	Board() {
		articles = new Article[100];
		articlesLastIndex = -1;
	}

	int getArticlesCount() {
		return articlesLastIndex + 1;
	}

	void setScanner(Scanner scanner) {
		this.scanner = scanner;
	}

	void start() {

		showHelp();

		while (true) {
			System.out.printf("\n게시판) ");
			String command = scanner.next().trim();

			if (command.equals("help")) {
				showHelp();

			}

			else if (command.equals("list")) {
				scanner.nextLine();
				doCommandList();
			}

			else if (command.equals("add")) {
				scanner.nextLine();
				doCommandAdd();
			}

			else if (command.equals("exit")) {
				scanner.nextLine();
				doCommandExit();
				break;
			}

			else if (command.equals("detail ${게시물번호}")) {
				int idToDetail = scanner.nextInt();
				scanner.nextLine();
				doCommandDetail(idToDetail);
			} else {
				scanner.nextLine();
				System.out.println("일치하는 명령어가 없습니다.");
			}
		}
	}

	void doCommandDetail(int num) {
		System.out.println("== 게시물 상세 ==");
		Article article = new Article();

		for (int i = articlesLastIndex; i >= 0; i--) {
			if (i > articlesLastIndex) {
				System.out.println("해당 게시물은 존재하지 않습니다.\n");
			} else if (i == num) {
				articles[i].id = i;
				article = articles[i];

				System.out.printf("번호 : %d%n", article.id);
				System.out.printf("날짜 : %s%n", article.regDate);
				System.out.printf("제목 : %s%n", article.title);
				System.out.printf("내용 : %s%n", article.body);

			}
		}
	}

	void doCommandExit() {
		System.out.println("== 게시판 종료 ==");

	}

	void doCommandAdd() {
		Article article = new Article();

		Article lastArticle = null;

		if (articlesLastIndex >= 0) {
			lastArticle = articles[articlesLastIndex];
		}

		int newId;

		if (lastArticle == null) {
			newId = 1;
		} else {
			newId = lastArticle.id + 1;
		}

		article.id = newId;
		article.regDate = getNowDateStr();
		System.out.print("제목 : ");
		article.title = scanner.nextLine();
		System.out.print("내용 : ");
		article.body = scanner.nextLine();

		int articlesNewIndex = articlesLastIndex + 1;
		articles[articlesNewIndex] = article;

		System.out.printf("%d번 글이 생성되었습니다.\n", article.id);

		articlesLastIndex++;
	}

	void doCommandList() {
		Article article;
		if (articlesLastIndex < 0) {
			System.out.println("== 게시물 리스트 ==");
			System.out.println("게시물이 존재하지 않습니다.");
		} else {
			System.out.println("번호 | 날짜 \t\t| 제목\n");

			for (int i = 0; i < articlesLastIndex + 1; i++) {
				article = articles[i];
				if (articlesLastIndex >= 0) {
					System.out.printf(" %d | %s|%s\n", article.id, article.regDate, article.title);
				}
			}
		}
	}

	void showHelp() {
		System.out.printf("= 명령어 리스트 ==\r\n" + "help : 명령어 리스트\r\n" + "list : 게시물 리스팅\r\n" + "add : 게시물 추가\r\n"
				+ "exit : 종료\r\n" + "detail ${게시물 번호} : 게시물 상세보기\n");

	}

	String getNowDateStr() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat Date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = Date.format(cal.getTime());
		return dateStr;
	}

	Article getLastArticle() {
		if (getArticlesCount() > 0) {
			return articles[articlesLastIndex];
		}
		return null;
	}

	Article getArticleById(int id) {
		for (int i = 0; i <= articlesLastIndex; i++) {
			if (articles[i].id == id) {
				return articles[i];
			}
		}

		return null;
	}

}

class Article {
	int id;
	String regDate;
	String title;
	String body;
}

'학습계획' 카테고리의 다른 글

2020.05.19  (0) 2020.05.19
2020 05 11 학습계획  (0) 2020.05.11
2020.04.29 학습계획  (0) 2020.04.29
2020.04.27 학습계획  (0) 2020.04.27
2020. 04. 24 학습계획  (0) 2020.04.24