2020-10-06 기초자바프로그래밍 내 코드 하는중

 package j_collection;


import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.Scanner;

import java.util.Set;


import e_oop.ScanUtil;


public class Board {


public static void main(String[] args) {

// TODO Auto-generated method stub


/*

* ArrayList와 HashMap 을 사용해 게시판 테이블을 만들고, 

* 조회, 등록, 수정, 삭제가 가능한 게시판을 만들어주세요. 

* 번호(pk) , 제목, 내용, 작성자, 작성일 

* --------------------------------

* 번호 제목 작성자 작성일

* --------------------------------

* 1 안녕하세요 홍길동 10-5

*  1 안녕하세요 홍길동 10-5

*  1 안녕하세요 홍길동 10-5

*  1 안녕하세요 홍길동 10-5

* -----------------------------------

* 1.조회 2.등록 3.종료 

*/

HashMap<String, Object> article  = new HashMap<>();

ArrayList<HashMap<String, Object>> table = new ArrayList<>(); 

article.put("번호", 1 ); 

article.put("제목", "안녕하세요"); 

article.put("내용" , "a"); 

article.put("작성자", "로우넘"); 

article.put("작성일", new Date()); 

table.add(article);

article = new HashMap<>(); 

article.put("번호", 2 ); 

article.put("제목", "맛집추천"); 

article.put("내용" , "하하호호"); 

article.put("작성자", "김달수"); 

article.put("작성일", new Date()); 

table.add(article);

System.out.println(table); 

System.out.println("----------------------------------------------------------"); 

System.out.println("번호\t" + "제목\t" + "내용\t" + "작성자\t" + "작성일\t");

System.out.println("-----------------------------------------------------------");

for( int i = 0 ; i < table.size() ; i++) {

System.out.println(table.get(i).get("번호") + "\t" + table.get(i).get("제목") + "\t" +

table.get(i).get("내용") + "\t" + table.get(i).get("작성자") + "\t" +table.get(i).get("작성일"));

}

System.out.println("-----------------------------------------------------------"); 

System.out.println("1.조회\t2.등록\t3.종료\t" ); 

System.out.println("번호입력"); 

int input = ScanUtil.nextInt(); 

switch (input) {

case 1 : //조회 

   read(table);

           break; 

case 2 : //등록

insult(table);

break; 

  

case 3 :  //종료

System.out.println("종료되었습니다.");

System.exit(0);

break; 

}

}


private static void insult(ArrayList<HashMap<String, Object>> table) {

System.out.println("1.새로등록\t2.수정 ");

int nu = ScanUtil.nextInt(); 

switch (nu){

case 1 : 

HashMap<String, Object> article2 = new HashMap<>();

System.out.println("번호를 등록해주세요");

int nun = ScanUtil.nextInt(); 

System.out.println("제목를 등록해주세요");

String title = ScanUtil.nextLine(); 

System.out.println("내용을 등록해주세요");

String story = ScanUtil.nextLine(); 

System.out.println("작성자를 등록해주세요");

String name = ScanUtil.nextLine(); 

article2.put("번호" , nun); 

article2.put("제목", title);

article2.put("내용", story); 

article2.put("작성자", name); 

article2.put("작성일", new Date()); 

table.add(article2); 

System.out.println("----------------------------------------------------------"); 

System.out.println("번호\t" + "제목\t" + "내용\t" + "작성자\t" + "작성일\t");

System.out.println("-----------------------------------------------------------");

for( int i = 0 ; i < table.size() ; i++) {

System.out.println(table.get(i).get("번호") + "\t" + table.get(i).get("제목") + "\t" +

table.get(i).get("내용") + "\t" + table.get(i).get("작성자") + "\t" +table.get(i).get("작성일"));

}

System.out.println("-----------------------------------------------------------"); 

System.out.println("1.조회\t2.등록\t3.종료\t" ); 

System.out.println("번호입력"); 

int input = ScanUtil.nextInt();

case 2 :

HashMap<String, Object> article3 = new HashMap<>(); 

System.out.println("변경할 게시판 번호를 입력하세요>");

int numm = ScanUtil.nextInt(); 

System.out.println("변경할 제목을 입력하세요>");

String title2 = ScanUtil.nextLine(); 

System.out.println("변경할 내용을 입력하세요>"); 

String story2 = ScanUtil.nextLine(); 

article3.put("번호", numm);

article3.put("제목", title2); 

article3.put("내용", story2); 

article3.put("작성일", new Date()); 

article3.put("작성자", table.get(numm-1).get("작성자"));

table.remove(numm-1); 

table.add(article3); 


}

}


private static void read(ArrayList<HashMap<String , Object>> table) {

System.out.println("1. 조회하기");

System.out.println("게시글 번호 입력>");

int no = ScanUtil.nextInt(); 

HashMap<String, Object> board = null; 

for(int i = 0 ; i< table.size(); i++){ 

int noo = (Integer)table.get(i).get("번호"); 

if(noo == no){ 

board = table.get(i); 

break;

}

}

System.out.println("-------------------------------------------------");

System.out.println("번호 : " + board.get("번호"));

System.out.println("제목 : " + board.get("제목"));

System.out.println("내용 : " + board.get("내용"));

System.out.println("작성자 : " + board.get("작성자"));

System.out.println("작성일 : " + board.get("작성일"));

System.out.println("-------------------------------------------------");

/*

HashMap<String, Object> board = null ; 

for(int i = 0 ; i < table.size() ; i++){ 

HashMap<String, Object> temp = table.get(i); 

if(Integer)temp.get("번호") == no){ 

board = temp; 

break; 

}

}

table.get(0).get("번호") == no ; */

     

}


}


댓글