Java 게시판 #4일차

김신하·2023년 2월 14일
post-thumbnail

게시물 삭제, 수정, 조회수 기능 구현

package com.KoreaIT.java.BasicAM;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
	System.out.println("==프로그램 시작==");

	Scanner sc = new Scanner(System.in);

	int lastArticleId = 0; 

	List<Article> articles = new ArrayList<>();

	while (true) {

		System.out.printf("명령어 ) ");
		String command = sc.nextLine().trim();

		if (command.length() == 0) {
			System.out.println("명령어를 입력해주세요");
			continue;
		}

		if (command.equals("system exit")) {
			break;
		}

		if (command.equals("article list")) {
			if (articles.size() == 0) {
				System.out.println("게시글이 없습니다");
				continue;
			}
			System.out.println("번호    /      제목     /     조회    ");
			String tempTitle = null;
			for (int i = articles.size() - 1; i >= 0; i--) {
				Article article = articles.get(i);
				if (article.title.length() > 4) {
					tempTitle = article.title.substring(0, 4);
					System.out.printf("%4d	/    %6s    /   %4d\n", article.id, tempTitle + "...", article.hit);
					continue;
				}

				System.out.printf("%4d	/    %6s    /   %4d\n", article.id, article.title, article.hit);
			}
		} else if (command.equals("article write")) {
			int id = lastArticleId + 1;
			System.out.printf("제목 : ");
			String regDate = Util.getNowDateStr();
			String title = sc.nextLine();
			System.out.printf("내용 : ");
			String body = sc.nextLine();

			Article article = new Article(id, regDate, regDate, title, body);
			articles.add(article);

			System.out.printf("%d번 글이 생성 되었습니다\n", id);
			lastArticleId++;
		} else if (command.startsWith("article detail ")) {
			String[] commandBits = command.split(" ");

			int id = Integer.parseInt(commandBits[2]);

			Article foundArticle = null;

			for (int i = 0; i < articles.size(); i++) {
				Article article = articles.get(i);
				if (article.id == id) {
					foundArticle = article;
					break;
				}
			}

			if (foundArticle == null) {
				System.out.printf("%d번 게시물은 존재하지 않습니다.\n", id);
				continue;
			}
			foundArticle.increaseHit();
			System.out.printf("번호 : %d\n", foundArticle.id);
			System.out.printf("작성날짜 : %s\n", foundArticle.regDate);
			System.out.printf("수정날짜 : %s\n", foundArticle.updateDate);
			System.out.printf("제목 : %s\n", foundArticle.title);
			System.out.printf("내용 : %s\n", foundArticle.body);
			System.out.printf("조회 : %d\n", foundArticle.hit);

		} else if (command.startsWith("article modify ")) {
			String[] commandBits = command.split(" ");

			int id = Integer.parseInt(commandBits[2]);

			Article foundArticle = null;

			for (int i = 0; i < articles.size(); i++) {
				Article article = articles.get(i);
				if (article.id == id) {
					foundArticle = article;
					break;
				}
			}

			if (foundArticle == null) {
				System.out.printf("%d번 게시물은 존재하지 않습니다.\n", id);
				continue;
			}

// 전 게시물에 있는 글쓰기 구현과 일치

			System.out.printf("제목 : ");
			String title = sc.nextLine();
			System.out.printf("내용 : ");
			String body = sc.nextLine();
			String updateDate = Util.getNowDateStr();

// 입력 받은 내용을 title / body 변수에 저장

			foundArticle.title = title;
			foundArticle.body = body;
			foundArticle.updateDate = updateDate;

// foundArticle의 title / body 에 title / body 저장

			System.out.printf("%d번 게시물을 수정했습니다\n", id);

		} else if (command.startsWith("article delete ")) {
			String[] commandBits = command.split(" ");

			int id = Integer.parseInt(commandBits[2]);

			int foundIndex = -1;

			for (int i = 0; i < articles.size(); i++) {
				Article article = articles.get(i);
				if (article.id == id) {
					foundIndex = i;
					break;
				}
			}

// articles 리스트 순회 , Article 정보 가져옴
// article의 id와 id(Integer.parseInt(commandBits[2]);)와 같다면 foundIndex 변수에 i값 대입 후 반복문 탈출

			if (foundIndex == -1) {
				System.out.printf("%d번 게시물은 존재하지 않습니다.\n", id);
				continue;
			}
			articles.remove(foundIndex);
			System.out.printf("%d번 게시물을 삭제했습니다\n", id);

		}

// 만약 foundIndex 변수에 -1이 그대로 남아있다면 %d번 게시물은 존재하지 않습니다. 출력
// 그렇지 않다면 articles 리스트에 저장된 (foundIndex 값)번째 정보 삭제

		else {
			System.out.println("존재하지 않는 명령어입니다");
		}

	}

	System.out.println("==프로그램 끝==");

	sc.close();
}

}

class Article {
	int id;
	String regDate;
	String updateDate;
	String title;
	String body;
	int hit; //조회수 기능 구현을 위한 변수

Article(int id, String regDate, String updateDate, String title, String body) {
	this.id = id;
	this.regDate = regDate;
	this.updateDate = updateDate;
	this.title = title;
	this.body = body;
	this.hit = 0;
}

void increaseHit() { //조회수 기능 구현을 위한 메서드
	this.hit++; // 조회수 1 증가
}

}

profile
개발자로 취직하고싶다!

0개의 댓글