반응형

@notepad_jj2

츄르사려고 코딩하는 코집사입니다.


1. bochung01.java

- 상당히 깔끔하지 않은 코드이다.

- Main 문 안에 간단하게 실행되게 만들 것이다.

/*
 * 데이터 저장 관리는 배열 이용
 * 
 * 전체 게시물 조회
 * 글번호 조회
 * 글등록
 * 글수정
 * 글삭제
 * 종료
 */

package bochung.step01.board;
import java.util.Scanner;

public class bochung01 {
	public static void main(String[] args) {
		//게시글을 저장할 배열 선언
		Board[] boards = new Board[2];
		
		// 배열의 크기 표시와 입력될 위치
		int pos = 0;// 글등록, 글삭제시 변경
		
		int no = 0; // 글번호 사용 변수
		Scanner sc = new Scanner(System.in);
		
		System.out.println("게시판 관리 프로그램 - v1");
		while(true) {
			System.out.println("--------------------------");
			System.out.println("1. 전체 게시물 조회");
			System.out.println("2. 글번호 조회");
			System.out.println("3. 글등록");
			System.out.println("4. 글수정");
			System.out.println("5. 글삭제");
			System.out.println("0. 종료");
			System.out.println("--------------------------");
			System.out.print("선택 : ");
			/*
			 * 아래의 코드는 줄바꿈이 생겨서 해결하기 위해 sc.nextLine()을 통해 줄바꿈 제거
			 * 계속 번거롭기 때문에 Integer.parseInt(sc.nextLine()) 한 줄로 사용
			int menu = sc.nextInt();
			sc.nextLine();
			*/
			
			//문자열 값을 받아서 숫자형으로 바꿔줌
			int menu = Integer.parseInt(sc.nextLine());
			
			System.out.println("선택한 메뉴 : " + menu);
			
			switch(menu) {
			
			case 1:
				System.out.println("전체 게시물 수 : " + pos);
				for(int i = pos-1; i>=0;i--) {
					Board b = boards[i];
					System.out.print(b.no + "\t");
					System.out.print(b.title + "\t");
					System.out.println(b.writer + "\t");
				}
				break;
				
			case 2: 
				
				break;
			
			case 3: 
				// 배열의 공간에 데이터가 다 들어가 있다면 더이상 입력이 불가능하다는 메세지를 출력
				if(pos==boards.length) {
					System.out.println("더이상 입력이 불가능합니다.");
					break;
				}
				System.out.println("글등록 호출됨.");
				System.out.print("작성자 : ");
				String writer = sc.nextLine();
				
				System.out.print("제목 : ");
				String title = sc.nextLine();
				
				System.out.print("내용 : ");
				String content = sc.nextLine();
				
				System.out.println("입력 끝");
				
				// writer, title, content의 변수의 값을 case 3번을 빠져나가기 전에 저장.
				Board b = new Board();
				b.writer = writer;
				b.title = title;
				b.content = content;
				b.no = ++no;
				boards[pos++] = b; // 여기서 저장
				break;
			
			case 4: 
				break;
			
			case 5: 
				break;
			
			case 0: 
				System.out.println("프로그램을 종료합니다.");
				System.exit(0); //심플하게 끝내는 방법
			}			
		}
	}
}

 


2. Board.java 클래스

package bochung.step01.board;

/**
 * 게시물 정보를 표현하는 클래스 
 * - 글쓴이
 * - 제목
 * - 내용
 * - 번호
 * @author yong
 */

public class Board {
	int no;
	String writer;
	String title;
	String content;
}
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기