Java 게시판 #3일차

김신하·2023년 2월 13일
post-thumbnail
  • 게시판 작성시 날짜와 시간 저장

Util Class

package com.KoreaIT.java.BasicAM;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Util {
/* 현재 날짜 시간 String /
public static String getNowDateStr() {

	SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//날짜 표현 형태를 "yyyy-MM-dd HH:mm:ss" 이렇게 지정해준다.

	Date now = new Date();

//Date는 이클립스 내부에 있는 class이다.

	return sdf1.format(now);
}

}

Main 클래스

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("%d	/	%s\n", article.id, tempTitle + "...");
					continue;
				}

				System.out.printf("%d	/	%s\n", article.id, article.title);
			}
		} 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();

//regDate String변수에 날짜와 시간을 대입한다.

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

// 날짜와 시간을 regDate라는 변수를 통해서 저장한다.

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

//startsWitch 함수는 대상 문자열이 특정 문자 또는 문자열로 시작하는지 체크하는 함수이다.
//split을 사용해 입력받은 문자열을 공백으로 구분해 commandBits라는 배열에 저장한다.

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

//id변수에 commandBits배열의 2번째 위치에 있는 문자열을 int형으로 변환시켜서 넣어준다.

			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.println(id + "번 글은 존재합니다");
			System.out.printf("번호 : %d\n", foundArticle.id);
			System.out.printf("날짜 : %s\n", foundArticle.regDate);
			System.out.printf("제목 : %s\n", foundArticle.title);
			System.out.printf("내용 : %s\n", foundArticle.body);
		}

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

	}

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

	sc.close();
}

}

class Article {

int id;
String regDate;
String title;
String body;

//regDate라는 변수도 Article 클래스에 추가해준다.

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

}

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

0개의 댓글