Error(에러)와 Exception(예외의 차이) 

에러(Error)란 컴퓨터 하드웨어의 오동작 또는 고장으로 인해 응용프로그램에 이상이 생겼거나 JVM 실행에 문제가 생겼을 경우 발생하는것을 말합니다. 이 경우 개발자는 대처할 방법이 극히 제한적입니다. 하지만 예외(Exception)은 다릅니다. 예외란 사용자의 잘못된 조작 또는 개발자의 잘못된 코딩으로 인해 발생하는 프로그램 오류를 말합니다. 예외가 발생하면 프로그램이 종료가 된다는것은 에러와 동일하지만 예외는 예외처리(Exception Handling)을 통해 프로그램을 종료 되지 않고 정상적으로 작동되게 만들어줄 수 있습니다. 자바에서 예외처리는 Try Catch문을 통해 해줄 수 있습니다.

 

 여러가지 예외들 

 예외 구문 이유 
ArithmeticException 정수를 0으로 나눌경우 발생 
ArrayIndexOutOfBoundsExcetion 배열의 범위를 벗어난 index를 접근할 시 발생
ClassCastExcetion 변환할 수 없는 타입으로 객체를 반환 시 발생 
NullPointException   존재하지 않는 레퍼런스를 참조할때 발생 
IllegalArgumentException 잘못된 인자를 전달 할 때 발생 
IOException 입출력 동작 실패 또는 인터럽트 시 발생 
OutOfMemoryException  메모리가 부족한 경우 발생  
NumberFormatException  문자열이 나타내는 숫자와 일치하지 않는 타입의 숫자로 변환시 발생 

 

 빈도수가 높은 예외 

NullPointException

자바 프로그램에서 가장 빈번하게 발생하는 에러입니다. 이 에러는 객체 참조가 없는 상태, 즉 null 값을 갖는 참조변수로 객체 접근 연사자인 토드(.)를 사용했을 때 발생합니다. 객체가 없는 상태에서 객체를 사용하려 했으니 예외가 발생하는 것입니다.

 

ArrayIndexOutOfBoundsExcetion

배열에서 인덱스 범위를 초과하여 사용할 경우 발생합니다. 예를 들어 길이가 3인 int[]arr = new int[3] 배열을 선언했다면 0 ~ 2까지의 index만 사용할 수 있습니다. 하지만 이 배열의 index가 -1 이나 3을 참조하는 순간 예외가 발생합니다.

 

NumberFormatException 

개발을 하다보면 문자열로 되어있는 데이터를 숫자타입으로 변경하는 경우가 자주발생하는데 숫자타입으로 변경할 수 없는 문자를 치환시키려고 하면 발생하는 에러입니다.

 

ClassCastExcetion

타입 변환은 상위 클래스와 하위클래스간에 발생하고 구현 클래스와 인터페이스간에도 발생합니다. 이런 관계가 아니면 클래스는 다른 클래스로 타입을 변환할 수 없습니다. 하지만 이 규칙을 무시하고 억지로 타입을 변환시킬경우 발생하는 에러입니다.

 

 예외 처리코드 (Try Catch) 

try{
    //에러가 발생할 수 있는 코드
    throw new Exception(); //강제 에러 출력 
}catch (Exception e){
    //에러시 수행
     e.printStackTrace(); //오류 출력(방법은 여러가지)
     throw e; //최상위 클래스가 아니라면 무조건 던져주자
}finally{
    //무조건 수행
} 

 

try블록에는 예외가 발생할 수 있는 코드가 위치합니다. try 블록의 코드가 예외없이 정상 실행되면 catch블록의 코드는 실행되지 않고 finally 블록의 코드를 실행합니다. 하지만 try 블록의 코드에서 예외가 발생하면 즉시 실행을 멈추고 catch 블록으로 이동하여 예외처리 코드를 실행합니다. 그리고 finally 블록의 코드를 실행합니다. (finally 블록은 생략 가능합니다.) 

try catch 문을 주로 쓰는 구간은 주로 데이터베이스에 데이터를 주고받을 경우 많이사용합니다. 데이터베이스를 거쳐올때는 변수가 많이 생기기 때문에 try catch문은 필수입니다. 그리고 finally에는 데이터베이스와의 연결을 끊어주는 코드를 주로 삽입하죠. 특정 예외가 발생하여 데이터베이스와의 연결이 끊어지지 않으면 여러가지 문제를 야기할수 있기 때문입니다.

 

예외는 반드시 Throw해주자.

최상단 클래스를 제외한 나머지 클래스에서의 예외처리는 반드시 Throw를 해주어야 합니다. 그렇지 않으면 예외처리를 해주었음에도 불구하고 Main에서는 Exception을 전달받지 못하여 개발자가 예외를 인지못하는 경우가 생깁니다.

 

 

출처:https://coding-factory.tistory.com/280

★App 클래스를 만들어 Main class의 부담을 줄임

package com.KoreaIT.java.AM;

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

import com.KoreaIT.java.AM.dto.Article;
import com.KoreaIT.java.AM.dto.Member;
import com.KoreaIT.java.AM.util.Util;

public class App {

	List<Article> articles;
	List<Member> members;

	public App() {
		articles = new ArrayList<>();
		members = new ArrayList<>();
	}

