Post.java
public class Post {
private int index;
private String title;
private String author;
private String paragraph;
private String date;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getParagraph() {
return paragraph;
}
public void setParagraph(String paragraph) {
this.paragraph = paragraph;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
Board.java
package Board;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class Board {
Scanner scan = new Scanner(System.in);
List<Post> list = new ArrayList<>();
public void showMenu() {
System.out.println("1 목록 보기 2 글 등록 3 내용 보기 4 글 삭제 0 종료 >> ");
}
public void showList() {
System.out.println("=================================");
System.out.println("번호" + "\t" + "제목" + "\t" + "작성자" + "\t" + "작성일");
for (Post p : list) {
System.out.println((list.indexOf(p) + 1) + "\t" + p.getTitle() + "\t" + p.getAuthor() + "\t" + p.getDate());
}
System.out.println("---------------------------------");
} // end of showList (1번 목록 보기)
public void addList(String title, String author, String contents) {
Post post = new Post();
post.setIndex(list.indexOf(post));
post.setTitle(title);
post.setAuthor(author);
post.setParagraph(contents);
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy, hh:mm aaa");
String date = sdf.format(new Date());
post.setDate(date);
list.add(post);
System.out.println("글이 추가되었습니다.");
} // end of addList (2번 글 등록하기)
public void searchList(int num) {
if (num <= 0 || num > list.size()) {
System.out.println("해당하는 번호에 대한 글이 없습니다.");
return;
}
Post search = list.get(num - 1);
System.out.println("=================================");
System.out.println(num + "\t" + search.getTitle() + "\t" + search.getAuthor() + "\t" + search.getDate());
System.out.println("---------------------------------");
System.out.println(search.getParagraph());
} // end of searchList (3번 내용 보기)
public void delList(int num) {
if (num <= 0 || num > list.size()) {
System.out.println("해당하는 번호에 대한 글이 없습니다.");
return;
}
Post del = list.get(num - 1);
list.remove(list.indexOf(del));
System.out.println(num + "번 글이 삭제되었습니다.");
} // end of delList (4번 글 삭제하기)
}
ProjectMain.java
package Board;
import java.util.Scanner;
public class ProjectMain {
public static void main(String[] args) {
Board b = new Board();
Scanner scan = new Scanner(System.in);
int count = 0;
try {
while (true) {
b.showMenu();
int num = scan.nextInt();
if (num == 0) {
System.out.println("종료합니다.");
break;
} else if (num == 1) {
if (count <= 0) {
System.out.println("등록된 글이 없습니다.");
} else {
b.showList();
}
} else if (num == 2) {
scan.nextLine();
System.out.print("제목 >> ");
String title = scan.nextLine();
System.out.print("작성자 >> ");
String author = scan.nextLine();
System.out.print("내용 >> ");
String contents = scan.nextLine();
b.addList(title, author, contents);
count++;
} else if (num == 3) {
if (count <= 0) {
System.out.println("등록된 글이 없습니다.");
} else {
System.out.print("내용을 볼 글 번호 >> ");
int searchNum = scan.nextInt();
b.searchList(searchNum);
}
} else if (num == 4) {
if (count <= 0) {
System.out.println("등록된 글이 없습니다.");
} else {
System.out.print("삭제할 글 번호 >> ");
int delNum = scan.nextInt();
b.delList(delNum);
count--;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}