본문 바로가기
IT관련/JAVA_자유 노트

[수업창작]게시판만들기 20-05-17

by XoX 2020. 5. 17.
728x90
개인적인 메모를 위해 작성된 글이므로 가벼운 참고용으로 봐주세요.

 

import java.util.Scanner;
import java.util.*;
import java.text.*;

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;
	void setScanner(Scanner sc) {
		this.scanner = sc;
	}
	int articleLastId ;
	int articleLastIndex;
	Article[] articleArr;
	int articleCount;
	Board(){
		articleLastId = 0;
		articleLastIndex = -1;
		articleArr = new Article[100];
	}
	void start() {
		commandHelp();
		while(true) {
			System.out.print("명령어입력 >>> ");
			String command = scanner.next().trim();
			if(command.equals("exit")) {
				scanner.nextLine();
				System.out.print("===게시판을 종료합니다.===");
				break;
			} else if(command.equals("add")) {
				scanner.nextLine();
				commandAdd();
			} else if(command.equals("list")) {
				scanner.nextLine();
				commandList();
			} else if(command.equals("detail")) {
				int detailIn = scanner.nextInt();
				scanner.nextLine();
				commandDetail(detailIn);
			} else if(command.equals("modify")) {
				int modifyIn = scanner.nextInt();
				scanner.nextLine();
				commandModify(modifyIn);
			} else if(command.equals("like")) {
				int likeIn = scanner.nextInt();
				scanner.nextLine();
				commandLike(likeIn);
			} else if(command.equals("delete")) {
				int deleteIn = scanner.nextInt();
				scanner.nextLine();
				commandDelete(deleteIn);
			} 
			
			
			
			
			 else if(command.length() > 0) {
				scanner.nextLine();
				System.out.printf("'%s'라는 명령어를 찾을 수 없습니다.\n",command);
			} 	
		}
	}
	void commandHelp() {
		System.out.println("===명령어 리스트===");
		System.out.println("help");
		System.out.println("list");
		System.out.println("add");
		System.out.println("detail + 번호");
		System.out.println("modify");
		System.out.println("delete");
		System.out.println("like");
		System.out.println("member");
		System.out.println("logIn");
		System.out.println("logOut");
		System.out.println("exit");
	}
	String getDate() {
		SimpleDateFormat formatter = new SimpleDateFormat ( "yyyy.MM.dd HH:mm:ss", Locale.KOREA );
		Date currentTime = new Date ( );
		String dTime = formatter.format ( currentTime ); 
		return dTime;
	}
	void commandAdd() {
		System.out.print("=게시물추가=\n");
		Article article = new Article();
		articleLastId++;
		System.out.print("제목: ");
		String title = scanner.nextLine();
		System.out.print("내용: ");
		String body = scanner.nextLine();
		article.title= title;
		article.body=body;
		article.date = getDate();
		article.id=articleLastId;
		System.out.printf("%d번 게시물이 추가 되었습니다.\n", article.id);
		articleLastIndex ++;
		articleArr[articleLastIndex] = article ;
	}
	void commandList() {
		System.out.print("번호|날짜               |제목\n");
		for(int i=0; i<=articleLastIndex; i++) {
			System.out.printf("%d|%s |%s\n",articleArr[i].id,articleArr[i].date,articleArr[i].title);
		}
	}
	Article getArticleById(int num){
		for(int i=0; i<=articleLastIndex; i++) {
			if(articleArr[i].id == num) {
				return articleArr[i];
			}
		}
		return null;
	}
	void commandDetail(int num) {
		Article temp = getArticleById(num);
		if(temp==null) {
			System.out.print("게시물이 존재하지 않습니다.\n");
		}
		else {
			System.out.print("=상세히보기=\n");
			System.out.printf("번호: %s\n", temp.id);
			System.out.printf("날짜: %s\n", temp.date);
			System.out.printf("제목: %s\n", temp.title);
			System.out.printf("내용: %s\n", temp.body);
			temp.articleCount ++;
			temp.articleCount = temp.articleCount;
			System.out.printf("조회수:%d\n",temp.articleCount);
			System.out.printf("추천수:%d\n",temp.articleLikeCount);
		}
	}
	void commandModify(int num) {
		Article temp = getArticleById(num);
		if(temp==null) {
			System.out.print("게시물이 존재하지 않습니다.\n");
		}
		else {
			System.out.print("제목: ");
			temp.title = scanner.nextLine();
			System.out.print("내용: ");
			temp.body = scanner.nextLine();
		}
	}
	void commandLike(int num) {
		Article temp = getArticleById(num);
		
		if(temp==null) {
			System.out.print("게시물이 존재하지 않습니다.\n");
		}
		else {
			System.out.printf("%d번 게시물을 추천합니다.\n", temp.id);
			temp.articleLikeCount ++;
			temp.articleLikeCount = temp.articleLikeCount;
		}
	}
	void commandDelete(int num) {
		int index = -1;
		for(int i=0; i<=articleLastIndex; i++) {
			if(articleArr[i].id==num) {
				index = i;
				break;
			}
		}
		if(index == -1) {
			System.out.println("게시물이 존재하지 않습니다.");
		}
		else {
			for(int i=index; i<=articleLastIndex-1; i++) {
				articleArr[i]=articleArr[i+1];
			}
			articleLastIndex --;
		} 
	}
}

class Article{
	int id;
	String date;
	String title;
	String body;
	int articleLikeCount;
	int articleCount;
}
728x90
반응형

댓글