	public void start() {
		System.out.println("==프로그램 시작==");

		makeTestData();

		Scanner sc = new Scanner(System.in);

		int lastArticleId = 3;
		int lastMemberId = 0;

		while (true) {
			System.out.print("명령어 > ");
			String command = sc.nextLine().trim();

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

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

			if (command.equals("member join")) {
				int id = lastMemberId + 1;
				String regDate = Util.getNowDateTimeStr();
				String loginId = null;
				while (true) {
					System.out.print("로그인 아이디 : ");
					loginId = sc.nextLine();

					if (isJoinableLoginId(loginId) == false) {
						System.out.println("이미 사용중인 아이디입니다");
						continue;
					}
					break;
				}

				String loginPw = null;
				String loginPwConfirm = null;

				while (true) {
					System.out.print("로그인 비밀번호 : ");
					loginPw = sc.nextLine();
					System.out.print("로그인 비밀번호 확인: ");
					loginPwConfirm = sc.nextLine();

					if (loginPw.equals(loginPwConfirm) == false) {
						System.out.println("비밀번호를 확인해주세요");
						continue;
					}
					break;
				}

				System.out.print("이름 : ");
				String name = sc.nextLine();

				Member member = new Member(id, regDate, regDate, loginId, loginPw, name);
				members.add(member);

				System.out.printf("%d번 회원이 가입되었습니다\n", id);
				lastMemberId++;

			} else if (command.equals("article write")) {
				int id = lastArticleId + 1;
				System.out.print("제목 : ");
				String regDate = Util.getNowDateTimeStr();
				String title = sc.nextLine();
				System.out.print("내용 : ");
				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 list")) {
				if (articles.size() == 0) {
					System.out.println("게시글이 없습니다");
					continue;
				}

				String searchKeyword = command.substring("article list".length()).trim();

				List<Article> forPrintArticles = articles;

				if (searchKeyword.length() > 0) {
					System.out.println("searchKeyword : " + searchKeyword);
					forPrintArticles = new ArrayList<>();

					for (Article article : articles) {
						if (article.title.contains(searchKeyword)) {
							forPrintArticles.add(article);
						}
					}
					if (forPrintArticles.size() == 0) {
						System.out.println("검색 결과가 없습니다");
						continue;
					}
				}

				System.out.println(" 번호  //  제목    //  조회  ");
				for (int i = forPrintArticles.size() - 1; i >= 0; i--) {
					Article article = forPrintArticles.get(i);
					System.out.printf("  %d   //   %s   //   %d  \n", article.id, article.title, article.hit);
				}

			} else if (command.startsWith("article detail")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

				Article foundArticle = getArticleById(id);

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

				foundArticle.hit++;

				System.out.println("번호 : " + foundArticle.id);
				System.out.println("작성날짜 : " + foundArticle.regDate);
				System.out.println("수정날짜 : " + foundArticle.updateDate);
				System.out.println("제목 : " + foundArticle.title);
				System.out.println("내용 : " + foundArticle.body);
				System.out.println("조회 : " + foundArticle.hit);

			} else if (command.startsWith("article modify")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

				Article foundArticle = getArticleById(id);

				if (foundArticle == null) {
					System.out.printf("%d번 게시물은 존재하지 않습니다\n", id);
					continue;
				}
				System.out.print("새 제목 : ");
				String updateDate = Util.getNowDateTimeStr();
				String newTitle = sc.nextLine();
				System.out.print("새 내용 : ");
				String newBody = sc.nextLine();

				foundArticle.title = newTitle;
				foundArticle.body = newBody;
				foundArticle.updateDate = updateDate;

				System.out.println(id + "번 글을 수정했습니다");

			} else if (command.startsWith("article delete")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

				int foundIndex = getArticleIndexById(id);

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

				articles.remove(foundIndex);
				System.out.println(id + "번 글을 삭제했습니다");

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

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

		sc.close();

	}

	private boolean isJoinableLoginId(String loginId) {
		int index = getMemberIndexByLoginId(loginId);

		if (index == -1) {
			return true;
		}

		return false;
	}

	private int getMemberIndexByLoginId(String loginId) {
		int i = 0;
		for (Member member : members) {
			if (member.loginId.equals(loginId)) {
				return i;
			}
			i++;
		}
		return -1;
	}

	private int getArticleIndexById(int id) {
		int i = 0;
		for (Article article : articles) {
			if (article.id == id) {
				return i;
			}
			i++;
		}
		return -1;
	}

	private Article getArticleById(int id) {
		int index = getArticleIndexById(id);

		if (index != -1) {
			return articles.get(index);
		}

		return null;
	}

	private void makeTestData() {
		System.out.println("테스트를 위한 데이터를 생성합니다");
		articles.add(new Article(1, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목1", "제목1", 11));
		articles.add(new Article(2, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목2", "제목2", 22));
		articles.add(new Article(3, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목3", "제목3", 33));
	}

}

굉장히 간략해진 Main 클래스

package com.KoreaIT.java.AM;

public class Main {
	public static void main(String[] args) {
		new App().start();
	}
}

dto 패키지를 만들어

 

Dto,Article,Member 클래스를 나눔

package com.KoreaIT.java.AM.dto;

public class Article extends Dto {
	public String title;
	public String body;
	public int hit;

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

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

//////////////////////


package com.KoreaIT.java.AM.dto;

public class Dto {
	public int id;
	public String regDate;
	public String updateDate;
}


///////////
package com.KoreaIT.java.AM.dto;

public class Member extends Dto {
	public String loginId;
	public String loginPw;
	public String name;

	public Member(int id, String regDate, String updateDate, String loginId, String loginPw, String name) {
		this.id = id;
		this.regDate = regDate;
		this.updateDate = updateDate;
		this.loginId = loginId;
		this.loginPw = loginPw;
		this.name = name;
	}
}

//////

Util 클래스의 util 패키지

package com.KoreaIT.java.AM.util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Util {
	/** 포맷팅 현재 날짜/시간 반환 Str */
	public static String getNowDateTimeStr() {
		// 현재 날짜/시간
		LocalDateTime now = LocalDateTime.now();
		// 포맷팅
		String formatedNow = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		// 포맷팅 현재 날짜/시간 출력

		return formatedNow;
	}
}
package com.KoreaIT.java.AM;

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

public class Main {
	static List<Article> articles = new ArrayList<>();
	static List<Member> members = new ArrayList<>();

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

		makeTestData();

		Scanner sc = new Scanner(System.in);

		int lastArticleId = 3;
		int lastMemberId = 0;

		while (true) {
			System.out.print("명령어 > ");
			String command = sc.nextLine().trim();

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

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

			if (command.equals("member join")) {
				int id = lastMemberId + 1;
				String regDate = Util.getNowDateTimeStr();
				String loginId = null;
				while (true) {
					System.out.print("로그인 아이디 : ");
					loginId = sc.nextLine();

					if (isJoinableLoginId(loginId) == false) {
						System.out.println("이미 사용중인 아이디입니다");
						continue;
					}
					break;
				}

				String loginPw = null;
				String loginPwConfirm = null;

				while (true) {
					System.out.print("로그인 비밀번호 : ");
					loginPw = sc.nextLine();
					System.out.print("로그인 비밀번호 확인: ");
					loginPwConfirm = sc.nextLine();

					if (loginPw.equals(loginPwConfirm) == false) {
						System.out.println("비밀번호를 확인해주세요");
						continue;
					}
					break;
				}

				System.out.print("이름 : ");
				String name = sc.nextLine();

				Member member = new Member(id, regDate, regDate, loginId, loginPw, name);
				members.add(member);

				System.out.printf("%d번 회원이 가입되었습니다\n", id);
				lastMemberId++;

			} else if (command.equals("article write")) {
				int id = lastArticleId + 1;
				System.out.print("제목 : ");
				String regDate = Util.getNowDateTimeStr();
				String title = sc.nextLine();
				System.out.print("내용 : ");
				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 list")) {
				if (articles.size() == 0) {
					System.out.println("게시글이 없습니다");
					continue;
				}

				String searchKeyword = command.substring("article list".length()).trim();

				List<Article> forPrintArticles = articles;

				if (searchKeyword.length() > 0) {
					System.out.println("searchKeyword : " + searchKeyword);
					forPrintArticles = new ArrayList<>();

					for (Article article : articles) {
						if (article.title.contains(searchKeyword)) {
							forPrintArticles.add(article);
						}
					}
					if (forPrintArticles.size() == 0) {
						System.out.println("검색 결과가 없습니다");
						continue;
					}
				}

				System.out.println(" 번호  //  제목    //  조회  ");
				for (int i = forPrintArticles.size() - 1; i >= 0; i--) {
					Article article = forPrintArticles.get(i);
					System.out.printf("  %d   //   %s   //   %d  \n", article.id, article.title, article.hit);
				}

			} else if (command.startsWith("article detail")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

				Article foundArticle = getArticleById(id);

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

				foundArticle.hit++;

				System.out.println("번호 : " + foundArticle.id);
				System.out.println("작성날짜 : " + foundArticle.regDate);
				System.out.println("수정날짜 : " + foundArticle.updateDate);
				System.out.println("제목 : " + foundArticle.title);
				System.out.println("내용 : " + foundArticle.body);
				System.out.println("조회 : " + foundArticle.hit);

			} else if (command.startsWith("article modify")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

				Article foundArticle = getArticleById(id);

				if (foundArticle == null) {
					System.out.printf("%d번 게시물은 존재하지 않습니다\n", id);
					continue;
				}
				System.out.print("새 제목 : ");
				String updateDate = Util.getNowDateTimeStr();
				String newTitle = sc.nextLine();
				System.out.print("새 내용 : ");
				String newBody = sc.nextLine();

				foundArticle.title = newTitle;
				foundArticle.body = newBody;
				foundArticle.updateDate = updateDate;

				System.out.println(id + "번 글을 수정했습니다");

			} else if (command.startsWith("article delete")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

				int foundIndex = getArticleIndexById(id);

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

				articles.remove(foundIndex);
				System.out.println(id + "번 글을 삭제했습니다");

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

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

		sc.close();
	}

	private static boolean isJoinableLoginId(String loginId) {
		int index = getMemberIndexByLoginId(loginId);

		if (index == -1) {
			return true;
		}

		return false;
	}

	private static int getMemberIndexByLoginId(String loginId) {
		int i = 0;
		for (Member member : members) {
			if (member.loginId.equals(loginId)) {
				return i;
			}
			i++;
		}
		return -1;
	}

	private static int getArticleIndexById(int id) {
		int i = 0;
		for (Article article : articles) {
			if (article.id == id) {
				return i;
			}
			i++;
		}
		return -1;
	}

	private static Article getArticleById(int id) {
		int index = getArticleIndexById(id);

		if (index != -1) {
			return articles.get(index);
		}

		return null;
	}

	private static void makeTestData() {
		System.out.println("테스트를 위한 데이터를 생성합니다");
		articles.add(new Article(1, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목1", "제목1", 11));
		articles.add(new Article(2, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목2", "제목2", 22));
		articles.add(new Article(3, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목3", "제목3", 33));
	}
}

class Member {
	int id;
	String regDate;
	String updateDate;
	String loginId;
	String loginPw;
	String name;

	Member(int id, String regDate, String updateDate, String loginId, String loginPw, String name) {
		this.id = id;
		this.regDate = regDate;
		this.updateDate = updateDate;
		this.loginId = loginId;
		this.loginPw = loginPw;
		this.name = name;
	}
}

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, regDate, updateDate, title, body, 0);
	}

	Article(int id, String regDate, String updateDate, String title, String body, int hit) {
		this.id = id;
		this.regDate = regDate;
		this.updateDate = updateDate;
		this.title = title;
		this.body = body;
		this.hit = hit;
	}
}
package com.KoreaIT.java.AM;

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

public class Main {
	static List<Article> articles = new ArrayList<>();
	static List<Member> members = new ArrayList<>();

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

		makeTestData();

		Scanner sc = new Scanner(System.in);

		int lastArticleId = 3;
		int lastMemberId = 0;

		while (true) {

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

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

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

			if (command.equals("member join")) {
				int id = lastMemberId + 1;
				String regDate = Util.getNowDateTimeStr();
				String loginId = null;
				while (true) {
					System.out.print("로그인 아이디 : ");
					loginId = sc.nextLine();

					if (isJoinableLoginId(loginId) == false) {
						System.out.println("이미 사용중인 아이디입니다");
						continue;
					}

					break;

				}
				System.out.print("로그인 비밀번호 : ");
				String loginPw = sc.nextLine();
				System.out.print("로그인 비밀번호 확인: ");
				String loginPwConfirm = sc.nextLine();
				System.out.print("이름 : ");
				String name = sc.nextLine();

				Member member = new Member(id, regDate, regDate, loginId, loginPw, name);
				members.add(member);

				System.out.printf("%d번 회원이 가입되었습니다\n", id);
				lastMemberId++;

			} else if (command.equals("article write")) {
				int id = lastArticleId + 1;
				System.out.print("제목 : ");
				String regDate = Util.getNowDateTimeStr();
				String title = sc.nextLine();
				System.out.print("내용 : ");
				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 list")) {
				if (articles.size() == 0) {
					System.out.println("게시글이 없습니다");
					continue;
				}

				String searchKeyword = command.substring("article list".length()).trim();

				List<Article> forPrintArticles = articles;

				if (searchKeyword.length() > 0) {
					System.out.println("searchKeyword : " + searchKeyword);
					forPrintArticles = new ArrayList<>();

					for (Article article : articles) {
						if (article.title.contains(searchKeyword)) {
							forPrintArticles.add(article);
						}
					}
					if (forPrintArticles.size() == 0) {
						System.out.println("검색 결과가 없습니다");
						continue;
					}
				}

				System.out.println(" 번호  //  제목    //  조회  ");
				for (int i = forPrintArticles.size() - 1; i >= 0; i--) {
					Article article = forPrintArticles.get(i);
					System.out.printf("  %d   //   %s   //   %d  \n", article.id, article.title, article.hit);
				}

			} else if (command.startsWith("article detail")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

				Article foundArticle = getArticleById(id);

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

				foundArticle.hit++;

				System.out.println("번호 : " + foundArticle.id);
				System.out.println("작성날짜 : " + foundArticle.regDate);
				System.out.println("수정날짜 : " + foundArticle.updateDate);
				System.out.println("제목 : " + foundArticle.title);
				System.out.println("내용 : " + foundArticle.body);
				System.out.println("조회 : " + foundArticle.hit);

			} else if (command.startsWith("article modify")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

				Article foundArticle = getArticleById(id);

				if (foundArticle == null) {
					System.out.printf("%d번 게시물은 존재하지 않습니다\n", id);
					continue;
				}
				System.out.print("새 제목 : ");
				String updateDate = Util.getNowDateTimeStr();
				String newTitle = sc.nextLine();
				System.out.print("새 내용 : ");
				String newBody = sc.nextLine();

				foundArticle.title = newTitle;
				foundArticle.body = newBody;
				foundArticle.updateDate = updateDate;

				System.out.println(id + "번 글을 수정했습니다");

			} else if (command.startsWith("article delete")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

				int foundIndex = getArticleIndexById(id);

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

				articles.remove(foundIndex);
				System.out.println(id + "번 글을 삭제했습니다");

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

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

		sc.close();
	}

	private static boolean isJoinableLoginId(String loginId) {
		int index = getMemberIndexByLoginId(loginId);

		if (index == -1) {
			return true;
		}

		return false;
	}

	private static int getMemberIndexByLoginId(String loginId) {
		int i = 0;
		for (Member member : members) {
			if (member.loginId.equals(loginId)) {
				return i;
			}
			i++;
		}
		return -1;
	}

	private static int getArticleIndexById(int id) {
		int i = 0;
		for (Article article : articles) {
			if (article.id == id) {
				return i;
			}
			i++;
		}
		return -1;
	}

	private static Article getArticleById(int id) {
		int index = getArticleIndexById(id);

		if (index != -1) {
			return articles.get(index);
		}

		return null;
	}

	private static void makeTestData() {
		System.out.println("테스트를 위한 데이터를 생성합니다");
		articles.add(new Article(1, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목1", "제목1", 11));
		articles.add(new Article(2, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목2", "제목2", 22));
		articles.add(new Article(3, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목3", "제목3", 33));
	}
}

class Member {
	int id;
	String regDate;
	String updateDate;
	String loginId;
	String loginPw;
	String name;

	Member(int id, String regDate, String updateDate, String loginId, String loginPw, String name) {
		this.id = id;
		this.regDate = regDate;
		this.updateDate = updateDate;
		this.loginId = loginId;
		this.loginPw = loginPw;
		this.name = name;
	}
}

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, regDate, updateDate, title, body, 0);
	}

	Article(int id, String regDate, String updateDate, String title, String body, int hit) {
		this.id = id;
		this.regDate = regDate;
		this.updateDate = updateDate;
		this.title = title;
		this.body = body;
		this.hit = hit;
	}
}
package com.KoreaIT.java.AM;

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

public class Main {
	static List<Article> articles = new ArrayList<>();

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

		makeTestData();

		Scanner sc = new Scanner(System.in);

		int lastArticleId = 3;

		while (true) {

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

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

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

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

			} else if (command.equals("article write")) {
				int id = lastArticleId + 1;
				System.out.print("제목 : ");
				String regDate = Util.getNowDateTimeStr();
				String title = sc.nextLine();
				System.out.print("내용 : ");
				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[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

				Article foundArticle = getArticleById(id);

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

				foundArticle.hit++;

				System.out.println("번호 : " + foundArticle.id);
				System.out.println("작성날짜 : " + foundArticle.regDate);
				System.out.println("수정날짜 : " + foundArticle.updateDate);
				System.out.println("제목 : " + foundArticle.title);
				System.out.println("내용 : " + foundArticle.body);
				System.out.println("조회 : " + foundArticle.hit);

			} else if (command.startsWith("article modify")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

				Article foundArticle = getArticleById(id);

				if (foundArticle == null) {
					System.out.printf("%d번 게시물은 존재하지 않습니다\n", id);
					continue;
				}
				System.out.print("새 제목 : ");
				String updateDate = Util.getNowDateTimeStr();
				String newTitle = sc.nextLine();
				System.out.print("새 내용 : ");
				String newBody = sc.nextLine();

				foundArticle.title = newTitle;
				foundArticle.body = newBody;
				foundArticle.updateDate = updateDate;

				System.out.println(id + "번 글을 수정했습니다");

			} else if (command.startsWith("article delete")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

				int foundIndex = getArticleIndexById(id);

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

				articles.remove(foundIndex);
				System.out.println(id + "번 글을 삭제했습니다");

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

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

		sc.close();
	}

	private static int getArticleIndexById(int id) {
		int i = 0;
		for (Article article : articles) {
			if (article.id == id) {
				return i;
			}
			i++;
		}
		return -1;
	}

	private static Article getArticleById(int id) {
//		for (int i = 0; i < articles.size(); i++) {
//			Article article = articles.get(i);
//			if (article.id == id) {
//				return article;
//			}
//		}

//		for (Article article : articles) {
//			if (article.id == id) {
//				return article;
//			}
//		}
		int index = getArticleIndexById(id);

		if (index != -1) {
			return articles.get(index);
		}

		return null;
	}

	private static void makeTestData() {
		System.out.println("테스트를 위한 데이터를 생성합니다");
		articles.add(new Article(1, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목1", "제목1", 11));
		articles.add(new Article(2, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목2", "제목2", 22));
		articles.add(new Article(3, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목3", "제목3", 33));
	}
}

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, regDate, updateDate, title, body, 0);
	}

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

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//자바에 존재하는 클래스, 메소드의 기능을 이렇게 다른 클래스에서 사용하려 하는 경우
//import라는 명령어를 이용해 연결해줍니다

public class Main {

	static List<Article> articles = new ArrayList<>();
	// 메인 클래스와 메서드 사이에 리스트를 만드는 것은
	// 메인 메서드 외에 다른 클래스에도 접근하기 위함입니다
	// static을 붙여 클래스 변수라는걸 나타냅니다
	//해당 클래스의 모든 인스턴스들이 공유하는 변수입니다 다른 메서드에서도 해당 리스트에 접근을 가능하게 합니다.
	//List와 ArrayList는 자바에서 제공하는 기능있는 클래스 이므로 import 합니다
	//List는 자바 자체에서 제공하는 인터페이스 중 하나로, 여러 개의 요소들을 순서에 따라 저장하고 관리할 수 있습니다.
    //List 인터페이스를 구현한 ArrayList 클래스를 이용하여, Article 객체들을 담을 수 있는 리스트를 생성하였습니다. 생성된 리스트의 이름은 articles이며
	//리스트 내부에는 Article 객체들이 담길 수 있습니다.
	//해당 ArrayList는 Article 타입의 변수 articles의 정보를 담기위해 시행하며
	//DB 대용으로 해당 프로젝트에서 저장값을 위해 쓰이는 중입니다
	public static void main(String[] args) {

		System.out.println("==프로그램 시작==");// 메인 메서드에서 가장 먼저 실행 될 출력문.
		makeTestData(); // makeTestData라는 함수를 실행 시킵니다 메인 메서드 안쪽에 만들었기에 Main.makeTestDate(); Main을 생략해도 됍니다

		Scanner sc = new Scanner(System.in);//출력문을 콘솔에 나타내기 위해
		 //스캐너 객체를 만들고 sc라는 변수에 담습니다 입력받을 대상은 표준입력 (System.in) 입니다
		 //스캐너 클래스는 자바에서 제공하기에 import 합니다

		int lastArticleId = 3;//글의 갯수를 저장하기 위한 정수 변수 초기화입니다
		//반복문 밖에 만든 이유는 반복문에서 계속 초기화 돼 값이 입력되지 않기 때문엡니다
		//3으로 하는 이유는 테스트 데이터를 3개 만들어서 3개를 기본값으로 가정하고 시작합니다.

		while (true) { // 명령어를 실행 하기 위한 무한 반복문의 시작

			System.out.print("명령어 > ");
			String command = sc.nextLine().trim(); //문자열 변수 command는
			//스캐너 변수 sc의 기능 nextLine()과 trim을 저장합니다

			if (command.length() == 0) {// 입력한 글자가 0이면 (없으면)
				System.out.println("명령어를 입력해주세요");//이것을 출력
				continue;//반복문으로 돌아갑니다 
			}

			if (command.equals("exit")) {//입력한 글자가 exit와 같다면
				break;//반복문을 빠져나갑니다
			}

			if (command.equals("article list")) {//입력한 글자가 article list라면
				if (articles.size() == 0) {//배열에 담겨둔 articles에서 저장해둔게 0이라면
					System.out.println("게시글이 없습니다");//출력
				} else {//그게 아니면 게시글 있다는것이니
					System.out.println(" 번호  //  제목    //  조회  ");//출력
					for (int i = articles.size() - 1; i >= 0; i--) {//i를 articles 길이의 배열 변수로 -1을 한 뒤 초기화합니다.조건식은 i가0이상일때이며 ,증감식은 i를 1씩 줄여갑니다
						Article article = articles.get(i);//Article 타입의 변수 article에는 artcles의 기능을 받습니다
						System.out.printf("  %d   //   %s   //   %d  \n", article.id, article.title, article.hit);
					}//그 결과 article.id,article.title,article.hit에 저장된 값을 %d(정수형) %s(문자형)으로 출력하고 \n 줄바꿈 합니다
				}

			} else if (command.equals("article write")) {//article list가 아닌 article write를 입력한다면
				int id = lastArticleId + 1; //id를 lastAtricleId(글번호) 0번부터가 아닌 1번부터 하기에 1을 더 한 값을 초기화시키고
				System.out.print("제목 : "); //제목 : 출력
				String regDate = Util.getNowDateTimeStr();//제목에 regDate(시점)이 담깁니다
				String title = sc.nextLine();//title이란 문자열 변수엔 선언해둔 스크린 변수sc의 nextLine();기능을 담습니다
				System.out.print("내용 : ");
				String body = sc.nextLine();//body이란 문자열 변수엔 선언해둔 스크린 변수sc의 nextLine();기능을 담습니다

				Article article = new Article(id, regDate, regDate, title, body);
				//Article 타입 article이란 변수에 객체 Article클래스를 담습니다 클래스 생성자 첫번째의 기능을 담습니다
				articles.add(article);
				//ArrayList클래스의 객체인 articles에 article이란 객체를 추가하는 작업입니다
				//article write에 쓰인 값들을 ArrayList에 저장하기 위함입니다

				System.out.printf("%d번글이 생성되었습니다\n", id);
			    //id 값에 담긴 %d(정수형)으로 출력하고 줄 바꿈
				lastArticleId++;//lastArticleId라는 변수를 1 추가합니다
				//반복문을 빠져 나갔을때 메인 메서드 밖에 지정해놓은 int 변수에 저장됍니다

			} else if (command.startsWith("article detail")) {//article list가 아닌 article detail을 입력한다면 실행
				//startWith 기능은 ~로 시작하는 단어를 찾는 명령어로
				//해당 article detail 로 시작하는 명령어를 받습니다

				String[] cmdDiv = command.split(" ");
				//문자열 배열에 cmdDiv라는 변수를 지정하고 split(" ") 기능을 넣습니다
				//공백을 기준으로 나눠서 인식하겠단 의미입니다

				if (cmdDiv.length < 3) {//그렇게 공백으로 검산해본 값이 3미만이면 ->article detail 후 입력된게 없음
					System.out.println("명령어를 확인해주세요");//출력
					continue;//해당 반복문을 돌아갑니다
				}

				int id = Integer.parseInt(cmdDiv[2]);
				//int id값을 문자열 배열 cmdDiv[2]값으로 치환하겠단 소리입니다
				//ㅁ ㅁ ㅁ  세번째 ㅁ에 숫자가 들어와도 문자로 인식하겠단 소리입니다

				Article foundArticle = getArticleById(id);
				//Article 타입의 변수 foundArticle에 getArticleById의 메소드 id값을 넣습니다

				if (foundArticle == null) {//found값이 null이라면
					System.out.printf("%d번 게시물은 존재하지 않습니다\n", id);//id값이 없다 출력하고
					continue;//해당 반복문으로 돌아갑니다
				}

				foundArticle.hit++;
				//detail 기능을 할 때마다 조회수가 1씩 증가하도록 합니다

				System.out.println("번호 : " + foundArticle.id);
				System.out.println("작성날짜 : " + foundArticle.regDate);
				System.out.println("수정날짜 : " + foundArticle.updateDate);
				System.out.println("제목 : " + foundArticle.title);
				System.out.println("내용 : " + foundArticle.body);
				System.out.println("조회 : " + foundArticle.hit);
				//여기까지 걸리지 않고 내려왔다면 실행
                //foundArticle.[  ]  찾은 값들을 넣어 출력해줍니다
			} else if (command.startsWith("article modify")) {//article list가 아닌 article modify을 입력한다면 실행

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

				Article foundArticle = getArticleById(id);

				if (foundArticle == null) {
					System.out.printf("%d번 게시물은 존재하지 않습니다\n", id);
					continue;
				}//detail과 같은 이유
				
				//여기까지 걸리지 않고 내려왔다면 실행
				System.out.print("새 제목 : ");
				String updateDate = Util.getNowDateTimeStr();
				String newTitle = sc.nextLine();
				System.out.print("새 내용 : ");
				String newBody = sc.nextLine();

				foundArticle.title = newTitle;
				foundArticle.body = newBody;
				foundArticle.updateDate = updateDate;

				System.out.println(id + "번 글을 수정했습니다");

			} else if (command.startsWith("article delete")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

				int foundIndex = getArticleIndexById(id);
				//int타입 foundIndex변수에 getArticleIndexById의 메소드 id값을 넣겠다

				if (foundIndex == -1) {//foundIndex의 값이 -1이라면 존재하지 않으므로
					System.out.printf("%d번 게시물은 존재하지 않습니다\n", id);//출력
					continue;//해당 반복문으로 돌아갑니다
				}//detail과 같은 이유

				articles.remove(foundIndex);//여기까지 걸리지 않고 내려왔다면 실행
				System.out.println(id + "번 글을 삭제했습니다");

			} else {//처음 if문과 이어지는 조건문 else
				System.out.println("존재하지 않는 명령어입니다");
			}//여기까지 왔다면 지정해놓은 모든 명령어가 없다는것
		}

		System.out.println("==프로그램 끝==");//exit로 해당 클래스를 break했을때 출력

		sc.close();//스캐너의 기능의 종료  습관화 들이면 좋습니다
	}

	private static int getArticleIndexById(int id) {
		int i = 0;
		for (Article article : articles) {
			if (article.id == id) {
				return i;
			}
			i++;
		}
		return -1;
	}
	
//	이 코드는 id 값에 해당하는 게시물의 인덱스를 찾아주는 메소드입니다.
//	articles는 게시물 정보를 담고 있는 List<Article> 객체로 
//for-each 반복문을 사용하여 모든 게시물을 순차적으로 조회하면서 article.id와 id 값을 비교합니다. 만약 두 값이 일치한다면
//return i를 통해 해당 게시물의 인덱스 i를 반환합니다.	
//	모든 게시물을 조회해도 해당 id 값에 해당하는 게시물을 찾지 못했다면
//return -1을 통해 id 값에 해당하는 게시물이 없음을 나타냅니다.	
//	따라서 이 메소드를 호출하면 id 값에 해당하는 게시물이 존재하는 경우 해당 게시물의 인덱스를 반환하고 존재하지 않는 경우 -1을 반환하게 됩니다.

	private static Article getArticleById(int id) {
		
//		for (int i = 0; i < articles.size(); i++) { 위의 메서드들이 없다면 글의 유무를 판별 할 때 마다
//			Article article = articles.get(i);     이렇게 매번 articles.size의 배열을
//			if (article.id == id) {                일일히 찾아봐야 하므로
//				return article;                     중복값 제거 메서드를 만듭니다
//			}
//		}

//		for (Article article : articles) {
//			if (article.id == id) {
//				return article;
//			}
//		}
		int index = getArticleIndexById(id);

		if (index != -1) {
			return articles.get(index);
		}

		return null;
	}
	
//	이 코드는 id 값에 해당하는 게시물 객체(Article)를 반환하는 메소드입니다.
//	getArticleIndexById 메소드를 호출하여 id 값에 해당하는 게시물의 인덱스를 구하고
//  이 인덱스를 index 변수에 저장합니다
//	index 변수의 값이 -1이 아니라면, articles 리스트에서 
//  index 인덱스에 해당하는 게시물 객체를 return articles.get(index);를 통해 반환합니다
//	근데 index 값이 -1이라면, 해당 id 값에 해당하는 게시물이 존재하지 않는 것이므로 null 값을 반환합니다.
//	따라서 이 메소드를 호출하면 id 값에 해당하는 게시물이 존재하는 경우 해당 게시물 객체를 반환하고 존재하지 않는 경우 null을 반환하게 됩니다.





	
	
	
	
	private static void makeTestData() { // 메인 메서드에 만들어 놓았던 테스트 데이터를 출력하는 메서드
		System.out.println("테스트를 위한 데이터를 생성합니다");
		articles.add(new Article(1, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목1", "제목1", 11));
		articles.add(new Article(2, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목2", "제목2", 22));
		articles.add(new Article(3, Util.getNowDateTimeStr(), Util.getNowDateTimeStr(), "제목3", "제목3", 33));
	} //articles 에 저장된 값들을 출력합니다 이때,2번째 생성자가 쓰입니다
}

class Article { // new를 했으니 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, regDate, updateDate, title, body, 0);
	}// 생성자를 만들어 객체를 생성하자 마자 값을 넣어줍니다

	Article(int id, String regDate, String updateDate, String title, String body, int hit) {
		this.id = id;
		this.regDate = regDate;
		this.updateDate = updateDate;
		this.title = title;
		this.body = body;
		this.hit = hit;
	}// 두번째 생성자 입니다 클래스 이름과 똑같이 만들며 위의 생성자와도 이름이 같습니다
		// 하지만 같은 생성자를 만들 때는 메서드의 인자나 타입이 달라야하는데 이것을 오버로딩 과정이라고 합니다.
}
package com.KoreaIT.java.AM;

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.print("명령어 > ");
			String command = sc.nextLine().trim();

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

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

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

			} else if (command.equals("article write")) {
				int id = lastArticleId + 1;
				System.out.print("제목 : ");
				String regDate = Util.getNowDateTimeStr();
				String title = sc.nextLine();
				System.out.print("내용 : ");
				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[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

				int id = Integer.parseInt(cmdDiv[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.hit++;

				System.out.println("번호 : " + foundArticle.id);
				System.out.println("작성날짜 : " + foundArticle.regDate);
				System.out.println("수정날짜 : " + foundArticle.updateDate);
				System.out.println("제목 : " + foundArticle.title);
				System.out.println("내용 : " + foundArticle.body);
				System.out.println("조회 : " + foundArticle.hit);

			} else if (command.startsWith("article modify")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

				int id = Integer.parseInt(cmdDiv[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.print("새 제목 : ");
				String updateDate = Util.getNowDateTimeStr();
				String newTitle = sc.nextLine();
				System.out.print("새 내용 : ");
				String newBody = sc.nextLine();

				foundArticle.title = newTitle;
				foundArticle.body = newBody;
				foundArticle.updateDate = updateDate;

				System.out.println(id + "번 글을 수정했습니다");

			} else if (command.startsWith("article delete")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

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

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

				articles.remove(foundIndex);
				System.out.println(id + "번 글을 삭제했습니다");

			} 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;
	}
}
package com.KoreaIT.java.AM;

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.print("명령어 > ");
			String command = sc.nextLine().trim();

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

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

			if (command.equals("article list")) {
				if (articles.size() == 0) {
					System.out.println("게시글이 없습니다");
				} else {
					System.out.println(" 번호  //  제목  ");
					for (int i = articles.size() - 1; i >= 0; i--) {
						Article article = articles.get(i);
						System.out.printf("  %d   //   %s  \n", article.id, article.title);
					}
				}

			} else if (command.equals("article write")) {
				int id = lastArticleId + 1;
				System.out.print("제목 : ");
				String regDate = Util.getNowDateTimeStr();
				String title = sc.nextLine();
				System.out.print("내용 : ");
				String body = sc.nextLine();

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

				System.out.printf("%d번글이 생성되었습니다\n", id);
				lastArticleId++;

			} else if (command.startsWith("article detail")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

				int id = Integer.parseInt(cmdDiv[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.println("번호 : " + foundArticle.id);
				System.out.println("날짜 : " + foundArticle.regDate);
				System.out.println("제목 : " + foundArticle.title);
				System.out.println("내용 : " + foundArticle.body);

			} else if (command.startsWith("article delete")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

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

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

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

				articles.remove(foundIndex);
				System.out.println(id + "번 글을 삭제했습니다");

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

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

		sc.close();
	}
}

class Article {
	int id;
	String regDate;
	String title;
	String body;

	Article(int id, String regDate, String title, String body) {
		this.id = id;
		this.regDate = regDate;
		this.title = title;
		this.body = body;
	}
}
package com.KoreaIT.java.AM;

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.print("명령어 > ");
			String command = sc.nextLine().trim();

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

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

			if (command.equals("article list")) {
				if (articles.size() == 0) {
					System.out.println("게시글이 없습니다");
				} else {
					System.out.println(" 번호  //  제목  ");
					for (int i = articles.size() - 1; i >= 0; i--) {
						Article article = articles.get(i);
						System.out.printf("  %d   //   %s  \n", article.id, article.title);
					}
				}

			} else if (command.equals("article write")) {
				int id = lastArticleId + 1;
				System.out.print("제목 : ");
				String regDate = Util.getNowDateTimeStr();
				String title = sc.nextLine();
				System.out.print("내용 : ");
				String body = sc.nextLine();

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

				System.out.printf("%d번글이 생성되었습니다\n", id);
				lastArticleId++;

			} else if (command.startsWith("article detail")) {

				String[] cmdDiv = command.split(" ");

				if (cmdDiv.length < 3) {
					System.out.println("명령어를 확인해주세요");
					continue;
				}

				int id = Integer.parseInt(cmdDiv[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.println("번호 : " + foundArticle.id);
				System.out.println("날짜 : " + foundArticle.regDate);
				System.out.println("제목 : " + foundArticle.title);
				System.out.println("내용 : " + foundArticle.body);

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

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

		sc.close();
	}
}

class Article {
	int id;
	String regDate;
	String title;
	String body;

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

 

메인 게시판

 

 

package com.KoreaIT.java.AM;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Util {
	/** 포맷팅 현재 날짜/시간 반환 Str */
	public static String getNowDateTimeStr() {
		// 현재 날짜/시간
		LocalDateTime now = LocalDateTime.now();
		// 포맷팅
		String formatedNow = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		// 포맷팅 현재 날짜/시간 출력

		return formatedNow;
	}
}

시간용 util 패키지를 만들어준다

 

for (int i = 1; i <= 5; i++) {
  for (int j = 1; j <= i; j++) {
    System.out.print("*");
  }
  System.out.println();
}

기초 삼각형 모양

int n = 5;
for (int i = 1; i <= n; i++) {
  for (int j = 1; j <= n - i; j++) {
    System.out.print(" ");
  }
  for (int j = 1; j <= 2 * i - 1; j++) {
    System.out.print("*");
  }
  System.out.println();
}

 

 

정삼각형

 

 

for (int i = 5; i >= 1; i--) {
  for (int j = 1; j <= i; j++) {
    System.out.print("*");
  }
  System.out.println();
}

역삼각형

 

 

 

int n = 5;
for (int i = 1; i <= n; i++) {
  for (int j = 1; j <= n - i; j++) {
    System.out.print(" ");
  }
  for (int j = 1; j <= 2 * i - 1; j++) {
    System.out.print("*");
  }
  System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
  for (int j = 1; j <= n - i; j++) {
    System.out.print(" ");
  }
  for (int j = 1; j <= 2 * i - 1; j++) {
    System.out.print("*");
  }
  System.out.println();
}

마름모

 

int n = 5;
for (int i = 1; i <= n; i++) {
  for (int j = 1; j <= n; j++) {
    System.out.print("*");
  }
  System.out.println();
}

정사각형

 

 

int n = 5;
for (int i = 1; i <= n; i++) {
  for (int j = 1; j <= n; j++) {
    if (i == j) {
      System.out.print("*");
    } else {
      System.out.print(" ");
    }
  }
  System.out.println();
}

대각선 별찍기

 

int n = 5;
for (int i = 1; i <= n; i++) {
  for (int j = 1; j <= n; j++) {
    if (i + j == n + 1) {
      System.out.print("*");
    } else {
      System.out.print(" ");
    }
  }
  System.out.println();
}

역대각선 별찍기

 

int n = 5;
for (int i = 1; i <= n; i++) {
  for (int j = 1; j <= n - i; j++) {
    System.out.print(" ");
  }
  for (int j = 1; j <= 2 * i - 1; j++) {
    System.out.print("*");
  }
  System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
  for (int j = 1; j <= n - i; j++) {
    System.out.print(" ");
  }
  for (int j = 1; j <= 2 * i - 1; j++) {
    System.out.print("*");
  }
  System.out.println();
}

다이아몬드 별찍기

+ Recent posts