package com.KoreaIT.java.AM;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TestMain {
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.print("명령어 > ");
String command = sc.nextLine();
if (command.length() == 0) { //아무것도 입력 x
System.out.println("명령어를 입력해주세요"); // 이게 출력된다
continue; // while문 초기로 돌려보냄!
}
if (command.equals("exit")) {
break;
}
if (command.equals("article list")) { // 이 명령어를 이제 유의미하게 만들었다
if (articles.size() == 0) { // 게시글이 없다면
System.out.println("게시글이 없습니다"); // 출력
} else { //있다면?
System.out.println("있던데???"); //출력
}
} else if (command.equals("article write")) {
int id = lastArticleId + 1;
System.out.print("제목 : ");
String title = sc.nextLine();
System.out.print("내용 : ");
String body = sc.nextLine();
Article article = new Article(id, title, body); //객체 생성자 호출
articles.add(article); //생성된 객체를 articles 변수 리스트에 추가!
System.out.printf("%d번글이 생성되었습니다\n", id);
lastArticleId++;
} else {
System.out.println("존재하지 않는 명령어입니다");
}
}
System.out.println("==프로그램 끝==");
sc.close();
}
}
class Article {
int id;
String title;
String body;
Article(int id, String title, String body) {
this.id = id;
this.title = title;
this.body = body;
}
}
나는 article list 명령어를 입력했을때 이제 유의미한 프로그램 가동을 하고싶다
List<Article> articles = new ArrayList<>(); 으로 배열 객체를 만들어주고
List와 ArrayList라는 자바에 있는 기능을 썼으므로 각각 import를 해준다
그리고 변수 articles에 그 값을 저장할 것이고
new로 객체를 지정해주었으므로 Article 클래스를 하나 만들어준다
Article은 게시글이란 뜻인데 개발자들이 통상 쓰는듯하다
일단 간단하게 몇번글인지,제목,내용만 나오는 명령어로 만들고 싶어서
변수를 Int id; , String title , String body 로 3개 지정해준다
실행 시키자마자 저 값을 입력 시키고 싶으니 생성자 Article을 만들어준다
int lastArticleId = 0;
List<Article> articles = new ArrayList<>(); 을 while문 바깥에다 쓰는 이유는 전에도 말했다 싶이
반복문안에 놓으면 무한적으로 초기화 되기 때문에 바깥에다가 값을 저장해놔야 글1,글2,글3 이런식으로 순서대로 저장이 될 것이다
클래스와 객체 생성자를 만들어 줬으니 써먹어야 한다.
// Article 객체 생성자 호출
Article article = new Article(id, title, body);
// 생성된 Article 객체를 articles 리스트에 추가
articles.add(article);
코드를 실행해보니 잘 되는 것 같다
'java > java 게시판 만들기' 카테고리의 다른 글
| 게시판 만들기 2일차 "게시물 상세보기 구현중" (0) | 2023.03.17 |
|---|---|
| 게시판 만들기 2일차 "게시물 목록 기능 구현" (0) | 2023.03.15 |
| 게시판 만들기 1일차 "게시물 작성 기능 추가" (0) | 2023.03.14 |
| 게시판 만들기 -1일차- "명령어 입력" (0) | 2023.03.14 |
| 게시판 만들기 -1일차- "명령어 입력과 github 올리기" (0) | 2023.03.13 |