diff --git "a/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/App.class" "b/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/App.class" new file mode 100644 index 0000000..c179134 Binary files /dev/null and "b/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/App.class" differ diff --git "a/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/Main.class" "b/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/Main.class" new file mode 100644 index 0000000..e85d86d Binary files /dev/null and "b/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/Main.class" differ diff --git "a/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/PageResponse.class" "b/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/PageResponse.class" new file mode 100644 index 0000000..d4bca93 Binary files /dev/null and "b/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/PageResponse.class" differ diff --git "a/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/WiseSaying.class" "b/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/WiseSaying.class" new file mode 100644 index 0000000..f7f04b4 Binary files /dev/null and "b/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/WiseSaying.class" differ diff --git "a/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/WiseSayingController.class" "b/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/WiseSayingController.class" new file mode 100644 index 0000000..7771048 Binary files /dev/null and "b/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/WiseSayingController.class" differ diff --git "a/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/WiseSayingRepository.class" "b/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/WiseSayingRepository.class" new file mode 100644 index 0000000..62c8726 Binary files /dev/null and "b/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/WiseSayingRepository.class" differ diff --git "a/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/WiseSayingService.class" "b/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/WiseSayingService.class" new file mode 100644 index 0000000..31e3dcf Binary files /dev/null and "b/[BE5]\354\235\264\354\235\200\354\244\200/out/production/devcourse/com/ll/wiseSaying/WiseSayingService.class" differ diff --git "a/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/App.java" "b/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/App.java" new file mode 100644 index 0000000..29b93e7 --- /dev/null +++ "b/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/App.java" @@ -0,0 +1,40 @@ +package com.ll.wiseSaying; + +import java.util.Scanner; + +public class App { + private final Scanner scanner; + private final WiseSayingController controller; + + public App() { + this.scanner = new Scanner(System.in); + WiseSayingRepository repository = new WiseSayingRepository(); + WiseSayingService service = new WiseSayingService(repository); + this.controller = new WiseSayingController(service, scanner); + } + + public void run() { + System.out.println("== 명언 앱 =="); + + while (true) { + System.out.print("명령) "); + String command = scanner.nextLine(); + + if (command.equals("등록")) { + controller.create(); + } else if (command.equals("목록") || command.startsWith("목록?")) { + controller.list(command); + } else if (command.startsWith("삭제?id=")) { + controller.delete(command); + } else if (command.startsWith("수정?id=")) { + controller.modify(command); + } else if (command.equals("빌드")) { + controller.build(); + } else if (command.equals("종료")) { + break; + } else { + System.out.println("알 수 없는 명령"); + } + } + } +} \ No newline at end of file diff --git "a/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/Main.java" "b/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/Main.java" new file mode 100644 index 0000000..afa68df --- /dev/null +++ "b/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/Main.java" @@ -0,0 +1,7 @@ +package com.ll.wiseSaying; + +public class Main { + public static void main(String[] args) { + new App().run(); + } +} \ No newline at end of file diff --git "a/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/PageResponse.java" "b/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/PageResponse.java" new file mode 100644 index 0000000..6b7a545 --- /dev/null +++ "b/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/PageResponse.java" @@ -0,0 +1,32 @@ +package com.ll.wiseSaying; + +import java.util.List; + +public class PageResponse { + private List items; + private int currentPage; + private int totalPages; + private static final int PAGE_SIZE = 5; + + public PageResponse(List items, int currentPage, int totalPages) { + this.items = items; + this.currentPage = currentPage; + this.totalPages = totalPages; + } + + public List getItems() { + return items; + } + + public int getCurrentPage() { + return currentPage; + } + + public int getTotalPages() { + return totalPages; + } + + public static int getPageSize() { + return PAGE_SIZE; + } +} \ No newline at end of file diff --git "a/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/WiseSaying.java" "b/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/WiseSaying.java" new file mode 100644 index 0000000..53a1b99 --- /dev/null +++ "b/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/WiseSaying.java" @@ -0,0 +1,33 @@ +package com.ll.wiseSaying; + +public class WiseSaying { + private int id; + private String content; + private String author; + + public WiseSaying(int id, String content, String author) { + this.id = id; + this.content = content; + this.author = author; + } + + public int getId() { + return id; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } +} \ No newline at end of file diff --git "a/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/WiseSayingController.java" "b/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/WiseSayingController.java" new file mode 100644 index 0000000..2f6db27 --- /dev/null +++ "b/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/WiseSayingController.java" @@ -0,0 +1,120 @@ +package com.ll.wiseSaying; + +import java.util.*; + +public class WiseSayingController { + private final WiseSayingService service; + private final Scanner scanner; + + public WiseSayingController(WiseSayingService service, Scanner scanner) { + this.service = service; + this.scanner = scanner; + } + + public void create() { + System.out.print("명언 : "); + String content = scanner.nextLine(); + + System.out.print("작가 : "); + String author = scanner.nextLine(); + + WiseSaying wiseSaying = service.create(content, author); + System.out.println(wiseSaying.getId() + "번 명언이 등록되었습니다."); + } + + public void list(String command) { + // 기본값 설정 + int page = 1; + String keywordType = null; + String keyword = null; + + // 명령어에 파라미터가 있는 경우 파싱 + if (command.contains("?")) { + String[] params = command.split("\\?")[1].split("&"); + for (String param : params) { + String[] keyValue = param.split("="); + switch (keyValue[0]) { + case "page": + page = Integer.parseInt(keyValue[1]); + break; + case "keywordType": + keywordType = keyValue[1]; + break; + case "keyword": + keyword = keyValue[1]; + break; + } + } + } + + // 검색 조건이 있는 경우 출력 + if (keywordType != null && keyword != null) { + System.out.println("----------------------"); + System.out.println("검색타입 : " + keywordType); + System.out.println("검색어 : " + keyword); + } + + System.out.println("번호 / 작가 / 명언"); + System.out.println("----------------------"); + + PageResponse pageResponse = service.getList(page, keywordType, keyword); + + // 아이템 출력 + for (WiseSaying wiseSaying : pageResponse.getItems()) { + System.out.printf("%d / %s / %s\n", + wiseSaying.getId(), + wiseSaying.getAuthor(), + wiseSaying.getContent()); + } + + System.out.println("----------------------"); + // 페이지 정보 출력 + String pageInfo = String.format("페이지 : %s / %s", + page == 1 ? "[1]" : "1", + page == pageResponse.getTotalPages() ? + "[" + pageResponse.getTotalPages() + "]" : + pageResponse.getTotalPages()); + System.out.println(pageInfo); + } + + public void delete(String command) { + try { + int id = Integer.parseInt(command.split("=")[1]); + if (service.delete(id)) { + System.out.println(id + "번 명언이 삭제되었습니다."); + } else { + System.out.println(id + "번 명언은 존재하지 않습니다."); + } + } catch (NumberFormatException e) { + System.out.println("삭제 오류"); + } + } + + public void modify(String command) { + try { + int id = Integer.parseInt(command.split("=")[1]); + WiseSaying wiseSaying = service.modify(id, "", ""); + + if (wiseSaying != null) { + System.out.println("명언(기존) : " + wiseSaying.getContent()); + System.out.print("명언 : "); + String content = scanner.nextLine(); + + System.out.println("작가(기존) : " + wiseSaying.getAuthor()); + System.out.print("작가 : "); + String author = scanner.nextLine(); + + service.modify(id, content, author); + } else { + System.out.println(id + "번 명언은 존재하지 않습니다."); + } + } catch (NumberFormatException e) { + System.out.println("수정 오류"); + } + } + + public void build() { + service.buildData(); + System.out.println("data.json 파일의 내용이 갱신되었습니다."); + } +} \ No newline at end of file diff --git "a/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/WiseSayingRepository.java" "b/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/WiseSayingRepository.java" new file mode 100644 index 0000000..39f8578 --- /dev/null +++ "b/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/WiseSayingRepository.java" @@ -0,0 +1,130 @@ +package com.ll.wiseSaying; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + +public class WiseSayingRepository { + private static final String BASE_PATH = "db/wiseSaying/"; + + public WiseSayingRepository() { + File folder = new File(BASE_PATH); + if (!folder.exists()) { + folder.mkdirs(); + } + } + + public void save(WiseSaying wiseSaying) { + try { + File file = new File(BASE_PATH + wiseSaying.getId() + ".json"); + try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { + writer.write("{\n"); + writer.write("\"id\": " + wiseSaying.getId() + ",\n"); + writer.write("\"content\": \"" + wiseSaying.getContent() + "\",\n"); + writer.write("\"author\": \"" + wiseSaying.getAuthor() + "\"\n"); + writer.write("}"); + } + } catch (IOException e) { + throw new RuntimeException("Failed to save wise saying", e); + } + } + + public WiseSaying findById(int id) { + File file = new File(BASE_PATH + id + ".json"); + if (!file.exists()) return null; + return loadFromFile(file); + } + + public List findAll() { + List wiseSayings = new ArrayList<>(); + File folder = new File(BASE_PATH); + File[] files = folder.listFiles((dir, name) -> name.matches("\\d+\\.json")); + + if (files != null) { + for (File file : files) { + WiseSaying wiseSaying = loadFromFile(file); + if (wiseSaying != null) { + wiseSayings.add(wiseSaying); + } + } + } + return wiseSayings; + } + + public boolean delete(int id) { + File file = new File(BASE_PATH + id + ".json"); + return file.exists() && file.delete(); + } + + private WiseSaying loadFromFile(File file) { + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + StringBuilder json = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + json.append(line); + } + return parseJson(json.toString()); + } catch (IOException e) { + return null; + } + } + + private WiseSaying parseJson(String json) { + try { + json = json.trim().replace("{", "").replace("}", "").replace("\n", "").replace("\r", ""); + String[] lines = json.split(","); + int id = Integer.parseInt(lines[0].split(":")[1].trim()); + String content = lines[1].split(":")[1].trim().replace("\"", ""); + String author = lines[2].split(":")[1].trim().replace("\"", ""); + return new WiseSaying(id, content, author); + } catch (Exception e) { + return null; + } + } + + public int getLastId() { + File file = new File(BASE_PATH + "lastId.txt"); + if (file.exists()) { + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + return Integer.parseInt(reader.readLine().trim()); + } catch (IOException | NumberFormatException e) { + return 1; + } + } + return 1; + } + + public void saveLastId(int lastId) { + try { + File file = new File(BASE_PATH + "lastId.txt"); + try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { + writer.write(String.valueOf(lastId)); + } + } catch (IOException e) { + throw new RuntimeException("Failed to save last ID", e); + } + } + + public void buildData() { + try { + List wiseSayings = findAll(); + File dataFile = new File(BASE_PATH + "data.json"); + try (BufferedWriter writer = new BufferedWriter(new FileWriter(dataFile))) { + writer.write("[\n"); + for (int i = 0; i < wiseSayings.size(); i++) { + WiseSaying ws = wiseSayings.get(i); + writer.write(" {\n"); + writer.write(" \"id\": " + ws.getId() + ",\n"); + writer.write(" \"content\": \"" + ws.getContent() + "\",\n"); + writer.write(" \"author\": \"" + ws.getAuthor() + "\"\n"); + writer.write(" }"); + if (i < wiseSayings.size() - 1) writer.write(","); + writer.write("\n"); + } + writer.write("]\n"); + } + } catch (IOException e) { + throw new RuntimeException("Failed to build data", e); + } + } +} \ No newline at end of file diff --git "a/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/WiseSayingService.java" "b/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/WiseSayingService.java" new file mode 100644 index 0000000..74c222e --- /dev/null +++ "b/[BE5]\354\235\264\354\235\200\354\244\200/src/com/ll/wiseSaying/WiseSayingService.java" @@ -0,0 +1,77 @@ +package com.ll.wiseSaying; + +import java.util.*; + +public class WiseSayingService { + private final WiseSayingRepository repository; + + public WiseSayingService(WiseSayingRepository repository) { + this.repository = repository; + } + + public WiseSaying create(String content, String author) { + int id = repository.getLastId(); + WiseSaying wiseSaying = new WiseSaying(id, content, author); + repository.save(wiseSaying); + repository.saveLastId(id + 1); + return wiseSaying; + } + + public PageResponse getList(int page, String keywordType, String keyword) { + List wiseSayings; + + // 검색 조건이 있는 경우 + if (keywordType != null && !keywordType.isEmpty() && keyword != null && !keyword.isEmpty()) { + wiseSayings = search(keywordType, keyword); + } else { + wiseSayings = repository.findAll(); + } + + // 최신순 정렬 + wiseSayings.sort((a, b) -> b.getId() - a.getId()); + + // 페이징 처리 + int pageSize = PageResponse.getPageSize(); + int totalPages = (int) Math.ceil((double) wiseSayings.size() / pageSize); + int startIndex = (page - 1) * pageSize; + int endIndex = Math.min(startIndex + pageSize, wiseSayings.size()); + + List pagedItems = wiseSayings.subList(startIndex, endIndex); + return new PageResponse<>(pagedItems, page, totalPages); + } + + private List search(String keywordType, String keyword) { + List allWiseSayings = repository.findAll(); + List searchResults = new ArrayList<>(); + + for (WiseSaying wiseSaying : allWiseSayings) { + if (keywordType.equals("content") && + wiseSaying.getContent().contains(keyword)) { + searchResults.add(wiseSaying); + } else if (keywordType.equals("author") && + wiseSaying.getAuthor().contains(keyword)) { + searchResults.add(wiseSaying); + } + } + + return searchResults; + } + + public boolean delete(int id) { + return repository.delete(id); + } + + public WiseSaying modify(int id, String content, String author) { + WiseSaying wiseSaying = repository.findById(id); + if (wiseSaying != null) { + wiseSaying.setContent(content); + wiseSaying.setAuthor(author); + repository.save(wiseSaying); + } + return wiseSaying; + } + + public void buildData() { + repository.buildData(); + } +} \ No newline at end of file