Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions main/java/org/example/Main.java
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();
}
}
34 changes: 34 additions & 0 deletions main/java/wiseSaying/App.java
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 = {"등록", "종료", "삭제", "수정", "빌드", "목록"};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

입력 가능한 command를 배열로 정리하여 한 눈에 보기에 편하네요

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);
}
}
}
}
13 changes: 13 additions & 0 deletions main/java/wiseSaying/Main.java
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();
}
}
19 changes: 19 additions & 0 deletions main/java/wiseSaying/WiseSaying.java
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;
}
}
95 changes: 95 additions & 0 deletions main/java/wiseSaying/wiseSayingController.java
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();
}
}
}
Loading