diff --git a/main/java/org/example/Main.java b/main/java/org/example/Main.java new file mode 100644 index 0000000..34149d9 --- /dev/null +++ b/main/java/org/example/Main.java @@ -0,0 +1,150 @@ +package org.example; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; + +public class Main { + public static void quotes() throws IOException, ParseException { + int number = 0; + Scanner in = new Scanner(System.in); + String command = ""; + Map words = new HashMap<>(); + Map names = new HashMap<>(); + File dir = new File("E:\\JavaStudy\\quotesList\\db\\wiseSaying"); + File files[] = dir.listFiles(); + for(int i = 0; i < files.length; i++){ + String filename = files[i].getName().split("\\.")[0]; + if(isNumberic(filename)){ + JSONParser parser = new JSONParser(); + Reader reader = new FileReader(files[i].getPath()); + JSONObject jsonObject = (JSONObject) parser.parse(reader); + + String w = (String) jsonObject.get("content"); + String n = (String) jsonObject.get("author"); + int num = Integer.parseInt(String.valueOf(jsonObject.get("id"))); + + words.put(num, w); + names.put(num, n); + + if(number < num) + number = num; + + reader.close(); + } + } + + while(true){ + System.out.println("== 명언 앱 =="); + System.out.print("명령) "); + command = in.nextLine(); + if(command.equals("종료")) + break; + else if(command.equals("등록")){ + System.out.print("명언 : "); + String word = in.nextLine(); + System.out.print("작가 : "); + String name = in.nextLine(); + number++; + words.put(number, word); + names.put(number, name); + System.out.println(number+ "번 명언이 등록되었습니다."); + file_save(number, word, name); + File f = new File("E:\\JavaStudy\\quotesList\\db\\wiseSaying\\lastId.txt"); + FileWriter fw = new FileWriter(f, false) ; + fw.write(number + ""); + fw.flush(); + fw.close(); + } + else if(command.equals("목록")){ + System.out.println("번호 / 작가 / 명언"); + for(int key : words.keySet()){ + System.out.println(key + " / " + names.get(key) + " / " + words.get(key)); + } + } + else if(command.contains("삭제")){ + int rm = Integer.parseInt(command.split("=")[1]); + if(words.containsKey(rm)){ + words.remove(rm); + names.remove(rm); + System.out.println(rm+"번 명언이 삭제되었습니다."); + Path path = Paths.get("E:\\JavaStudy\\quotesList\\db\\wiseSaying\\"+rm+".json"); + try { + Files.delete(path); + } catch (IOException e){ + e.printStackTrace(); + } + } + else + System.out.println(rm+"번 명언은 존재하지 않습니다."); + + } + else if(command.contains("수정")){ + int rp = Integer.parseInt(command.split("=")[1]); + if(words.containsKey(rp)){ + System.out.println("명언(기존) : " + words.get(rp)); + System.out.print("명언 : "); + String word = in.nextLine(); + System.out.println("작가(기존) : " + names.get(rp)); + System.out.print("작가 : "); + String name = in.nextLine(); + words.put(rp, word); + names.put(rp, name); + file_save(rp, word, name); + } + else + System.out.println(rp+"번 명언은 존재하지 않습니다."); + } + else if(command.equals("빌드")){ + JSONArray jsonArray = new JSONArray(); + for(int key : words.keySet()){ + JSONObject obj = new JSONObject(); + obj.put("id", key); + obj.put("content", words.get(key)); + obj.put("author", names.get(key)); + jsonArray.add(obj); + } + try { + FileWriter file = new FileWriter("E:\\JavaStudy\\quotesList\\db\\wiseSaying\\data.json"); + file.write(jsonArray.toJSONString()); + file.flush(); + file.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + + } + + public static boolean isNumberic(String str) { + return str.matches("[+-]?\\d*(\\.\\d+)?"); + } + + public static void file_save(int number, String word, String name){ + JSONObject obj = new JSONObject(); + obj.put("id", number); + obj.put("content", word); + obj.put("author", name); + try { + FileWriter file = new FileWriter("E:\\JavaStudy\\quotesList\\db\\wiseSaying\\"+number+".json"); + file.write(obj.toJSONString()); + file.flush(); + file.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) throws IOException, ParseException { + quotes(); + } +} \ No newline at end of file diff --git a/main/java/wiseSaying/App.java b/main/java/wiseSaying/App.java new file mode 100644 index 0000000..4238900 --- /dev/null +++ b/main/java/wiseSaying/App.java @@ -0,0 +1,34 @@ +package wiseSaying; + +import org.json.simple.parser.ParseException; +import java.io.*; +import java.util.*; + +public class App { + public final wiseSayingController controller; + public App(){ + this.controller = new wiseSayingController(); + } + public void run() throws IOException { + int number = 0; + String[] commands = {"등록", "종료", "삭제", "수정", "빌드", "목록"}; + Scanner in = new Scanner(System.in); + String command = ""; + + while(true){ + System.out.println("== 명언 앱 =="); + System.out.print("명령) "); + command = in.nextLine(); + + if(command.length() < 2 || !Arrays.asList(commands).contains(command.substring(0, 2))){ + System.out.println("존재하지 않는 커맨드입니다."); + } + else if(command.equals("종료")){ + break; + } + else{ + controller.control(command); + } + } + } +} diff --git a/main/java/wiseSaying/Main.java b/main/java/wiseSaying/Main.java new file mode 100644 index 0000000..491987c --- /dev/null +++ b/main/java/wiseSaying/Main.java @@ -0,0 +1,13 @@ +package wiseSaying; + +import org.json.simple.parser.ParseException; + +import java.io.IOException; + +public class Main { + public static App app = new App(); + + public static void main(String[] args) throws IOException { + app.run(); + } +} diff --git a/main/java/wiseSaying/WiseSaying.java b/main/java/wiseSaying/WiseSaying.java new file mode 100644 index 0000000..01cd591 --- /dev/null +++ b/main/java/wiseSaying/WiseSaying.java @@ -0,0 +1,19 @@ +package wiseSaying; + +class WiseSaying { + int id; + String content; + String author; + + public int getId() { + return id; + } + + public String getAuthor() { + return author; + } + + public String getContent() { + return content; + } +} diff --git a/main/java/wiseSaying/wiseSayingController.java b/main/java/wiseSaying/wiseSayingController.java new file mode 100644 index 0000000..b7dffac --- /dev/null +++ b/main/java/wiseSaying/wiseSayingController.java @@ -0,0 +1,95 @@ +package wiseSaying; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +public class wiseSayingController { + public final Scanner in; + public final wiseSayingService wiseSayingService; + + public wiseSayingController(){ + this.in = new Scanner(System.in); + this.wiseSayingService = new wiseSayingService(); + } + + public void control(String command) throws IOException { + + if(command.equals("등록")){ + System.out.print("명언 : "); + String content = in.nextLine(); + System.out.print("작가 : "); + String author = in.nextLine(); + + wiseSayingService.addSaying(content, author); + System.out.println(wiseSayingService.getLastId()+ "번 명언이 등록되었습니다."); + } + else if(command.startsWith("목록?keywordType")){ + String[] commands = command.split("="); + String keywordType = commands[1].split("&")[0]; + String keyword = commands[2]; + + System.out.println("번호 / 작가 / 명언"); + List wiseSayingList = wiseSayingService.searchSaying(keywordType, keyword); + for(WiseSaying wiseSaying : wiseSayingList){ + System.out.println(wiseSaying.getId() + " / " + wiseSaying.getAuthor() + " / " + wiseSaying.getContent()); + } + } + else if(command.startsWith("목록")){ + int page = 1; + + if(command.contains("?")) + page = Integer.parseInt(command.split("=")[1]); + + System.out.println("번호 / 작가 / 명언"); + System.out.println("----------------"); + + List wiseSayingList = wiseSayingService.getPageWiseSayings(page); + for(WiseSaying wiseSaying : wiseSayingList){ + System.out.println(wiseSaying.getId() + " / " + wiseSaying.getAuthor() + " / " + wiseSaying.getContent()); + } + System.out.println("----------------"); + System.out.print("페이지 : "); + + int maxPage = wiseSayingService.getMaxPage(); + for(int i = 1; i <= maxPage; i++){ + if(i == page) + System.out.print(" ["+ i + "] "); + else + System.out.print(" "+i+" "); + + if(i != maxPage) + System.out.print("/"); + else + System.out.println(); + } + } + else if(command.startsWith("삭제?")){ + int rm = Integer.parseInt(command.split("=")[1]); + if(wiseSayingService.contentExists(rm)) { + wiseSayingService.deleteSaying(rm); + } + else + System.out.println(rm+"번 명언은 존재하지 않습니다."); + } + else if(command.startsWith("수정?")){ + int rp = Integer.parseInt(command.split("=")[1]); + if(wiseSayingService.contentExists(rp)){ + Map wiseSayingMap = wiseSayingService.getWiseSayings(); + System.out.println("명언(기존) : " + wiseSayingMap.get(rp).getContent()); + System.out.print("명언 : "); + String content = in.nextLine(); + System.out.println("작가(기존) : " + wiseSayingMap.get(rp).getAuthor()); + System.out.print("작가 : "); + String author = in.nextLine(); + wiseSayingService.updateSaying(rp, content, author); + } + else + System.out.println(rp+"번 명언은 존재하지 않습니다."); + } + else if(command.equals("빌드")){ + wiseSayingService.build(); + } + } +} diff --git a/main/java/wiseSaying/wiseSayingRepository.java b/main/java/wiseSaying/wiseSayingRepository.java new file mode 100644 index 0000000..386cf40 --- /dev/null +++ b/main/java/wiseSaying/wiseSayingRepository.java @@ -0,0 +1,127 @@ +package wiseSaying; + +import org.json.simple.JSONArray; +import org.json.simple.parser.JSONParser; + +import org.json.simple.JSONObject; +import org.json.simple.parser.ParseException; +import java.io.*; +import java.util.*; + +public class wiseSayingRepository { + private final String dirPath = "src\\main\\resources\\db\\wiseSaying"; + private final Map wiseSayings = new HashMap(); + private int lastId = 0; + + public wiseSayingRepository(){ + loadContents(); + } + + private void loadContents() { + File dir = new File(dirPath); + File[] files = dir.listFiles(); + + if (files == null) return; + + JSONParser parser = new JSONParser(); + + for (File file : files) { + String fileName = file.getName().split("\\.")[0]; + if (isNumeric(fileName)) { + try (Reader reader = new FileReader(file)) { + JSONObject jsonObject = (JSONObject) parser.parse(reader); + int id = Integer.parseInt(jsonObject.get("id").toString()); + String content = (String) jsonObject.get("content"); + String author = (String) jsonObject.get("author"); + + WiseSaying w = new WiseSaying(); + w.id = id; + w.content = content; + w.author = author; + + wiseSayings.put(id, w); + + lastId = Math.max(lastId, id); + } catch (IOException | ParseException e) { + e.printStackTrace(); + } + } + } + } + + public void saveContent(int id, String content, String author){ + WiseSaying w = new WiseSaying(); + w.id = id; + w.content = content; + w.author = author; + + wiseSayings.put(id, w); + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("id", id); + jsonObject.put("content", content); + jsonObject.put("author", author); + + try (FileWriter file = new FileWriter(dirPath + "\\" + id + ".json")) { + file.write(jsonObject.toJSONString()); + file.flush(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void saveLastId() throws IOException { + File f = new File(dirPath+"\\lastId.txt"); + FileWriter fw = new FileWriter(f, false) ; + fw.write(lastId + ""); + fw.flush(); + fw.close(); + } + + public void deleteContent(int id) { + wiseSayings.remove(id); + + File file = new File(dirPath + "\\" + id + ".json"); + if (file.exists()) { + file.delete(); + } + } + + public void buildJson() { + JSONArray jsonArray = new JSONArray(); + for (WiseSaying wiseSaying : wiseSayings.values()) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("id", wiseSaying.getId()); + jsonObject.put("content", wiseSaying.getContent()); + jsonObject.put("author", wiseSaying.getAuthor()); + jsonArray.add(jsonObject); + } + + try (FileWriter file = new FileWriter(dirPath + "\\data.json")) { + file.write(jsonArray.toJSONString()); + file.flush(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public boolean contentExists(int id) { + return wiseSayings.containsKey(id); + } + + public int plusLastId(){ + return ++lastId; + } + + public int getLastId(){ + return lastId; + } + + public Map getWiseSayings(){ + return wiseSayings; + } + + private boolean isNumeric(String str) { + return str.matches("[+-]?\\d*(\\.\\d+)?"); + } +} diff --git a/main/java/wiseSaying/wiseSayingService.java b/main/java/wiseSaying/wiseSayingService.java new file mode 100644 index 0000000..3c37233 --- /dev/null +++ b/main/java/wiseSaying/wiseSayingService.java @@ -0,0 +1,88 @@ +package wiseSaying; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class wiseSayingService { + public final wiseSayingRepository wiseSayingRepository; + + public wiseSayingService(){ + this.wiseSayingRepository = new wiseSayingRepository(); + } + + public void addSaying(String content, String author) throws IOException { + int id = wiseSayingRepository.plusLastId(); + wiseSayingRepository.saveContent(id, content, author); + wiseSayingRepository.saveLastId(); + } + + public void deleteSaying(int number){ + wiseSayingRepository.deleteContent(number); + } + + public void updateSaying(int id, String content, String author){ + wiseSayingRepository.saveContent(id, content, author); + } + + public List searchSaying(String keywordType, String keyword){ + List result = new ArrayList<>(); + Map wiseSayingMap = wiseSayingRepository.getWiseSayings(); + + for(WiseSaying wiseSaying : wiseSayingMap.values()){ + if("content".equalsIgnoreCase(keywordType) && wiseSaying.getContent().contains(keyword)){ + result.add(wiseSaying); + } + else if("author".equalsIgnoreCase(keywordType) && wiseSaying.getAuthor().contains(keyword)){ + result.add(wiseSaying); + } + } + + return result; + } + + public List getPageWiseSayings(int page){ + final int pageSize = 5; + Map wiseSayingMap = wiseSayingRepository.getWiseSayings(); + + List sayingList = new ArrayList<>(wiseSayingMap.values()); + sayingList.sort((a, b) -> Integer.compare(b.getId(), a.getId())); + + int start = (page - 1) * pageSize; + int end = Math.min(start + pageSize, sayingList.size()); + + if(start >= sayingList.size()){ + return Collections.emptyList(); + } + + return sayingList.subList(start, end); + } + + public int getMaxPage(){ + final int pageSize = 5; + + int MaxPage = wiseSayingRepository.getWiseSayings().size() / 5; + if(wiseSayingRepository.getWiseSayings().size() % 5 != 0) + MaxPage++; + + return MaxPage; + } + + public Map getWiseSayings(){ + return wiseSayingRepository.getWiseSayings(); + } + + public boolean contentExists(int id) { + return wiseSayingRepository.contentExists(id); + } + + public void build(){ + wiseSayingRepository.buildJson(); + } + + public int getLastId(){ + return wiseSayingRepository.getLastId(); + } +} diff --git a/main/resources/db/wiseSaying/10.json b/main/resources/db/wiseSaying/10.json new file mode 100644 index 0000000..749f171 --- /dev/null +++ b/main/resources/db/wiseSaying/10.json @@ -0,0 +1 @@ +{"author":"작가4","id":10,"content":"명언4"} \ No newline at end of file diff --git a/main/resources/db/wiseSaying/2.json b/main/resources/db/wiseSaying/2.json new file mode 100644 index 0000000..003b002 --- /dev/null +++ b/main/resources/db/wiseSaying/2.json @@ -0,0 +1 @@ +{"author":"하이","id":2,"content":"하이호"} \ No newline at end of file diff --git a/main/resources/db/wiseSaying/3.json b/main/resources/db/wiseSaying/3.json new file mode 100644 index 0000000..b31ee8b --- /dev/null +++ b/main/resources/db/wiseSaying/3.json @@ -0,0 +1 @@ +{"author":"안녕","id":3,"content":"안녕합니다"} diff --git a/main/resources/db/wiseSaying/5.json b/main/resources/db/wiseSaying/5.json new file mode 100644 index 0000000..76d6338 --- /dev/null +++ b/main/resources/db/wiseSaying/5.json @@ -0,0 +1 @@ +{"author":"반가워","id":5,"content":"안녕"} \ No newline at end of file diff --git a/main/resources/db/wiseSaying/6.json b/main/resources/db/wiseSaying/6.json new file mode 100644 index 0000000..3824132 --- /dev/null +++ b/main/resources/db/wiseSaying/6.json @@ -0,0 +1 @@ +{"author":"반가워 반가워","id":6,"content":"안녕 안녕"} \ No newline at end of file diff --git a/main/resources/db/wiseSaying/7.json b/main/resources/db/wiseSaying/7.json new file mode 100644 index 0000000..00ffd18 --- /dev/null +++ b/main/resources/db/wiseSaying/7.json @@ -0,0 +1 @@ +{"author":"작가1","id":7,"content":"명언1"} \ No newline at end of file diff --git a/main/resources/db/wiseSaying/8.json b/main/resources/db/wiseSaying/8.json new file mode 100644 index 0000000..3d6e296 --- /dev/null +++ b/main/resources/db/wiseSaying/8.json @@ -0,0 +1 @@ +{"author":"작가2","id":8,"content":"명언2"} \ No newline at end of file diff --git a/main/resources/db/wiseSaying/9.json b/main/resources/db/wiseSaying/9.json new file mode 100644 index 0000000..ebd18b8 --- /dev/null +++ b/main/resources/db/wiseSaying/9.json @@ -0,0 +1 @@ +{"author":"작가3","id":9,"content":"명언3"} \ No newline at end of file diff --git a/main/resources/db/wiseSaying/data.json b/main/resources/db/wiseSaying/data.json new file mode 100644 index 0000000..97d9003 --- /dev/null +++ b/main/resources/db/wiseSaying/data.json @@ -0,0 +1 @@ +[{"author":"하이","id":2,"content":"하이호"},{"author":"안녕하다구요","id":3,"content":"안녕합니다"},{"author":"구들아","id":4,"content":"반갑다"},{"author":"반가워","id":5,"content":"안녕"},{"author":"반가워 반가워","id":6,"content":"안녕 안녕"}] \ No newline at end of file diff --git a/main/resources/db/wiseSaying/lastId.txt b/main/resources/db/wiseSaying/lastId.txt new file mode 100644 index 0000000..9a03714 --- /dev/null +++ b/main/resources/db/wiseSaying/lastId.txt @@ -0,0 +1 @@ +10 \ No newline at end of file