public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.print("명령어 : ");
String command = scan.nextLine();
if (command.equals("exit")) {
System.out.println("프로그램을 종료합니다.");
break;
}
}
}
}
==============================================================================================
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Mian {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Article> articles = new ArrayList<>();
int articleNum = 0;
while (true) {
System.out.print("명령어 : ");
String command = sc.nextLine();
if (command.equals("exit")) {
System.out.println("프로그램을 종료합니다.");
break;
} else if (command.equals("add")) {
articleNum++;
System.out.print("게시물 제목을 입력해주세요 : ");
String title = sc.nextLine();
System.out.print("게시물 내용을 입력해주세요 : ");
String content = sc.nextLine();
Article article = new Article(articleNum, title, content);
articles.add(articles.size(), article);
System.out.println("게시물이 등록되었습니다.");
}
}
}
}
class Article {
int id;
String title;
String content;
Article(int id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}
}
==============================================================================================
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Mian {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Article> articles = new ArrayList<>();
int articleNum = 0;
while (true) {
System.out.print("명령어 : ");
String command = sc.nextLine();
if (command.equals("exit")) {
System.out.println("프로그램을 종료합니다.");
break;
} else if (command.equals("add")) {
articleNum++;
System.out.print("게시물 제목을 입력해주세요 : ");
String title = sc.nextLine();
System.out.print("게시물 내용을 입력해주세요 : ");
String content = sc.nextLine();
Article article = new Article(articleNum, title, content);
articles.add(articles.size(), article);
System.out.println("게시물이 등록되었습니다.");
} else if (command.equals("list")) {
for (int i = 0; i < articles.size(); i++) {
System.out.println("==================");
System.out.println("번호 : " + articles.get(i).id);
System.out.println("제목 : " + articles.get(i).title);
}
}
}
}
}
class Article {
int id;
String title;
String content;
Article(int id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}
}
==============================================================================================
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Mian {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Article> articles = new ArrayList<>();
int articleNum = 0;
while (true) {
System.out.print("명령어 : ");
String command = sc.nextLine();
if (command.equals("exit")) {
System.out.println("프로그램을 종료합니다.");
break;
} else if (command.equals("add")) {
articleNum++;
System.out.print("게시물 제목을 입력해주세요 : ");
String title = sc.nextLine();
System.out.print("게시물 내용을 입력해주세요 : ");
String content = sc.nextLine();
Article article = new Article(articleNum, title, content);
articles.add(articles.size(), article);
System.out.println("게시물이 등록되었습니다.");
} else if (command.equals("update")) {
System.out.printf("수정할 게시물 번호 : ");
int number = sc.nextInt();
sc.nextLine();
boolean contentExist = false;
for (int i = 0; i < articles.size(); i++) {
if (number == articles.get(i).id) {
System.out.printf("제목 : ");
String title = sc.nextLine();
System.out.printf("내용 : ");
String content = sc.nextLine();
Article article = new Article(number, title, content);
articles.set(i, article);
System.out.printf("%d번 게시물이 수정되었습니다\n", number);
contentExist = true;
}
}
if (contentExist == false) {
System.out.println("없는 게시물 번호입니다.");
}
} else if (command.equals("list")) {
for (int i = 0; i < articles.size(); i++) {
System.out.println("==================");
System.out.println("번호 : " + articles.get(i).id);
System.out.println("제목 : " + articles.get(i).title);
}
}
}
}
}
class Article {
int id;
String title;
String content;
Article(int id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}
}
==============================================================================================
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Mian {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Article> articles = new ArrayList<>();
int articleNum = 0;
while (true) {
System.out.print("명령어 : ");
String command = sc.nextLine();
if (command.equals("exit")) {
System.out.println("프로그램을 종료합니다.");
break;
} else if (command.equals("add")) {
articleNum++;
System.out.print("게시물 제목을 입력해주세요 : ");
String title = sc.nextLine();
System.out.print("게시물 내용을 입력해주세요 : ");
String content = sc.nextLine();
Article article = new Article(articleNum, title, content);
articles.add(articles.size(), article);
System.out.println("게시물이 등록되었습니다.");
} else if (command.equals("update")) {
System.out.printf("수정할 게시물 번호 : ");
int number = sc.nextInt();
sc.nextLine();
boolean contentExist = false;
for (int i = 0; i < articles.size(); i++) {
if (number == articles.get(i).id) {
System.out.printf("제목 : ");
String title = sc.nextLine();
System.out.printf("내용 : ");
String content = sc.nextLine();
Article article = new Article(number, title, content);
articles.set(i, article);
System.out.printf("%d번 게시물이 수정되었습니다\n", number);
contentExist = true;
}
}
if (contentExist == false) {
System.out.println("없는 게시물 번호입니다.");
}
} else if (command.equals("delete")) {
System.out.printf("삭제할 게시물 번호 : ");
int number = sc.nextInt();
sc.nextLine();
boolean contentExist = false;
for (int i = 0; i < articles.size(); i++) {
if (number == articles.get(i).id) {
articles.remove(i);
System.out.printf("%d번 게시물이 삭제되었습니다\n", number);
contentExist = true;
}
}
if (contentExist == false) {
System.out.println("없는 게시물 번호입니다.");
}
} else if (command.equals("list")) {
for (int i = 0; i < articles.size(); i++) {
System.out.println("==================");
System.out.println("번호 : " + articles.get(i).id);
System.out.println("제목 : " + articles.get(i).title);
}
}
}
}
}
class Article {
int id;
String title;
String content;
Article(int id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}
}