2020-09-23 TIL
한빛출판판네트워크 코딩 테스트 문제 유형 2020
검색해보시오
pms 30
putlic class App saveproject 확인
아래가 정상 코드
public class App {
// main(), saveBoards(), loadBoards() 가 공유하는 필드
static List
// main(), saveMembers(), loadMembers() 가 공유하는 필드
static List
// main(), saveProjects(), loadProjects() 가 공유하는 필드
static List
// main(), saveTasks(), loadTasks() 가 공유하는 필드
static List
public static void main(String[] args) {
// 파일에서 데이터 로딩
loadBoards();
loadMembers();
loadProjects();
loadTasks();
Map<String,Command> commandMap = new HashMap<>();
commandMap.put("/board/add", new BoardAddCommand(boardList));
commandMap.put("/board/list", new BoardListCommand(boardList));
commandMap.put("/board/detail", new BoardDetailCommand(boardList));
commandMap.put("/board/update", new BoardUpdateCommand(boardList));
commandMap.put("/board/delete", new BoardDeleteCommand(boardList));
MemberListCommand memberListCommand = new MemberListCommand(memberList);
commandMap.put("/member/add", new MemberAddCommand(memberList));
commandMap.put("/member/list", memberListCommand);
commandMap.put("/member/detail", new MemberDetailCommand(memberList));
commandMap.put("/member/update", new MemberUpdateCommand(memberList));
commandMap.put("/member/delete", new MemberDeleteCommand(memberList));
commandMap.put("/project/add", new ProjectAddCommand(projectList, memberListCommand));
commandMap.put("/project/list", new ProjectListCommand(projectList));
commandMap.put("/project/detail", new ProjectDetailCommand(projectList));
commandMap.put("/project/update", new ProjectUpdateCommand(projectList, memberListCommand));
commandMap.put("/project/delete", new ProjectDeleteCommand(projectList));
commandMap.put("/task/add", new TaskAddCommand(taskList, memberListCommand));
commandMap.put("/task/list", new TaskListCommand(taskList));
commandMap.put("/task/detail", new TaskDetailCommand(taskList));
commandMap.put("/task/update", new TaskUpdateCommand(taskList, memberListCommand));
commandMap.put("/task/delete", new TaskDeleteCommand(taskList));
commandMap.put("/hello", new HelloCommand());
Deque<String> commandStack = new ArrayDeque<>();
Queue<String> commandQueue = new LinkedList<>();
loop:
while (true) {
String inputStr = Prompt.inputString("명령> ");
if (inputStr.length() == 0) {
continue;
}
commandStack.push(inputStr);
commandQueue.offer(inputStr);
switch (inputStr) {
case "history": printCommandHistory(commandStack.iterator()); break;
case "history2": printCommandHistory(commandQueue.iterator()); break;
case "quit":
case "exit":
System.out.println("안녕!");
break loop;
default:
Command command = commandMap.get(inputStr);
if (command != null) {
try {
// 실행 중 오류가 발생할 수 있는 코드는 try 블록 안에 둔다.
command.execute();
} catch (Exception e) {
// 오류가 발생하면 그 정보를 갖고 있는 객체의 클래스 이름을 출력한다.
System.out.println("--------------------------------------------------------------");
System.out.printf("명령어 실행 중 오류 발생: %s\n", e);
System.out.println("--------------------------------------------------------------");
}
} else {
System.out.println("실행할 수 없는 명령입니다.");
}
}
System.out.println();
}
Prompt.close();
// 데이터를 파일에 저장
saveBoards();
saveMembers();
saveProjects();
saveTasks(); }
맨 앞을 바꾸니까 정상 작동함
public class App {
// main(), saveBoards(), loadBoards() 가 공유하는 필드
static List
static List
static List
static List
public static void main(String[] args) {
// 파일에서 데이터 로딩
loadBoards();
loadMembers();
loadProjects();
loadTasks();
Map<String,Command> commandMap = new HashMap<>();
commandMap.put("/board/add", new BoardAddCommand(boardList));
commandMap.put("/board/list", new BoardListCommand(boardList));
commandMap.put("/board/detail", new BoardDetailCommand(boardList));
commandMap.put("/board/update", new BoardUpdateCommand(boardList));
commandMap.put("/board/delete", new BoardDeleteCommand(boardList));
MemberListCommand memberListCommand = new MemberListCommand(memberList);
commandMap.put("/member/add", new MemberAddCommand(memberList));
commandMap.put("/member/list", memberListCommand);
commandMap.put("/member/detail", new MemberDetailCommand(memberList));
commandMap.put("/member/update", new MemberUpdateCommand(memberList));
commandMap.put("/member/delete", new MemberDeleteCommand(memberList));
commandMap.put("/project/add", new ProjectAddCommand(projectList, memberListCommand));
commandMap.put("/project/list", new ProjectListCommand(projectList));
commandMap.put("/project/detail", new ProjectDetailCommand(projectList));
commandMap.put("/project/update", new ProjectUpdateCommand(projectList, memberListCommand));
commandMap.put("/project/delete", new ProjectDeleteCommand(projectList));
commandMap.put("/task/add", new TaskAddCommand(taskList, memberListCommand));
commandMap.put("/task/list", new TaskListCommand(taskList));
commandMap.put("/task/detail", new TaskDetailCommand(taskList));
commandMap.put("/task/update", new TaskUpdateCommand(taskList, memberListCommand));
commandMap.put("/task/delete", new TaskDeleteCommand(taskList));
commandMap.put("/hello", new HelloCommand());
Deque<String> commandStack = new ArrayDeque<>();
Queue<String> commandQueue = new LinkedList<>();
loop:
while (true) {
String inputStr = Prompt.inputString("명령> ");
if (inputStr.length() == 0) {
continue;
}
commandStack.push(inputStr);
commandQueue.offer(inputStr);
switch (inputStr) {
case "history": printCommandHistory(commandStack.iterator()); break;
case "history2": printCommandHistory(commandQueue.iterator()); break;
case "quit":
case "exit":
System.out.println("안녕!");
break loop;
default:
Command command = commandMap.get(inputStr);
if (command != null) {
try {
// 실행 중 오류가 발생할 수 있는 코드는 try 블록 안에 둔다.
command.execute();
} catch (Exception e) {
// 오류가 발생하면 그 정보를 갖고 있는 객체의 클래스 이름을 출력한다.
System.out.println("--------------------------------------------------------------");
System.out.printf("명령어 실행 중 오류 발생: %s\n", e);
System.out.println("--------------------------------------------------------------");
}
} else {
System.out.println("실행할 수 없는 명령입니다.");
}
}
System.out.println();
}
Prompt.close();
// 데이터를 파일에 저장
saveBoards();
saveMembers();
saveProjects();
saveTasks(); }
static void printCommandHistory(Iterator
if ((count % 5) == 0 && Prompt.inputString(":").equalsIgnoreCase("q")) {
break;
}
}
} catch (Exception e) {
System.out.println("history 명령 처리 중 오류 발생!");
} }
private static void saveBoards() { FileOutputStream out = null;
try {
// 파일로 데이터를 출력할 때 사용할 도구를 준비한다.
out = new FileOutputStream(boardFile);
out.write(boardList.size() >> 24);
out.write(boardList.size() >> 16);
out.write(boardList.size() >> 8);
out.write(boardList.size());
for (Board board : boardList) {
// 게시글 목록에서 게시글 데이터를 꺼내 바이너리 형식으로 출력한다.
// => 게시글 번호 출력 (4바이트)
out.write(board.getNo() >> 24);
out.write(board.getNo() >> 16);
out.write(board.getNo() >> 8);
out.write(board.getNo());
// => 게시글 제목 출력
// 문자열의 바이트 길이(2바이트) + 문자열의 바이트 배열
byte[] bytes = board.getTitle().getBytes("UTF-8");
out.write(bytes.length >> 8);
out.write(bytes.length);
out.write(bytes);
// => 게시글 내용 출력
// 문자열의 바이트 길이(2바이트) + 문자열의 바이트 배열
bytes = board.getContent().getBytes("UTF-8");
out.write(bytes.length >> 8);
out.write(bytes.length);
out.write(bytes);
// => 게시글 작성자 출력
// 문자열의 바이트 길이(2바이트) + 문자열의 바이트 배열
bytes = board.getWriter().getBytes("UTF-8");
out.write(bytes.length >> 8);
out.write(bytes.length);
out.write(bytes);
// => 게시글 등록일 출력 (10바이트)
bytes = board.getRegisteredDate().toString().getBytes("UTF-8");
out.write(bytes);
// => 게시글 조회수 출력
out.write(board.getViewCount() >> 24);
out.write(board.getViewCount() >> 16);
out.write(board.getViewCount() >> 8);
out.write(board.getViewCount());
}
System.out.printf("총 %d 개의 게시글 데이터를 저장했습니다.\n", boardList.size());
} catch (IOException e) {
System.out.println("게시글 데이터의 파일 쓰기 중 오류 발생! - " + e.getMessage());
} finally {
try {
out.close();
} catch (IOException e) {
// FileWriter를 닫을 때 발생하는 예외는 무시한다.
}
} }
private static void loadBoards() { FileInputStream in = null;
try {
// 파일을 읽을 때 사용할 도구를 준비한다.
in = new FileInputStream(boardFile);
// 데이터의 개수를 먼저 읽는다. (4바이트)
int size = in.read() << 24;
size += in.read() << 16;
size += in.read() << 8;
size += in.read();
for (int i = 0; i < size; i++) {
Board board = new Board();
// 출력 형식에 맞춰서 파일에서 데이터를 읽는다.
// => 게시글 번호 읽기
int value = in.read() << 24;
value += in.read() << 16;
value += in.read() << 8;
value += in.read();
board.setNo(value);
// 문자열을 읽을 바이트 배열을 준비한다.
byte[] bytes = new byte[30000];
// => 게시글 제목 읽기
int len = in.read() << 8 | in.read();
in.read(bytes, 0, len);
board.setTitle(new String(bytes, 0, len, "UTF-8"));
// => 게시글 내용 읽기
len = in.read() << 8 | in.read();
in.read(bytes, 0, len);
board.setContent(new String(bytes, 0, len, "UTF-8"));
// => 게시글 작성자 읽기
len = in.read() << 8 | in.read();
in.read(bytes, 0, len);
board.setWriter(new String(bytes, 0, len, "UTF-8"));
// => 게시글 등록일 읽기
in.read(bytes, 0, 10);
board.setRegisteredDate(Date.valueOf(new String(bytes, 0, 10, "UTF-8")));
// => 게시글 조회수 읽기
value = in.read() << 24;
value += in.read() << 16;
value += in.read() << 8;
value += in.read();
board.setViewCount(value);
// 게시글 객체를 Command가 사용하는 목록에 저장한다.
boardList.add(board);
}
System.out.printf("총 %d 개의 게시글 데이터를 로딩했습니다.\n", boardList.size());
} catch (Exception e) {
System.out.println("게시글 파일 읽기 중 오류 발생! - " + e.getMessage());
// 파일에서 데이터를 읽다가 오류가 발생하더라도
// 시스템을 멈추지 않고 계속 실행하게 한다.
// 이것이 예외처리를 하는 이유이다!!!
} finally {
try {
in.close();
} catch (Exception e) {
// close() 실행하다가 오류가 발생한 경우 무시한다.
// 왜? 닫다가 발생한 오류는 특별히 처리할 게 없다.
}
} } private static void saveMembers() {
FileOutputStream out = null;
try {
out = new FileOutputStream(memberFile);
out.write(memberList.size() >> 24);
out.write(memberList.size() >> 16);
out.write(memberList.size() >> 8);
out.write(memberList.size());
for (Member member : memberList) {
out.write(member.getNo() >> 24);
out.write(member.getNo() >> 16);
out.write(member.getNo() >> 8);
out.write(member.getNo());
byte[] bytes = member.getName().getBytes("UTF-8");
out.write(bytes.length >> 8);
out.write(bytes.length);
out.write(bytes);
bytes = member.getEmail().getBytes("UTF-8");
out.write(bytes.length >> 8);
out.write(bytes.length);
out.write(bytes);
bytes = member.getPassword().getBytes("UTF=8");
out.write(bytes.length >> 8);
out.write(bytes.length);
out.write(bytes);
bytes = member.getPhoto().getBytes("UTF-8");
out.write(bytes.length >> 8);
out.write(bytes.length);
out.write(bytes);
bytes = member.getTel().getBytes("UTF-8");
out.write(bytes.length >> 8);
out.write(bytes.length);
out.write(bytes);
// => 게시글 등록일 출력 (10바이트)
bytes = member.getRegisteredDate().toString().getBytes("UTF-8");
out.write(bytes);
}
System.out.printf("총 %d 개의 게시글 데이터를 저장했습니다.\n", memberList.size());
} catch (IOException e) {
System.out.println("게시글 데이터의 파일 쓰기 중 오류 발생! - " + e.getMessage());
} finally {
try {
out.close();
} catch (IOException e) {
// FileWriter를 닫을 때 발생하는 예외는 무시한다.
}
} }
private static void loadMembers() { FileInputStream in = null;
try {
// 파일을 읽을 때 사용할 도구를 준비한다.
in = new FileInputStream(memberFile);
int size = in.read() << 24;
size += in.read() << 16;
size += in.read() << 8;
size += in.read();
for (int i = 0; i < size; i++) {
Member member = new Member();
// 출력 형식에 맞춰서 파일에서 데이터를 읽는다.
// => 게시글 번호 읽기
int value = in.read() << 24;
value += in.read() << 16;
value += in.read() << 8;
value += in.read();
member.setNo(value);
// 문자열을 읽을 바이트 배열을 준비한다.
byte[] bytes = new byte[30000];
int len = in.read() << 8 | in.read();
in.read(bytes, 0, len);
member.setName(new String(bytes, 0, len, "UTF-8"));
len = in.read() << 8 | in.read();
in.read(bytes, 0, len);
member.setEmail(new String(bytes, 0, len, "UTF-8"));
len = in.read() << 8 | in.read();
in.read(bytes, 0, len);
member.setPassword(new String(bytes, 0, len, "UTF-8"));
len = in.read() << 8 | in.read();
in.read(bytes, 0, len);
member.setPhoto(new String(bytes, 0, len, "UTF-8"));
len = in.read() << 8 | in.read();
in.read(bytes, 0, len);
member.setTel(new String(bytes, 0, len, "UTF-8"));
// => 게시글 등록일 읽기
in.read(bytes, 0, 10);
member.setRegisteredDate(Date.valueOf(new String(bytes, 0, 10, "UTF-8")));
// 게시글 객체를 Command가 사용하는 목록에 저장한다.
memberList.add(member);
}
System.out.printf("총 %d 개의 게시글 데이터를 로딩했습니다.\n", memberList.size());
} catch (Exception e) {
System.out.println("게시글 파일 읽기 중 오류 발생! - " + e.getMessage());
} finally {
try {
in.close();
} catch (Exception e) {
// close() 실행하다가 오류가 발생한 경우 무시한다.
// 왜? 닫다가 발생한 오류는 특별히 처리할 게 없다.
}
} }
private static void saveProjects() { FileOutputStream out = null;
try {
// 파일로 데이터를 출력할 때 사용할 도구를 준비한다.
out = new FileOutputStream(projectFile);
out.write(projectList.size() >> 24);
out.write(projectList.size() >> 16);
out.write(projectList.size() >> 8);
out.write(projectList.size());
for (Project project : projectList) {
out.write(project.getNo() >> 24);
out.write(project.getNo() >> 16);
out.write(project.getNo() >> 8);
out.write(project.getNo());
byte[] bytes = project.getTitle().getBytes("UTF-8");
out.write(bytes.length >> 8);
out.write(bytes.length);
out.write(bytes);
bytes = project.getContent().getBytes("UTF-8");
out.write(bytes.length >> 8);
out.write(bytes.length);
out.write(bytes);
bytes = project.getStartDate().toString().getBytes("UTF-8");
out.write(bytes);
bytes = project.getEndDate().toString().getBytes("UTF-8");
out.write(bytes);
bytes = project.getOwner().getBytes("UTF=8");
out.write(bytes.length >> 8);
out.write(bytes.length);
out.write(bytes);
bytes = project.getMembers().getBytes("UTF-8");
out.write(bytes.length >> 8);
out.write(bytes.length);
out.write(bytes);
}
System.out.printf("총 %d 개의 게시글 데이터를 저장했습니다.\n", memberList.size());
} catch (IOException e) {
System.out.println("게시글 데이터의 파일 쓰기 중 오류 발생! - " + e.getMessage());
} finally {
try {
out.close();
} catch (IOException e) {
// FileWriter를 닫을 때 발생하는 예외는 무시한다.
}
} }
private static void loadProjects() { FileInputStream in = null;
try {
// 파일을 읽을 때 사용할 도구를 준비한다.
in = new FileInputStream(projectFile);
int size = in.read() << 24;
size += in.read() << 16;
size += in.read() << 8;
size += in.read();
for (int i = 0; i < size; i++) {
Project project = new Project();
// 출력 형식에 맞춰서 파일에서 데이터를 읽는다.
// => 게시글 번호 읽기
int value = in.read() << 24;
value += in.read() << 16;
value += in.read() << 8;
value += in.read();
project.setNo(value);
// 문자열을 읽을 바이트 배열을 준비한다.
byte[] bytes = new byte[30000];
int len = in.read() << 8 | in.read();
in.read(bytes, 0, len);
project.setTitle(new String(bytes, 0, len, "UTF-8"));
len = in.read() << 8 | in.read();
in.read(bytes, 0, len);
project.setContent(new String(bytes, 0, len, "UTF-8"));
in.read(bytes, 0, 10);
project.setStartDate(Date.valueOf(new String(bytes, 0, 10, "UTF-8")));
in.read(bytes, 0, 10);
project.setEndDate(Date.valueOf(new String(bytes, 0, 10, "UTF-8")));
len = in.read() << 8 | in.read();
in.read(bytes, 0, len);
project.setOwner(new String(bytes, 0, len, "UTF-8"));
len = in.read() << 8 | in.read();
in.read(bytes, 0, len);
project.setMembers(new String(bytes, 0, len, "UTF-8"));
projectList.add(project);
}
System.out.printf("총 %d 개의 게시글 데이터를 로딩했습니다.\n", projectList.size());
} catch (Exception e) {
System.out.println("게시글 파일 읽기 중 오류 발생! - " + e.getMessage());
} finally {
try {
in.close();
} catch (Exception e) {
// close() 실행하다가 오류가 발생한 경우 무시한다.
// 왜? 닫다가 발생한 오류는 특별히 처리할 게 없다.
}
} }
private static void saveTasks() { FileOutputStream out = null;
try {
// 파일로 데이터를 출력할 때 사용할 도구를 준비한다.
out = new FileOutputStream(taskFile);
out.write(taskList.size() >> 24);
out.write(taskList.size() >> 16);
out.write(taskList.size() >> 8);
out.write(taskList.size());
for (Task task : taskList) {
out.write(task.getNo() >> 24);
out.write(task.getNo() >> 16);
out.write(task.getNo() >> 8);
out.write(task.getNo());
byte[] bytes = task.getContent().getBytes("UTF-8");
out.write(bytes.length >> 8);
out.write(bytes.length);
out.write(bytes);
bytes = task.getDeadline().toString().getBytes("UTF-8");
out.write(bytes);
out.write(task.getStatus() >> 24);
out.write(task.getStatus() >> 16);
out.write(task.getStatus() >> 8);
out.write(task.getStatus());
bytes = task.getOwner().getBytes("UTF-8");
out.write(bytes.length >> 8);
out.write(bytes.length);
out.write(bytes);
}
System.out.printf("총 %d 개의 게시글 데이터를 저장했습니다.\n", taskList.size());
} catch (IOException e) {
System.out.println("게시글 데이터의 파일 쓰기 중 오류 발생! - " + e.getMessage());
} finally {
try {
out.close();
} catch (IOException e) {
// FileWriter를 닫을 때 발생하는 예외는 무시한다.
}
} }
private static void loadTasks() { FileInputStream in = null;
try {
// 파일을 읽을 때 사용할 도구를 준비한다.
in = new FileInputStream(taskFile);
int size = in.read() << 24;
size += in.read() << 16;
size += in.read() << 8;
size += in.read();
for (int i = 0; i < size; i++) {
Task task = new Task();
// 출력 형식에 맞춰서 파일에서 데이터를 읽는다.
// => 게시글 번호 읽기
int value = in.read() << 24;
value += in.read() << 16;
value += in.read() << 8;
value += in.read();
task.setNo(value);
// 문자열을 읽을 바이트 배열을 준비한다.
byte[] bytes = new byte[30000];
int len = in.read() << 8 | in.read();
in.read(bytes, 0, len);
task.setContent(new String(bytes, 0, len, "UTF-8"));
in.read(bytes, 0, 10);
task.setDeadline(Date.valueOf(new String(bytes, 0, 10, "UTF-8")));
value = in.read() << 24;
value += in.read() << 16;
value += in.read() << 8;
value += in.read();
task.setStatus(value);
len = in.read() << 8 | in.read();
in.read(bytes, 0, len);
task.setOwner(new String(bytes, 0, len, "UTF-8"));
// 게시글 객체를 Command가 사용하는 목록에 저장한다.
taskList.add(task);
}
System.out.printf("총 %d 개의 게시글 데이터를 로딩했습니다.\n", taskList.size());
} catch (Exception e) {
System.out.println("게시글 파일 읽기 중 오류 발생! - " + e.getMessage());
} finally {
try {
in.close();
} catch (Exception e) {
// close() 실행하다가 오류가 발생한 경우 무시한다.
// 왜? 닫다가 발생한 오류는 특별히 처리할 게 없다.
}
} }
}