-
Notifications
You must be signed in to change notification settings - Fork 28
[BE5] 윤상민 #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
skvhffpdyd
wants to merge
2
commits into
chacha86:main
Choose a base branch
from
skvhffpdyd:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[BE5] 윤상민 #7
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Integer,String> words = new HashMap<>(); | ||
| Map<Integer,String> 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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<WiseSaying> 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<WiseSaying> 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<Integer, WiseSaying> 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(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
입력 가능한 command를 배열로 정리하여 한 눈에 보기에 편하네요