From ba8420fc913ccbb4b5c91f35f13d8e7ddf6fcb87 Mon Sep 17 00:00:00 2001 From: joseph Date: Thu, 19 Dec 2024 19:59:46 +0900 Subject: [PATCH 01/13] firstsetting --- .gitignore | 38 +++++++++++++++++++++++++++++ .idea/.gitignore | 8 ++++++ .idea/misc.xml | 14 +++++++++++ .idea/vcs.xml | 6 +++++ pom.xml | 14 +++++++++++ src/main/java/org/example/Main.java | 17 +++++++++++++ 6 files changed, 97 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/main/java/org/example/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..1c85357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f7acc6d --- /dev/null +++ b/pom.xml @@ -0,0 +1,14 @@ + + 4.0.0 + + com.example + codereview + 1.0-SNAPSHOT + + + 1.8 + 1.8 + + \ No newline at end of file diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java new file mode 100644 index 0000000..a02700f --- /dev/null +++ b/src/main/java/org/example/Main.java @@ -0,0 +1,17 @@ +package org.example; + +//TIP To Run code, press or +// click the icon in the gutter. +public class Main { + public static void main(String[] args) { + //TIP Press with your caret at the highlighted text + // to see how IntelliJ IDEA suggests fixing it. + System.out.printf("Hello and welcome!"); + + for (int i = 1; i <= 5; i++) { + //TIP Press to start debugging your code. We have set one breakpoint + // for you, but you can always add more by pressing . + System.out.println("i = " + i); + } + } +} \ No newline at end of file From 1c57815d6db5bec6bc741fdfa24f7881d0aab1fe Mon Sep 17 00:00:00 2001 From: joseph Date: Fri, 20 Dec 2024 11:05:17 +0900 Subject: [PATCH 02/13] =?UTF-8?q?feat:=20Lv1=20=EA=B5=AC=ED=98=84(#1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/example/Lv1.java | 12 ++++++++++++ src/main/java/org/example/Main.java | 11 ++--------- 2 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/example/Lv1.java diff --git a/src/main/java/org/example/Lv1.java b/src/main/java/org/example/Lv1.java new file mode 100644 index 0000000..0867f6a --- /dev/null +++ b/src/main/java/org/example/Lv1.java @@ -0,0 +1,12 @@ +package org.example; + +import java.util.Scanner; + +public class Lv1 { + + public static void doLv1(){ + Scanner scan = new Scanner(System.in); + String str = scan.next(); + System.out.println(str); + } +} diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index a02700f..4eea657 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -4,14 +4,7 @@ // click the icon in the gutter. public class Main { public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); - - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); - } + Lv1 lv1 = new Lv1(); + lv1.doLv1(); } } \ No newline at end of file From 774d14ca4deb5da16317a931c263b41cef4eb089 Mon Sep 17 00:00:00 2001 From: joseph Date: Mon, 23 Dec 2024 14:27:30 +0900 Subject: [PATCH 03/13] =?UTF-8?q?feat:=20Lv2=20=EA=B5=AC=ED=98=84(#2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 ++ src/main/java/org/example/Lv1.java | 2 +- src/main/java/org/example/Lv2.java | 28 ++++++++++++++++++++++++++++ src/main/java/org/example/Main.java | 5 +++-- 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/example/Lv2.java diff --git a/pom.xml b/pom.xml index f7acc6d..5c54067 100644 --- a/pom.xml +++ b/pom.xml @@ -11,4 +11,6 @@ 1.8 1.8 + + \ No newline at end of file diff --git a/src/main/java/org/example/Lv1.java b/src/main/java/org/example/Lv1.java index 0867f6a..f9fbfce 100644 --- a/src/main/java/org/example/Lv1.java +++ b/src/main/java/org/example/Lv1.java @@ -4,7 +4,7 @@ public class Lv1 { - public static void doLv1(){ + public void doLv1(){ Scanner scan = new Scanner(System.in); String str = scan.next(); System.out.println(str); diff --git a/src/main/java/org/example/Lv2.java b/src/main/java/org/example/Lv2.java new file mode 100644 index 0000000..98a3d43 --- /dev/null +++ b/src/main/java/org/example/Lv2.java @@ -0,0 +1,28 @@ +package org.example; + +import java.util.Scanner; + +public class Lv2 { + public void doLv2(){ + Scanner scan = new Scanner(System.in); + + System.out.println("== 명언 앱 =="); + while(true) { + System.out.printf("명령) "); + String order = scan.nextLine().trim(); //next() 와 nextLine() 혼용 X - 혼용 시 Scanner 버퍼문제 가능성 + String quote = ""; + String name = ""; + if (order.equals("등록")) { + System.out.printf("명언 : "); + quote = scan.nextLine().trim(); + + System.out.printf("작가 : "); + name = scan.nextLine().trim(); + + } + else if (order.equals("종료")) { + return; + } + } + } +} diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 4eea657..ae6ec4f 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -4,7 +4,8 @@ // click the icon in the gutter. public class Main { public static void main(String[] args) { - Lv1 lv1 = new Lv1(); - lv1.doLv1(); + + Lv2 lv2 = new Lv2(); + lv2.doLv2(); } } \ No newline at end of file From f7d09398a1d91c4f67fa824814613b46a458fe09 Mon Sep 17 00:00:00 2001 From: joseph Date: Mon, 23 Dec 2024 15:06:28 +0900 Subject: [PATCH 04/13] =?UTF-8?q?feat:=20Lv3=20=EA=B5=AC=ED=98=84(#5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/example/Lv3.java | 38 +++++++++++++++++++++++++++++ src/main/java/org/example/Main.java | 4 +-- 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/example/Lv3.java diff --git a/src/main/java/org/example/Lv3.java b/src/main/java/org/example/Lv3.java new file mode 100644 index 0000000..6fd38c2 --- /dev/null +++ b/src/main/java/org/example/Lv3.java @@ -0,0 +1,38 @@ +package org.example; + +import java.util.Scanner; + +public class Lv3 { + public void doLv3(){ + String order=""; + String quote=""; + String name=""; + int count = 0; + Scanner scan = new Scanner(System.in); + + System.out.println("== 명언 앱 =="); + + + while(true){ + System.out.printf("명령: "); + order = scan.nextLine().trim(); + if(order.equals("등록")){ + System.out.printf("명언: "); + quote=scan.nextLine().trim(); + System.out.printf("작가: "); + name=scan.nextLine().trim(); + + count++; + + System.out.println(count + "번 명언이 등록되었습니다."); + return; + } + else if(order.equals("종료")){ + return; + } + + } + + + } +} diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index ae6ec4f..97e5eff 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -5,7 +5,7 @@ public class Main { public static void main(String[] args) { - Lv2 lv2 = new Lv2(); - lv2.doLv2(); + Lv3 lv3 = new Lv3(); + lv3.doLv3(); } } \ No newline at end of file From a4a36b39d6f6a5c9e03c00cae7b25952a2195e9c Mon Sep 17 00:00:00 2001 From: joseph Date: Mon, 23 Dec 2024 15:27:18 +0900 Subject: [PATCH 05/13] =?UTF-8?q?feat:=20Lv4=20=EA=B5=AC=ED=98=84(#6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/example/Lv4.java | 42 +++++++++++++++++++++++++++++ src/main/java/org/example/Main.java | 4 +-- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/example/Lv4.java diff --git a/src/main/java/org/example/Lv4.java b/src/main/java/org/example/Lv4.java new file mode 100644 index 0000000..3fa4909 --- /dev/null +++ b/src/main/java/org/example/Lv4.java @@ -0,0 +1,42 @@ +package org.example; + +import java.util.*; + +class QuoteLv4{ // 관리를 위해 명언-작가 저장하는 클래스 따로 작성 + String quote; + String author; + int quoteCnt = 0; + QuoteLv4(String quote, String author){ + this.quote = quote; + this.author = author; + } +} +public class Lv4 { + + public void doLv4(){ + int actCnt = 0; + Scanner scan=new Scanner(System.in); + List QL4= new ArrayList<>(); + + while(true){ + System.out.printf("명령) "); + String order=scan.nextLine().trim(); + if(order.equals("등록")){ + System.out.printf("명언 : "); + String quote = scan.nextLine().trim(); + System.out.printf("작가 : "); + String author = scan.nextLine().trim(); + + QL4.add(new QuoteLv4(quote,author)); + actCnt++; + + System.out.println(actCnt+"번 명령이 등록되었습니다."); + } + else if(order.equals("종료")){ + return; + } + + } + + } +} diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 97e5eff..532b3bb 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -5,7 +5,7 @@ public class Main { public static void main(String[] args) { - Lv3 lv3 = new Lv3(); - lv3.doLv3(); + Lv4 lv4 = new Lv4(); + lv4.doLv4(); } } \ No newline at end of file From 3ad8a6d4be5e766e4923319806652ec72fddd48f Mon Sep 17 00:00:00 2001 From: joseph Date: Mon, 23 Dec 2024 16:42:34 +0900 Subject: [PATCH 06/13] =?UTF-8?q?feat:=20Lv5=20=EA=B5=AC=ED=98=84(#7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/example/Lv5.java | 67 +++++++++++++++++++++++++++++ src/main/java/org/example/Main.java | 4 +- 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/example/Lv5.java diff --git a/src/main/java/org/example/Lv5.java b/src/main/java/org/example/Lv5.java new file mode 100644 index 0000000..3f993bd --- /dev/null +++ b/src/main/java/org/example/Lv5.java @@ -0,0 +1,67 @@ +package org.example; + +import java.util.*; + +class QuoteLv5{ + int id; // Lv4의 actCnt처럼 몇번 째로 등록이 아닌, 명언별로 번호를 달아주기위해 추가 + String quote; + String author; + + //생성자 -> 생성자를 사용하지 않으면 객체 생성 후 별도로 필드를 초기화해야 하므로 번거로움 + QuoteLv5(int id, String quote, String name){ + this.id=id; + this.quote=quote; + this.author=author; + } + + //List나 다른 컬렉션에서 객체를 출력하려고 할 때, 각 객체의 toString() 메서드가 자동으로 호출되므로 toString()을 오버라이딩 + //오버라이딩 하지 않으면 클래스명@주소값 이 반환됨. + @Override + public String toString(){ + return id+" / "+author+" / "+quote; + } +} + +public class Lv5 { + Scanner scan = new Scanner(System.in); + List QL5 = new ArrayList<>(); + + public void doLv5(){ + int id = 1; + System.out.println("== 명언 앱 =="); + + while(true){ + System.out.printf("명령) "); + String order=scan.nextLine().trim(); + if(order.equals("등록")){ + System.out.printf("명언 : "); + String quote = scan.nextLine().trim(); + System.out.printf("작가 : "); + String author = scan.nextLine().trim(); + + QL5.add(new QuoteLv5(id,quote,author)); + System.out.println(id++ +"번 명언이 등록되었습니다."); + } + else if(order.equals("목록")){ + System.out.println("번호 / 작가 / 명언"); + System.out.println("----------------------"); + if(QL5.isEmpty()) { + System.out.println("등록된 명언이 없습니다."); + } + else{ + for(int i=QL5.size()-1;i>=0;i--){ + System.out.println(QL5.get(i)); + } + } + } + else if(order.equals("종료")){ + return; + } + else{ + System.out.println("유효하지 않은 명령어입니다."); + } + } + + } + +} diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 532b3bb..038779c 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -5,7 +5,7 @@ public class Main { public static void main(String[] args) { - Lv4 lv4 = new Lv4(); - lv4.doLv4(); + Lv5 lv5 = new Lv5(); + lv5.doLv5(); } } \ No newline at end of file From f05ae4295c8092a73f5b74528094f50d8718817a Mon Sep 17 00:00:00 2001 From: joseph Date: Mon, 23 Dec 2024 17:32:45 +0900 Subject: [PATCH 07/13] =?UTF-8?q?feat:=20Lv6=20=EA=B5=AC=ED=98=84(#8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/example/Lv6.java | 82 +++++++++++++++++++++++++++++ src/main/java/org/example/Main.java | 4 +- 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/example/Lv6.java diff --git a/src/main/java/org/example/Lv6.java b/src/main/java/org/example/Lv6.java new file mode 100644 index 0000000..5ebbb61 --- /dev/null +++ b/src/main/java/org/example/Lv6.java @@ -0,0 +1,82 @@ +package org.example; + +import java.util.*; + +class QuoteLv6{ + int id; // Lv4의 actCnt처럼 몇번 째로 등록이 아닌, 명언별로 번호를 달아주기위해 추가 + String quote; + String author; + + //생성자 -> 생성자를 사용하지 않으면 객체 생성 후 별도로 필드를 초기화해야 하므로 번거로움 + QuoteLv6(int id, String quote, String name){ + this.id=id; + this.quote=quote; + this.author=author; + } + + //List나 다른 컬렉션에서 객체를 출력하려고 할 때, 각 객체의 toString() 메서드가 자동으로 호출되므로 toString()을 오버라이딩 + //오버라이딩 하지 않으면 클래스명@주소값 이 반환됨. + @Override + public String toString(){ + return id+" / "+author+" / "+quote; + } +} + +public class Lv6 { + Scanner scan = new Scanner(System.in); + List QL6 = new ArrayList<>(); + + public void doLv6(){ + int id = 1; + System.out.println("== 명언 앱 =="); + + while(true){ + System.out.printf("명령) "); + String order=scan.nextLine().trim(); + if(order.equals("등록")){ + System.out.printf("명언 : "); + String quote = scan.nextLine().trim(); + System.out.printf("작가 : "); + String author = scan.nextLine().trim(); + + QL6.add(new QuoteLv6(id,quote,author)); + System.out.println(id++ +"번 명언이 등록되었습니다."); + } + else if(order.equals("목록")){ + System.out.println("번호 / 작가 / 명언"); + System.out.println("----------------------"); + if(QL6.isEmpty()) { + System.out.println("등록된 명언이 없습니다."); + } + else{ + for(int i=QL6.size()-1;i>=0;i--){ + System.out.println(QL6.get(i)); + } + } + } + else if(order.equals("종료")){ + return; + } + else if(order.startsWith("삭제?id=")){ //startsWith 메서드를 사용. + int deleteId = Integer.parseInt(order.split("=")[1]); + boolean ifFound=false; + for(int i=QL6.size()-1;i>=0;i--){ + if(QL6.get(i).id==deleteId){ + QL6.remove(i); + System.out.println(deleteId + "번 명언이 삭제되었습니다."); + ifFound=true; + break; + } + } + if(!ifFound){ + System.out.println(deleteId + "번 명언은 목록에 없습니다."); + } + } + else{ + System.out.println("유효하지 않은 명령어입니다."); + } + } + + } + +} diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 038779c..9d10207 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -5,7 +5,7 @@ public class Main { public static void main(String[] args) { - Lv5 lv5 = new Lv5(); - lv5.doLv5(); + Lv6 lv6 = new Lv6(); + lv6.doLv6(); } } \ No newline at end of file From aeacce51199175870e001eaab5a002b1ce4bdf71 Mon Sep 17 00:00:00 2001 From: joseph Date: Mon, 23 Dec 2024 17:50:41 +0900 Subject: [PATCH 08/13] =?UTF-8?q?feat:=20Lv7=20=EA=B5=AC=ED=98=84,=20refac?= =?UTF-8?q?:=20Lv6=20=EC=82=AD=EC=A0=9C=20=EB=A1=9C=EC=A7=81=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EC=B2=98=EB=A6=AC=20=EB=B0=8F=20=EB=9E=8C=EB=8B=A4?= =?UTF-8?q?=EC=8B=9D=20=EC=A0=81=EC=9A=A9=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/example/Lv7.java | 82 +++++++++++++++++++++++++++++ src/main/java/org/example/Main.java | 4 +- 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/example/Lv7.java diff --git a/src/main/java/org/example/Lv7.java b/src/main/java/org/example/Lv7.java new file mode 100644 index 0000000..85f9f5f --- /dev/null +++ b/src/main/java/org/example/Lv7.java @@ -0,0 +1,82 @@ +package org.example; + +import java.util.*; + +class QuoteLv7{ + int id; // Lv4의 actCnt처럼 몇번 째로 등록이 아닌, 명언별로 번호를 달아주기위해 추가 + String quote; + String author; + + //생성자 -> 생성자를 사용하지 않으면 객체 생성 후 별도로 필드를 초기화해야 하므로 번거로움 + QuoteLv7(int id, String quote, String name){ + this.id=id; + this.quote=quote; + this.author=author; + } + + //List나 다른 컬렉션에서 객체를 출력하려고 할 때, 각 객체의 toString() 메서드가 자동으로 호출되므로 toString()을 오버라이딩 + //오버라이딩 하지 않으면 클래스명@주소값 이 반환됨. + @Override + public String toString(){ + return id+" / "+author+" / "+quote; + } +} + +public class Lv7 { + Scanner scan = new Scanner(System.in); + List QLstLv7 = new ArrayList<>(); + + public void doLv7(){ + int id = 1; + System.out.println("== 명언 앱 =="); + + while(true){ + System.out.printf("명령) "); + String order=scan.nextLine().trim(); + if(order.equals("등록")){ + System.out.printf("명언 : "); + String quote = scan.nextLine().trim(); + System.out.printf("작가 : "); + String author = scan.nextLine().trim(); + + QLstLv7.add(new QuoteLv7(id,quote,author)); + System.out.println(id++ +"번 명언이 등록되었습니다."); + } + else if(order.equals("목록")){ + System.out.println("번호 / 작가 / 명언"); + System.out.println("----------------------"); + if(QLstLv7.isEmpty()) { + System.out.println("등록된 명언이 없습니다."); + } + else{ + for(int i=QLstLv7.size()-1;i>=0;i--){ + System.out.println(QLstLv7.get(i)); + } + } + } + else if(order.equals("종료")){ + return; + } + else if(order.startsWith("삭제?id=")){ //startsWith 메서드를 사용. + try{ + int deleteId = Integer.parseInt(order.split("=")[1]); //이부분 동작에 대해 예외처리 함. + boolean isRemoved = QLstLv7.removeIf(QLstEle -> QLstEle.id == deleteId); + + if (isRemoved) { + System.out.println(deleteId + "번 명언이 삭제되었습니다."); + } else { + System.out.println(deleteId + "번 명언은 존재하지 않습니다."); + } + } + catch(Exception e){ + System.out.println("명령어 형식을 재확인 하십시오. 형식 : 삭제?id=숫자 "); + } + } + else{ + System.out.println("유효하지 않은 명령어입니다."); + } + } + + } + +} diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 9d10207..b66f8ec 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -5,7 +5,7 @@ public class Main { public static void main(String[] args) { - Lv6 lv6 = new Lv6(); - lv6.doLv6(); + Lv7 lv7 = new Lv7(); + lv7.doLv7(); } } \ No newline at end of file From 0394350a3141900f36a8444ee2e0fc989dc1e475 Mon Sep 17 00:00:00 2001 From: joseph Date: Tue, 24 Dec 2024 09:40:12 +0900 Subject: [PATCH 09/13] =?UTF-8?q?feat:=20Lv8=20=EA=B5=AC=ED=98=84=20&=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=EC=9E=90=20=EC=B4=88=EA=B8=B0=ED=99=94=20?= =?UTF-8?q?=EB=B6=80=EB=B6=84=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?(#10)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/example/Lv8.java | 113 ++++++++++++++++++++++++++++ src/main/java/org/example/Main.java | 4 +- 2 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/example/Lv8.java diff --git a/src/main/java/org/example/Lv8.java b/src/main/java/org/example/Lv8.java new file mode 100644 index 0000000..4b5b045 --- /dev/null +++ b/src/main/java/org/example/Lv8.java @@ -0,0 +1,113 @@ +package org.example; + +import java.util.*; + +class QuoteLv8{ + int id; // Lv4의 actCnt처럼 몇번 째로 등록이 아닌, 명언별로 번호를 달아주기위해 추가 + String quote; + String author; + + //생성자 -> 생성자를 사용하지 않으면 객체 생성 후 별도로 필드를 초기화해야 하므로 번거로움 + QuoteLv8(int id, String quote, String author){ + this.id=id; + this.quote=quote; + this.author=author; + } + + //List나 다른 컬렉션에서 객체를 출력하려고 할 때, 각 객체의 toString() 메서드가 자동으로 호출되므로 toString()을 오버라이딩 + //오버라이딩 하지 않으면 클래스명@주소값 이 반환됨. + @Override + public String toString(){ + return id+" / "+author+" / "+quote; + } +} + +public class Lv8 { + Scanner scan = new Scanner(System.in); + List QLstLv8 = new ArrayList<>(); + + public void doLv8(){ + int id = 1; + System.out.println("== 명언 앱 =="); + + while(true){ + System.out.printf("명령) "); + String order=scan.nextLine().trim(); + if(order.equals("등록")){ + System.out.printf("명언 : "); + String quote = scan.nextLine().trim(); + System.out.printf("작가 : "); + String author = scan.nextLine().trim(); + + QLstLv8.add(new QuoteLv8(id,quote,author)); + System.out.println(id++ +"번 명언이 등록되었습니다."); + } + else if(order.equals("목록")){ + System.out.println("번호 / 작가 / 명언"); + System.out.println("----------------------"); + if(QLstLv8.isEmpty()) { + System.out.println("등록된 명언이 없습니다."); + } + else{ + for(int i=QLstLv8.size()-1;i>=0;i--){ + System.out.println(QLstLv8.get(i)); + } + } + } + else if(order.equals("종료")){ + return; + } + else if(order.startsWith("삭제?id=")){ //startsWith 메서드를 사용. + try{ + int deleteId = Integer.parseInt(order.split("=")[1]); //이부분 동작에 대해 예외처리 함. + boolean isRemoved = QLstLv8.removeIf(QLstEle -> QLstEle.id == deleteId); + + if (isRemoved) { + System.out.println(deleteId + "번 명언이 삭제되었습니다."); + } else { + System.out.println(deleteId + "번 명언은 존재하지 않습니다."); + } + } + catch(Exception e){ + System.out.println("명령어 형식을 재확인 하십시오. 형식 : 삭제?id=숫자 "); + } + } + else if(order.startsWith("수정?id=")){ //startsWith 메서드를 사용. + try{ + int modifyId = Integer.parseInt(order.split("=")[1]); //이부분 동작에 대해 예외처리 함. + boolean isExist = false; //존재여부 확인용 + + for(int i=0 ; i Date: Thu, 2 Jan 2025 10:08:38 +0900 Subject: [PATCH 10/13] refac: Lv8 - function method divided(#12) --- .idea/uiDesigner.xml | 124 +++++++++++++++++ pom.xml | 11 +- src/main/java/org/example/ConsistanceLv9.java | 35 +++++ src/main/java/org/example/Lv8.java | 1 + src/main/java/org/example/Lv9.java | 130 ++++++++++++++++++ src/main/java/org/example/Main.java | 4 +- 6 files changed, 302 insertions(+), 3 deletions(-) create mode 100644 .idea/uiDesigner.xml create mode 100644 src/main/java/org/example/ConsistanceLv9.java create mode 100644 src/main/java/org/example/Lv9.java diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5c54067..688ef93 100644 --- a/pom.xml +++ b/pom.xml @@ -7,10 +7,19 @@ codereview 1.0-SNAPSHOT + + 1.8 1.8 + + + com.googlecode.json-simple + json-simple + 1.1.1 + + - \ No newline at end of file + diff --git a/src/main/java/org/example/ConsistanceLv9.java b/src/main/java/org/example/ConsistanceLv9.java new file mode 100644 index 0000000..7628a22 --- /dev/null +++ b/src/main/java/org/example/ConsistanceLv9.java @@ -0,0 +1,35 @@ +package org.example; + +import org.json.simple.*; +import org.json.simple.parser.*; + +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +public class ConsistanceLv9 { + + // JSON 파일 읽기 + public JSONObject readJSON(String filePath) { + JSONParser parser = new JSONParser(); + try (FileReader reader = new FileReader(filePath)) { // try-with-resources 사용 + Object obj = parser.parse(reader); + return (JSONObject) obj; + } catch (IOException | ParseException e) { + e.printStackTrace(); + return null; // 오류 발생 시 null 반환 + } + } + + // JSON 파일 쓰기 + public void writeJSON(String filePath, JSONObject jsonObject) { + try (FileWriter writer = new FileWriter(filePath)) { // try-with-resources 사용 + writer.write(jsonObject.toJSONString()); + writer.flush(); + System.out.println("JSON 파일 저장 완료: " + filePath); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/src/main/java/org/example/Lv8.java b/src/main/java/org/example/Lv8.java index 4b5b045..2d64894 100644 --- a/src/main/java/org/example/Lv8.java +++ b/src/main/java/org/example/Lv8.java @@ -110,4 +110,5 @@ else if(order.startsWith("수정?id=")){ //startsWith 메서드를 사용. } + } diff --git a/src/main/java/org/example/Lv9.java b/src/main/java/org/example/Lv9.java new file mode 100644 index 0000000..80023ee --- /dev/null +++ b/src/main/java/org/example/Lv9.java @@ -0,0 +1,130 @@ +package org.example; + +import java.util.*; + +//메인 기능 +class QuoteLv9 { + int id; + String quote; + String author; + + QuoteLv9(int id, String quote, String author) { + this.id = id; + this.quote = quote; + this.author = author; + } + + @Override + public String toString() { + return id + " / " + author + " / " + quote; + } +} + +public class Lv9 { + private final Scanner scan = new Scanner(System.in); + private final List quotes = new ArrayList<>(); + private int nextId = 1; + + public void doLv9() { + System.out.println("== 명언 앱 =="); + + while (true) { + System.out.printf("명령) "); + String command = scan.nextLine().trim(); + + switch (command.split("\\?")[0]) { + case "등록": + registerQuote(); + break; + case "목록": + listQuotes(); + break; + case "종료": + System.out.println("프로그램을 종료합니다."); + return; + case "삭제": + deleteQuote(command); + break; + case "수정": + modifyQuote(command); + break; + default: + System.out.println("유효하지 않은 명령어입니다."); + } + } + } + + private void registerQuote() { + System.out.printf("명언 : "); + String quoteText = scan.nextLine().trim(); + System.out.printf("작가 : "); + String author = scan.nextLine().trim(); + + quotes.add(new QuoteLv9(nextId, quoteText, author)); + System.out.println(nextId++ + "번 명언이 등록되었습니다."); + } + + private void listQuotes() { + System.out.println("번호 / 작가 / 명언"); + System.out.println("----------------------"); + + if (quotes.isEmpty()) { + System.out.println("등록된 명언이 없습니다."); + } else { + for (int i = quotes.size() - 1; i >= 0; i--) { + System.out.println(quotes.get(i)); + } + } + } + + private void deleteQuote(String command) { + try { + int idToDelete = getIdFromCommand(command); + boolean removed = quotes.removeIf(quote -> quote.id == idToDelete); + + if (removed) { + System.out.println(idToDelete + "번 명언이 삭제되었습니다."); + } else { + System.out.println(idToDelete + "번 명언은 존재하지 않습니다."); + } + } catch (NumberFormatException e) { + System.out.println("명령어 형식을 재확인 하십시오. 형식 : 삭제?id=숫자"); + } + } + + private void modifyQuote(String command) { + try { + int idToModify = getIdFromCommand(command); + QuoteLv9 targetQuote = findQuoteById(idToModify); + + if (targetQuote != null) { + System.out.println("명언(기존) : " + targetQuote.quote); + System.out.printf("명언 : "); + targetQuote.quote = scan.nextLine().trim(); + + System.out.println("작가(기존) : " + targetQuote.author); + System.out.printf("작가 : "); + targetQuote.author = scan.nextLine().trim(); + + System.out.println(idToModify + "번 명언이 수정되었습니다."); + } else { + System.out.println(idToModify + "번 명언은 존재하지 않습니다."); + } + } catch (NumberFormatException e) { + System.out.println("명령어 형식을 재확인 하십시오. 형식 : 수정?id=숫자"); + } + } + + private int getIdFromCommand(String command) { + return Integer.parseInt(command.split("=")[1].trim()); + } + + private QuoteLv9 findQuoteById(int id) { + for (QuoteLv9 quote : quotes) { + if (quote.id == id) { + return quote; + } + } + return null; + } +} \ No newline at end of file diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 3078430..d79e629 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -5,7 +5,7 @@ public class Main { public static void main(String[] args) { - Lv8 lv8 = new Lv8(); - lv8.doLv8(); + Lv9 lv9 = new Lv9(); + lv9.doLv9(); } } \ No newline at end of file From ae4442b70f83b5a609e8838ccdd6634e750b8a72 Mon Sep 17 00:00:00 2001 From: joseph Date: Thu, 2 Jan 2025 10:29:54 +0900 Subject: [PATCH 11/13] refac: Lv8 - file divided(#12) --- src/main/java/org/example/ConsistanceLv9.java | 35 ------ src/main/java/org/example/Lv1.java | 12 -- src/main/java/org/example/Lv2.java | 28 ----- src/main/java/org/example/Lv3.java | 38 ------ src/main/java/org/example/Lv4.java | 42 ------- src/main/java/org/example/Lv5.java | 67 ---------- src/main/java/org/example/Lv6.java | 82 ------------- src/main/java/org/example/Lv7.java | 82 ------------- src/main/java/org/example/Lv8.java | 114 ------------------ src/main/java/org/example/Main.java | 4 +- src/main/java/org/example/Quote.java | 18 +++ src/main/java/org/example/QuoteApp.java | 41 +++++++ .../{Lv9.java => QuoteController.java} | 85 ++++--------- 13 files changed, 86 insertions(+), 562 deletions(-) delete mode 100644 src/main/java/org/example/ConsistanceLv9.java delete mode 100644 src/main/java/org/example/Lv1.java delete mode 100644 src/main/java/org/example/Lv2.java delete mode 100644 src/main/java/org/example/Lv3.java delete mode 100644 src/main/java/org/example/Lv4.java delete mode 100644 src/main/java/org/example/Lv5.java delete mode 100644 src/main/java/org/example/Lv6.java delete mode 100644 src/main/java/org/example/Lv7.java delete mode 100644 src/main/java/org/example/Lv8.java create mode 100644 src/main/java/org/example/Quote.java create mode 100644 src/main/java/org/example/QuoteApp.java rename src/main/java/org/example/{Lv9.java => QuoteController.java} (53%) diff --git a/src/main/java/org/example/ConsistanceLv9.java b/src/main/java/org/example/ConsistanceLv9.java deleted file mode 100644 index 7628a22..0000000 --- a/src/main/java/org/example/ConsistanceLv9.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.example; - -import org.json.simple.*; -import org.json.simple.parser.*; - -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; - -public class ConsistanceLv9 { - - // JSON 파일 읽기 - public JSONObject readJSON(String filePath) { - JSONParser parser = new JSONParser(); - try (FileReader reader = new FileReader(filePath)) { // try-with-resources 사용 - Object obj = parser.parse(reader); - return (JSONObject) obj; - } catch (IOException | ParseException e) { - e.printStackTrace(); - return null; // 오류 발생 시 null 반환 - } - } - - // JSON 파일 쓰기 - public void writeJSON(String filePath, JSONObject jsonObject) { - try (FileWriter writer = new FileWriter(filePath)) { // try-with-resources 사용 - writer.write(jsonObject.toJSONString()); - writer.flush(); - System.out.println("JSON 파일 저장 완료: " + filePath); - } catch (IOException e) { - e.printStackTrace(); - } - } - -} \ No newline at end of file diff --git a/src/main/java/org/example/Lv1.java b/src/main/java/org/example/Lv1.java deleted file mode 100644 index f9fbfce..0000000 --- a/src/main/java/org/example/Lv1.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.example; - -import java.util.Scanner; - -public class Lv1 { - - public void doLv1(){ - Scanner scan = new Scanner(System.in); - String str = scan.next(); - System.out.println(str); - } -} diff --git a/src/main/java/org/example/Lv2.java b/src/main/java/org/example/Lv2.java deleted file mode 100644 index 98a3d43..0000000 --- a/src/main/java/org/example/Lv2.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.example; - -import java.util.Scanner; - -public class Lv2 { - public void doLv2(){ - Scanner scan = new Scanner(System.in); - - System.out.println("== 명언 앱 =="); - while(true) { - System.out.printf("명령) "); - String order = scan.nextLine().trim(); //next() 와 nextLine() 혼용 X - 혼용 시 Scanner 버퍼문제 가능성 - String quote = ""; - String name = ""; - if (order.equals("등록")) { - System.out.printf("명언 : "); - quote = scan.nextLine().trim(); - - System.out.printf("작가 : "); - name = scan.nextLine().trim(); - - } - else if (order.equals("종료")) { - return; - } - } - } -} diff --git a/src/main/java/org/example/Lv3.java b/src/main/java/org/example/Lv3.java deleted file mode 100644 index 6fd38c2..0000000 --- a/src/main/java/org/example/Lv3.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.example; - -import java.util.Scanner; - -public class Lv3 { - public void doLv3(){ - String order=""; - String quote=""; - String name=""; - int count = 0; - Scanner scan = new Scanner(System.in); - - System.out.println("== 명언 앱 =="); - - - while(true){ - System.out.printf("명령: "); - order = scan.nextLine().trim(); - if(order.equals("등록")){ - System.out.printf("명언: "); - quote=scan.nextLine().trim(); - System.out.printf("작가: "); - name=scan.nextLine().trim(); - - count++; - - System.out.println(count + "번 명언이 등록되었습니다."); - return; - } - else if(order.equals("종료")){ - return; - } - - } - - - } -} diff --git a/src/main/java/org/example/Lv4.java b/src/main/java/org/example/Lv4.java deleted file mode 100644 index 3fa4909..0000000 --- a/src/main/java/org/example/Lv4.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.example; - -import java.util.*; - -class QuoteLv4{ // 관리를 위해 명언-작가 저장하는 클래스 따로 작성 - String quote; - String author; - int quoteCnt = 0; - QuoteLv4(String quote, String author){ - this.quote = quote; - this.author = author; - } -} -public class Lv4 { - - public void doLv4(){ - int actCnt = 0; - Scanner scan=new Scanner(System.in); - List QL4= new ArrayList<>(); - - while(true){ - System.out.printf("명령) "); - String order=scan.nextLine().trim(); - if(order.equals("등록")){ - System.out.printf("명언 : "); - String quote = scan.nextLine().trim(); - System.out.printf("작가 : "); - String author = scan.nextLine().trim(); - - QL4.add(new QuoteLv4(quote,author)); - actCnt++; - - System.out.println(actCnt+"번 명령이 등록되었습니다."); - } - else if(order.equals("종료")){ - return; - } - - } - - } -} diff --git a/src/main/java/org/example/Lv5.java b/src/main/java/org/example/Lv5.java deleted file mode 100644 index 3f993bd..0000000 --- a/src/main/java/org/example/Lv5.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.example; - -import java.util.*; - -class QuoteLv5{ - int id; // Lv4의 actCnt처럼 몇번 째로 등록이 아닌, 명언별로 번호를 달아주기위해 추가 - String quote; - String author; - - //생성자 -> 생성자를 사용하지 않으면 객체 생성 후 별도로 필드를 초기화해야 하므로 번거로움 - QuoteLv5(int id, String quote, String name){ - this.id=id; - this.quote=quote; - this.author=author; - } - - //List나 다른 컬렉션에서 객체를 출력하려고 할 때, 각 객체의 toString() 메서드가 자동으로 호출되므로 toString()을 오버라이딩 - //오버라이딩 하지 않으면 클래스명@주소값 이 반환됨. - @Override - public String toString(){ - return id+" / "+author+" / "+quote; - } -} - -public class Lv5 { - Scanner scan = new Scanner(System.in); - List QL5 = new ArrayList<>(); - - public void doLv5(){ - int id = 1; - System.out.println("== 명언 앱 =="); - - while(true){ - System.out.printf("명령) "); - String order=scan.nextLine().trim(); - if(order.equals("등록")){ - System.out.printf("명언 : "); - String quote = scan.nextLine().trim(); - System.out.printf("작가 : "); - String author = scan.nextLine().trim(); - - QL5.add(new QuoteLv5(id,quote,author)); - System.out.println(id++ +"번 명언이 등록되었습니다."); - } - else if(order.equals("목록")){ - System.out.println("번호 / 작가 / 명언"); - System.out.println("----------------------"); - if(QL5.isEmpty()) { - System.out.println("등록된 명언이 없습니다."); - } - else{ - for(int i=QL5.size()-1;i>=0;i--){ - System.out.println(QL5.get(i)); - } - } - } - else if(order.equals("종료")){ - return; - } - else{ - System.out.println("유효하지 않은 명령어입니다."); - } - } - - } - -} diff --git a/src/main/java/org/example/Lv6.java b/src/main/java/org/example/Lv6.java deleted file mode 100644 index 5ebbb61..0000000 --- a/src/main/java/org/example/Lv6.java +++ /dev/null @@ -1,82 +0,0 @@ -package org.example; - -import java.util.*; - -class QuoteLv6{ - int id; // Lv4의 actCnt처럼 몇번 째로 등록이 아닌, 명언별로 번호를 달아주기위해 추가 - String quote; - String author; - - //생성자 -> 생성자를 사용하지 않으면 객체 생성 후 별도로 필드를 초기화해야 하므로 번거로움 - QuoteLv6(int id, String quote, String name){ - this.id=id; - this.quote=quote; - this.author=author; - } - - //List나 다른 컬렉션에서 객체를 출력하려고 할 때, 각 객체의 toString() 메서드가 자동으로 호출되므로 toString()을 오버라이딩 - //오버라이딩 하지 않으면 클래스명@주소값 이 반환됨. - @Override - public String toString(){ - return id+" / "+author+" / "+quote; - } -} - -public class Lv6 { - Scanner scan = new Scanner(System.in); - List QL6 = new ArrayList<>(); - - public void doLv6(){ - int id = 1; - System.out.println("== 명언 앱 =="); - - while(true){ - System.out.printf("명령) "); - String order=scan.nextLine().trim(); - if(order.equals("등록")){ - System.out.printf("명언 : "); - String quote = scan.nextLine().trim(); - System.out.printf("작가 : "); - String author = scan.nextLine().trim(); - - QL6.add(new QuoteLv6(id,quote,author)); - System.out.println(id++ +"번 명언이 등록되었습니다."); - } - else if(order.equals("목록")){ - System.out.println("번호 / 작가 / 명언"); - System.out.println("----------------------"); - if(QL6.isEmpty()) { - System.out.println("등록된 명언이 없습니다."); - } - else{ - for(int i=QL6.size()-1;i>=0;i--){ - System.out.println(QL6.get(i)); - } - } - } - else if(order.equals("종료")){ - return; - } - else if(order.startsWith("삭제?id=")){ //startsWith 메서드를 사용. - int deleteId = Integer.parseInt(order.split("=")[1]); - boolean ifFound=false; - for(int i=QL6.size()-1;i>=0;i--){ - if(QL6.get(i).id==deleteId){ - QL6.remove(i); - System.out.println(deleteId + "번 명언이 삭제되었습니다."); - ifFound=true; - break; - } - } - if(!ifFound){ - System.out.println(deleteId + "번 명언은 목록에 없습니다."); - } - } - else{ - System.out.println("유효하지 않은 명령어입니다."); - } - } - - } - -} diff --git a/src/main/java/org/example/Lv7.java b/src/main/java/org/example/Lv7.java deleted file mode 100644 index 85f9f5f..0000000 --- a/src/main/java/org/example/Lv7.java +++ /dev/null @@ -1,82 +0,0 @@ -package org.example; - -import java.util.*; - -class QuoteLv7{ - int id; // Lv4의 actCnt처럼 몇번 째로 등록이 아닌, 명언별로 번호를 달아주기위해 추가 - String quote; - String author; - - //생성자 -> 생성자를 사용하지 않으면 객체 생성 후 별도로 필드를 초기화해야 하므로 번거로움 - QuoteLv7(int id, String quote, String name){ - this.id=id; - this.quote=quote; - this.author=author; - } - - //List나 다른 컬렉션에서 객체를 출력하려고 할 때, 각 객체의 toString() 메서드가 자동으로 호출되므로 toString()을 오버라이딩 - //오버라이딩 하지 않으면 클래스명@주소값 이 반환됨. - @Override - public String toString(){ - return id+" / "+author+" / "+quote; - } -} - -public class Lv7 { - Scanner scan = new Scanner(System.in); - List QLstLv7 = new ArrayList<>(); - - public void doLv7(){ - int id = 1; - System.out.println("== 명언 앱 =="); - - while(true){ - System.out.printf("명령) "); - String order=scan.nextLine().trim(); - if(order.equals("등록")){ - System.out.printf("명언 : "); - String quote = scan.nextLine().trim(); - System.out.printf("작가 : "); - String author = scan.nextLine().trim(); - - QLstLv7.add(new QuoteLv7(id,quote,author)); - System.out.println(id++ +"번 명언이 등록되었습니다."); - } - else if(order.equals("목록")){ - System.out.println("번호 / 작가 / 명언"); - System.out.println("----------------------"); - if(QLstLv7.isEmpty()) { - System.out.println("등록된 명언이 없습니다."); - } - else{ - for(int i=QLstLv7.size()-1;i>=0;i--){ - System.out.println(QLstLv7.get(i)); - } - } - } - else if(order.equals("종료")){ - return; - } - else if(order.startsWith("삭제?id=")){ //startsWith 메서드를 사용. - try{ - int deleteId = Integer.parseInt(order.split("=")[1]); //이부분 동작에 대해 예외처리 함. - boolean isRemoved = QLstLv7.removeIf(QLstEle -> QLstEle.id == deleteId); - - if (isRemoved) { - System.out.println(deleteId + "번 명언이 삭제되었습니다."); - } else { - System.out.println(deleteId + "번 명언은 존재하지 않습니다."); - } - } - catch(Exception e){ - System.out.println("명령어 형식을 재확인 하십시오. 형식 : 삭제?id=숫자 "); - } - } - else{ - System.out.println("유효하지 않은 명령어입니다."); - } - } - - } - -} diff --git a/src/main/java/org/example/Lv8.java b/src/main/java/org/example/Lv8.java deleted file mode 100644 index 2d64894..0000000 --- a/src/main/java/org/example/Lv8.java +++ /dev/null @@ -1,114 +0,0 @@ -package org.example; - -import java.util.*; - -class QuoteLv8{ - int id; // Lv4의 actCnt처럼 몇번 째로 등록이 아닌, 명언별로 번호를 달아주기위해 추가 - String quote; - String author; - - //생성자 -> 생성자를 사용하지 않으면 객체 생성 후 별도로 필드를 초기화해야 하므로 번거로움 - QuoteLv8(int id, String quote, String author){ - this.id=id; - this.quote=quote; - this.author=author; - } - - //List나 다른 컬렉션에서 객체를 출력하려고 할 때, 각 객체의 toString() 메서드가 자동으로 호출되므로 toString()을 오버라이딩 - //오버라이딩 하지 않으면 클래스명@주소값 이 반환됨. - @Override - public String toString(){ - return id+" / "+author+" / "+quote; - } -} - -public class Lv8 { - Scanner scan = new Scanner(System.in); - List QLstLv8 = new ArrayList<>(); - - public void doLv8(){ - int id = 1; - System.out.println("== 명언 앱 =="); - - while(true){ - System.out.printf("명령) "); - String order=scan.nextLine().trim(); - if(order.equals("등록")){ - System.out.printf("명언 : "); - String quote = scan.nextLine().trim(); - System.out.printf("작가 : "); - String author = scan.nextLine().trim(); - - QLstLv8.add(new QuoteLv8(id,quote,author)); - System.out.println(id++ +"번 명언이 등록되었습니다."); - } - else if(order.equals("목록")){ - System.out.println("번호 / 작가 / 명언"); - System.out.println("----------------------"); - if(QLstLv8.isEmpty()) { - System.out.println("등록된 명언이 없습니다."); - } - else{ - for(int i=QLstLv8.size()-1;i>=0;i--){ - System.out.println(QLstLv8.get(i)); - } - } - } - else if(order.equals("종료")){ - return; - } - else if(order.startsWith("삭제?id=")){ //startsWith 메서드를 사용. - try{ - int deleteId = Integer.parseInt(order.split("=")[1]); //이부분 동작에 대해 예외처리 함. - boolean isRemoved = QLstLv8.removeIf(QLstEle -> QLstEle.id == deleteId); - - if (isRemoved) { - System.out.println(deleteId + "번 명언이 삭제되었습니다."); - } else { - System.out.println(deleteId + "번 명언은 존재하지 않습니다."); - } - } - catch(Exception e){ - System.out.println("명령어 형식을 재확인 하십시오. 형식 : 삭제?id=숫자 "); - } - } - else if(order.startsWith("수정?id=")){ //startsWith 메서드를 사용. - try{ - int modifyId = Integer.parseInt(order.split("=")[1]); //이부분 동작에 대해 예외처리 함. - boolean isExist = false; //존재여부 확인용 - - for(int i=0 ; i quotes = new ArrayList<>(); + private int nextId = 1; + + public void run() { + System.out.println("== 명언 앱 =="); + + while (true) { + System.out.printf("명령) "); + String command = scan.nextLine().trim(); + + switch (command.split("\\?")[0]) { + case "등록": + quoteController.registerQuote(); + break; + case "목록": + quoteController.listQuotes(); + break; + case "종료": + System.out.println("프로그램을 종료합니다."); + return; + case "삭제": + quoteController.deleteQuote(command); + break; + case "수정": + quoteController.modifyQuote(command); + break; + default: + System.out.println("유효하지 않은 명령어입니다."); + } + } + } +} diff --git a/src/main/java/org/example/Lv9.java b/src/main/java/org/example/QuoteController.java similarity index 53% rename from src/main/java/org/example/Lv9.java rename to src/main/java/org/example/QuoteController.java index 80023ee..9f67b11 100644 --- a/src/main/java/org/example/Lv9.java +++ b/src/main/java/org/example/QuoteController.java @@ -1,70 +1,31 @@ package org.example; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; -//메인 기능 -class QuoteLv9 { - int id; - String quote; - String author; - - QuoteLv9(int id, String quote, String author) { - this.id = id; - this.quote = quote; - this.author = author; - } - - @Override - public String toString() { - return id + " / " + author + " / " + quote; - } -} - -public class Lv9 { - private final Scanner scan = new Scanner(System.in); - private final List quotes = new ArrayList<>(); +public class QuoteController { + private final List quotes = new ArrayList<>(); + private final Scanner scan; private int nextId = 1; - public void doLv9() { - System.out.println("== 명언 앱 =="); - - while (true) { - System.out.printf("명령) "); - String command = scan.nextLine().trim(); - - switch (command.split("\\?")[0]) { - case "등록": - registerQuote(); - break; - case "목록": - listQuotes(); - break; - case "종료": - System.out.println("프로그램을 종료합니다."); - return; - case "삭제": - deleteQuote(command); - break; - case "수정": - modifyQuote(command); - break; - default: - System.out.println("유효하지 않은 명령어입니다."); - } - } + public QuoteController(Scanner scan) { + this.scan = scan; } - private void registerQuote() { + // 1. 명언 등록 + public void registerQuote() { System.out.printf("명언 : "); String quoteText = scan.nextLine().trim(); System.out.printf("작가 : "); String author = scan.nextLine().trim(); - quotes.add(new QuoteLv9(nextId, quoteText, author)); + quotes.add(new Quote(nextId, quoteText, author)); System.out.println(nextId++ + "번 명언이 등록되었습니다."); } - private void listQuotes() { + // 2. 명언 목록 + public void listQuotes() { System.out.println("번호 / 작가 / 명언"); System.out.println("----------------------"); @@ -77,9 +38,10 @@ private void listQuotes() { } } - private void deleteQuote(String command) { + // 3. 명언 삭제 + public void deleteQuote(String command) { try { - int idToDelete = getIdFromCommand(command); + int idToDelete = parseIdFromCommand(command); boolean removed = quotes.removeIf(quote -> quote.id == idToDelete); if (removed) { @@ -92,10 +54,11 @@ private void deleteQuote(String command) { } } - private void modifyQuote(String command) { + // 4. 명언 수정 + public void modifyQuote(String command) { try { - int idToModify = getIdFromCommand(command); - QuoteLv9 targetQuote = findQuoteById(idToModify); + int idToModify = parseIdFromCommand(command); + Quote targetQuote = findQuoteById(idToModify); if (targetQuote != null) { System.out.println("명언(기존) : " + targetQuote.quote); @@ -115,12 +78,14 @@ private void modifyQuote(String command) { } } - private int getIdFromCommand(String command) { + // 명령어에서 ID 추출 + private int parseIdFromCommand(String command) { return Integer.parseInt(command.split("=")[1].trim()); } - private QuoteLv9 findQuoteById(int id) { - for (QuoteLv9 quote : quotes) { + // ID로 명언 찾기 + private Quote findQuoteById(int id) { + for (Quote quote : quotes) { if (quote.id == id) { return quote; } From 6fe15a20ffc58a8e73e202ae62f96c3d272e713e Mon Sep 17 00:00:00 2001 From: joseph Date: Thu, 2 Jan 2025 13:06:01 +0900 Subject: [PATCH 12/13] refac: Lv8 - file divided & MVC(#12) --- pom.xml | 17 +++- src/main/java/org/example/App.java | 46 +++++++++ src/main/java/org/example/Main.java | 2 +- src/main/java/org/example/Quote.java | 18 ---- src/main/java/org/example/QuoteApp.java | 41 -------- .../java/org/example/QuoteController.java | 95 ------------------- src/main/java/org/example/Test.java | 4 + .../example/wiseSaying/SystemController.java | 9 ++ .../org/example/wiseSaying/WiseSaying.java | 35 +++++++ .../wiseSaying/WiseSayingController.java | 77 +++++++++++++++ .../wiseSaying/WiseSayingRepository.java | 38 ++++++++ .../example/wiseSaying/WiseSayingService.java | 41 ++++++++ 12 files changed, 266 insertions(+), 157 deletions(-) create mode 100644 src/main/java/org/example/App.java delete mode 100644 src/main/java/org/example/Quote.java delete mode 100644 src/main/java/org/example/QuoteApp.java delete mode 100644 src/main/java/org/example/QuoteController.java create mode 100644 src/main/java/org/example/Test.java create mode 100644 src/main/java/org/example/wiseSaying/SystemController.java create mode 100644 src/main/java/org/example/wiseSaying/WiseSaying.java create mode 100644 src/main/java/org/example/wiseSaying/WiseSayingController.java create mode 100644 src/main/java/org/example/wiseSaying/WiseSayingRepository.java create mode 100644 src/main/java/org/example/wiseSaying/WiseSayingService.java diff --git a/pom.xml b/pom.xml index 688ef93..30b0742 100644 --- a/pom.xml +++ b/pom.xml @@ -10,8 +10,8 @@ - 1.8 - 1.8 + 23 + 23 @@ -22,4 +22,17 @@ + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 23 + 23 + + + + diff --git a/src/main/java/org/example/App.java b/src/main/java/org/example/App.java new file mode 100644 index 0000000..a8adc13 --- /dev/null +++ b/src/main/java/org/example/App.java @@ -0,0 +1,46 @@ +package org.example; +import org.example.wiseSaying.WiseSayingController; +import org.example.wiseSaying.SystemController; + +import java.util.Scanner; + +public class App { + + private final WiseSayingController wiseSayingController; + private final SystemController systemController; + private final Scanner scanner; + + public App() { + scanner = new Scanner(System.in); + wiseSayingController = new WiseSayingController(scanner); + systemController = new SystemController(); + } + + public void run() { + + wiseSayingController.makeTestData(); + + System.out.println("== 명언 앱 =="); + while (true) { + System.out.print("명령) "); + String command = scanner.nextLine(); + + if (command.equals("종료")) { + systemController.exit(); + break; + } else if (command.equals("등록")) { + wiseSayingController.writeWiseSaying(); + } else if (command.equals("목록")) { + wiseSayingController.printWiseSayingList(); + } else if (command.startsWith("삭제?id=")) { + String strId = command.substring(6); + int id = Integer.parseInt(strId); + wiseSayingController.deleteWiseSaying(id); + } else if (command.startsWith("수정?id=")) { + String strId = command.substring(6); + int id = Integer.parseInt(strId); + wiseSayingController.updateWiseSaying(id); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 95b1d26..023bd19 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -5,7 +5,7 @@ public class Main { public static void main(String[] args) { - QuoteApp app = new QuoteApp(); + App app = new App(); app.run(); } } \ No newline at end of file diff --git a/src/main/java/org/example/Quote.java b/src/main/java/org/example/Quote.java deleted file mode 100644 index 9ea8fc2..0000000 --- a/src/main/java/org/example/Quote.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.example; - -class Quote { - int id; - String quote; - String author; - - Quote(int id, String quote, String author) { - this.id = id; - this.quote = quote; - this.author = author; - } - - @Override - public String toString() { - return id + " / " + author + " / " + quote; - } -} \ No newline at end of file diff --git a/src/main/java/org/example/QuoteApp.java b/src/main/java/org/example/QuoteApp.java deleted file mode 100644 index 58c89d6..0000000 --- a/src/main/java/org/example/QuoteApp.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.example; - -import java.util.*; - - - -public class QuoteApp { - private final Scanner scan = new Scanner(System.in); - QuoteController quoteController = new QuoteController(scan); - private final List quotes = new ArrayList<>(); - private int nextId = 1; - - public void run() { - System.out.println("== 명언 앱 =="); - - while (true) { - System.out.printf("명령) "); - String command = scan.nextLine().trim(); - - switch (command.split("\\?")[0]) { - case "등록": - quoteController.registerQuote(); - break; - case "목록": - quoteController.listQuotes(); - break; - case "종료": - System.out.println("프로그램을 종료합니다."); - return; - case "삭제": - quoteController.deleteQuote(command); - break; - case "수정": - quoteController.modifyQuote(command); - break; - default: - System.out.println("유효하지 않은 명령어입니다."); - } - } - } -} diff --git a/src/main/java/org/example/QuoteController.java b/src/main/java/org/example/QuoteController.java deleted file mode 100644 index 9f67b11..0000000 --- a/src/main/java/org/example/QuoteController.java +++ /dev/null @@ -1,95 +0,0 @@ -package org.example; - -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -public class QuoteController { - private final List quotes = new ArrayList<>(); - private final Scanner scan; - private int nextId = 1; - - public QuoteController(Scanner scan) { - this.scan = scan; - } - - // 1. 명언 등록 - public void registerQuote() { - System.out.printf("명언 : "); - String quoteText = scan.nextLine().trim(); - System.out.printf("작가 : "); - String author = scan.nextLine().trim(); - - quotes.add(new Quote(nextId, quoteText, author)); - System.out.println(nextId++ + "번 명언이 등록되었습니다."); - } - - // 2. 명언 목록 - public void listQuotes() { - System.out.println("번호 / 작가 / 명언"); - System.out.println("----------------------"); - - if (quotes.isEmpty()) { - System.out.println("등록된 명언이 없습니다."); - } else { - for (int i = quotes.size() - 1; i >= 0; i--) { - System.out.println(quotes.get(i)); - } - } - } - - // 3. 명언 삭제 - public void deleteQuote(String command) { - try { - int idToDelete = parseIdFromCommand(command); - boolean removed = quotes.removeIf(quote -> quote.id == idToDelete); - - if (removed) { - System.out.println(idToDelete + "번 명언이 삭제되었습니다."); - } else { - System.out.println(idToDelete + "번 명언은 존재하지 않습니다."); - } - } catch (NumberFormatException e) { - System.out.println("명령어 형식을 재확인 하십시오. 형식 : 삭제?id=숫자"); - } - } - - // 4. 명언 수정 - public void modifyQuote(String command) { - try { - int idToModify = parseIdFromCommand(command); - Quote targetQuote = findQuoteById(idToModify); - - if (targetQuote != null) { - System.out.println("명언(기존) : " + targetQuote.quote); - System.out.printf("명언 : "); - targetQuote.quote = scan.nextLine().trim(); - - System.out.println("작가(기존) : " + targetQuote.author); - System.out.printf("작가 : "); - targetQuote.author = scan.nextLine().trim(); - - System.out.println(idToModify + "번 명언이 수정되었습니다."); - } else { - System.out.println(idToModify + "번 명언은 존재하지 않습니다."); - } - } catch (NumberFormatException e) { - System.out.println("명령어 형식을 재확인 하십시오. 형식 : 수정?id=숫자"); - } - } - - // 명령어에서 ID 추출 - private int parseIdFromCommand(String command) { - return Integer.parseInt(command.split("=")[1].trim()); - } - - // ID로 명언 찾기 - private Quote findQuoteById(int id) { - for (Quote quote : quotes) { - if (quote.id == id) { - return quote; - } - } - return null; - } -} \ No newline at end of file diff --git a/src/main/java/org/example/Test.java b/src/main/java/org/example/Test.java new file mode 100644 index 0000000..4eec911 --- /dev/null +++ b/src/main/java/org/example/Test.java @@ -0,0 +1,4 @@ +package org.example; + +public class Test { +} diff --git a/src/main/java/org/example/wiseSaying/SystemController.java b/src/main/java/org/example/wiseSaying/SystemController.java new file mode 100644 index 0000000..6f08758 --- /dev/null +++ b/src/main/java/org/example/wiseSaying/SystemController.java @@ -0,0 +1,9 @@ +package org.example.wiseSaying; + + +public class SystemController { + + public void exit() { + System.out.println("명언 앱을 종료합니다."); + } +} \ No newline at end of file diff --git a/src/main/java/org/example/wiseSaying/WiseSaying.java b/src/main/java/org/example/wiseSaying/WiseSaying.java new file mode 100644 index 0000000..d77e901 --- /dev/null +++ b/src/main/java/org/example/wiseSaying/WiseSaying.java @@ -0,0 +1,35 @@ +package org.example.wiseSaying; + +public class WiseSaying { + private int id; + private String content; + private String author; + + WiseSaying(int id, String content, String author) { + this.id = id; + this.content = content; + this.author = author; + } + + public void setContent(String content) { + this.content = content; + } + public void setId(int id) { + this.id = id; + } + public void setAuthor(String author) { + this.author = author; + } + public int getId() { + return id; + } + + public String getContent() { + return content; + } + + public String getAuthor() { + return author; + } + +} \ No newline at end of file diff --git a/src/main/java/org/example/wiseSaying/WiseSayingController.java b/src/main/java/org/example/wiseSaying/WiseSayingController.java new file mode 100644 index 0000000..25fdd58 --- /dev/null +++ b/src/main/java/org/example/wiseSaying/WiseSayingController.java @@ -0,0 +1,77 @@ +package org.example.wiseSaying; + + +import java.util.ArrayList; +import java.util.Scanner; + +public class WiseSayingController { + + private final WiseSayingService wiseSayingService; + private final Scanner scanner; + + public WiseSayingController(Scanner scanner) { + this.scanner = scanner; + this.wiseSayingService = new WiseSayingService(new WiseSayingRepository()); + } + + public void updateWiseSaying(int targetId) { + + WiseSaying wiseSaying = wiseSayingService.findById(targetId); + + if (wiseSaying == null) { + System.out.println("%d번 명언은 존재하지 않습니다.".formatted(targetId)); + return; + } + + System.out.println("명언(기존) : %s".formatted(wiseSaying.getContent())); + System.out.print("명언 : "); + String newContent = scanner.nextLine(); + System.out.println("명언(작가) : %s".formatted(wiseSaying.getAuthor())); + System.out.print("작가 : "); + String newAuthor = scanner.nextLine(); + + wiseSayingService.update(wiseSaying, newContent, newAuthor); + System.out.println("%d번 명언이 수정되었습니다.".formatted(targetId)); + + } + + public void deleteWiseSaying(int targetId) { + + WiseSaying wiseSaying = wiseSayingService.findById(targetId); + + if (wiseSaying == null) { + System.out.println("%d번 명언은 존재하지 않습니다.".formatted(targetId)); + return; + } + + wiseSayingService.remove(wiseSaying); + System.out.println("%d번 명언이 삭제되었습니다.".formatted(targetId)); + } + + public void printWiseSayingList() { + System.out.println("번호 / 작가 / 명언"); + System.out.println("----------------------"); + + ArrayList wiseSayings = wiseSayingService.findAll(); + + for (WiseSaying wiseSaying : wiseSayings.reversed()) { + System.out.println("%d / %s / %s".formatted(wiseSaying.getId(), wiseSaying.getAuthor(), wiseSaying.getContent())); + } + } + + public void writeWiseSaying() { + System.out.print("명언 : "); + String content = scanner.nextLine(); + + System.out.print("작가 : "); + String author = scanner.nextLine(); + + WiseSaying wiseSaying = wiseSayingService.add(content, author); + System.out.println("%d번 명언이 등록되었습니다.".formatted(wiseSaying.getId())); + } + + public void makeTestData() { + wiseSayingService.add("꿈을 지녀라. 그러면 어려운 현실을 이길 수 있다.", "월트 디즈니"); + wiseSayingService.add("현재를 사랑하라", "작자 미상"); + } +} \ No newline at end of file diff --git a/src/main/java/org/example/wiseSaying/WiseSayingRepository.java b/src/main/java/org/example/wiseSaying/WiseSayingRepository.java new file mode 100644 index 0000000..cc26db7 --- /dev/null +++ b/src/main/java/org/example/wiseSaying/WiseSayingRepository.java @@ -0,0 +1,38 @@ +package org.example.wiseSaying; + +import java.util.ArrayList; + +public class WiseSayingRepository { + private final ArrayList wiseSayingList = new ArrayList<>(); + private int lastId = 0; + + public WiseSaying findById(int targetId) { + for (WiseSaying wiseSaying : wiseSayingList) { + if (wiseSaying.getId() == targetId) { + return wiseSaying; + } + } + + return null; + } + + public WiseSaying add(String content, String author) { + int id = ++lastId; + WiseSaying wiseSaying = new WiseSaying(id, content, author); + wiseSayingList.add(wiseSaying); + return wiseSaying; + } + + public ArrayList findAll() { + return wiseSayingList; + } + + public void remove(WiseSaying wiseSaying) { + wiseSayingList.remove(wiseSaying); + } + + public void update(WiseSaying wiseSaying) { + + // 저장. -> ArrayList 사용 -> 메모리 저장 + } +} \ No newline at end of file diff --git a/src/main/java/org/example/wiseSaying/WiseSayingService.java b/src/main/java/org/example/wiseSaying/WiseSayingService.java new file mode 100644 index 0000000..1b49ad1 --- /dev/null +++ b/src/main/java/org/example/wiseSaying/WiseSayingService.java @@ -0,0 +1,41 @@ +package org.example.wiseSaying; + +import java.util.ArrayList; + +public class WiseSayingService { + + private final WiseSayingRepository wiseSayingRepository; + + public WiseSayingService(WiseSayingRepository wiseSayingRepository) { + this.wiseSayingRepository = wiseSayingRepository; + } + + public WiseSaying findById(int targetId) { + return wiseSayingRepository.findById(targetId); + } + + public void update(WiseSaying wiseSaying, String newContent, String newAuthor) { + + // 비즈니스 로직(객체를 조립하거나 조작하는 것 - 비즈니스 로직) + wiseSaying.setContent(newContent); + wiseSaying.setAuthor(newAuthor); + + // 객체를 저장 -> 데이터 저장 로직 + + // 비즈니스 로직 - 서비스 + // 데이터 입출력 - 레포지터리 + wiseSayingRepository.update(wiseSaying); + } + + public WiseSaying add(String content, String author) { + return wiseSayingRepository.add(content, author); + } + + public ArrayList findAll() { + return wiseSayingRepository.findAll(); + } + + public void remove(WiseSaying wiseSaying) { + wiseSayingRepository.remove(wiseSaying); + } +} \ No newline at end of file From 4e656e3ccb57b1dae2b63c08eb4b937cc7644129 Mon Sep 17 00:00:00 2001 From: joseph Date: Thu, 2 Jan 2025 14:33:12 +0900 Subject: [PATCH 13/13] feat: Lv9 - WiseSayingFileRepository added / preparing for LV9 (#13) --- .../org/example/wiseSaying/WiseSaying.java | 5 ++- .../wiseSaying/WiseSayingController.java | 18 ++++----- .../wiseSaying/WiseSayingFileRepository.java | 32 +++++++++++++++ .../wiseSaying/WiseSayingMemRepository.java | 37 ++++++++++++++++++ .../wiseSaying/WiseSayingRepository.java | 39 +++---------------- .../example/wiseSaying/WiseSayingService.java | 14 ++----- 6 files changed, 91 insertions(+), 54 deletions(-) create mode 100644 src/main/java/org/example/wiseSaying/WiseSayingFileRepository.java create mode 100644 src/main/java/org/example/wiseSaying/WiseSayingMemRepository.java diff --git a/src/main/java/org/example/wiseSaying/WiseSaying.java b/src/main/java/org/example/wiseSaying/WiseSaying.java index d77e901..aa0f348 100644 --- a/src/main/java/org/example/wiseSaying/WiseSaying.java +++ b/src/main/java/org/example/wiseSaying/WiseSaying.java @@ -5,7 +5,7 @@ public class WiseSaying { private String content; private String author; - WiseSaying(int id, String content, String author) { + public WiseSaying(int id, String content, String author) { this.id = id; this.content = content; this.author = author; @@ -14,12 +14,15 @@ public class WiseSaying { public void setContent(String content) { this.content = content; } + public void setId(int id) { this.id = id; } + public void setAuthor(String author) { this.author = author; } + public int getId() { return id; } diff --git a/src/main/java/org/example/wiseSaying/WiseSayingController.java b/src/main/java/org/example/wiseSaying/WiseSayingController.java index 25fdd58..9a73f99 100644 --- a/src/main/java/org/example/wiseSaying/WiseSayingController.java +++ b/src/main/java/org/example/wiseSaying/WiseSayingController.java @@ -1,6 +1,4 @@ package org.example.wiseSaying; - - import java.util.ArrayList; import java.util.Scanner; @@ -11,12 +9,12 @@ public class WiseSayingController { public WiseSayingController(Scanner scanner) { this.scanner = scanner; - this.wiseSayingService = new WiseSayingService(new WiseSayingRepository()); + this.wiseSayingService = new WiseSayingService(new WiseSayingMemRepository()); } public void updateWiseSaying(int targetId) { - WiseSaying wiseSaying = wiseSayingService.findById(targetId); + WiseSaying wiseSaying = wiseSayingService.getItem(targetId); if (wiseSaying == null) { System.out.println("%d번 명언은 존재하지 않습니다.".formatted(targetId)); @@ -30,14 +28,14 @@ public void updateWiseSaying(int targetId) { System.out.print("작가 : "); String newAuthor = scanner.nextLine(); - wiseSayingService.update(wiseSaying, newContent, newAuthor); + wiseSayingService.modify(wiseSaying, newContent, newAuthor); System.out.println("%d번 명언이 수정되었습니다.".formatted(targetId)); } public void deleteWiseSaying(int targetId) { - WiseSaying wiseSaying = wiseSayingService.findById(targetId); + WiseSaying wiseSaying = wiseSayingService.getItem(targetId); if (wiseSaying == null) { System.out.println("%d번 명언은 존재하지 않습니다.".formatted(targetId)); @@ -52,7 +50,7 @@ public void printWiseSayingList() { System.out.println("번호 / 작가 / 명언"); System.out.println("----------------------"); - ArrayList wiseSayings = wiseSayingService.findAll(); + ArrayList wiseSayings = wiseSayingService.getItems(); for (WiseSaying wiseSaying : wiseSayings.reversed()) { System.out.println("%d / %s / %s".formatted(wiseSaying.getId(), wiseSaying.getAuthor(), wiseSaying.getContent())); @@ -66,12 +64,12 @@ public void writeWiseSaying() { System.out.print("작가 : "); String author = scanner.nextLine(); - WiseSaying wiseSaying = wiseSayingService.add(content, author); + WiseSaying wiseSaying = wiseSayingService.write(content, author); System.out.println("%d번 명언이 등록되었습니다.".formatted(wiseSaying.getId())); } public void makeTestData() { - wiseSayingService.add("꿈을 지녀라. 그러면 어려운 현실을 이길 수 있다.", "월트 디즈니"); - wiseSayingService.add("현재를 사랑하라", "작자 미상"); + wiseSayingService.write("꿈을 지녀라. 그러면 어려운 현실을 이길 수 있다.", "월트 디즈니"); + wiseSayingService.write("현재를 사랑하라", "작자 미상"); } } \ No newline at end of file diff --git a/src/main/java/org/example/wiseSaying/WiseSayingFileRepository.java b/src/main/java/org/example/wiseSaying/WiseSayingFileRepository.java new file mode 100644 index 0000000..1619031 --- /dev/null +++ b/src/main/java/org/example/wiseSaying/WiseSayingFileRepository.java @@ -0,0 +1,32 @@ +package org.example.wiseSaying; + +import java.util.ArrayList; + +public class WiseSayingFileRepository implements WiseSayingRepository{ + + // 오버라이딩 + public WiseSaying findById(int id) { + return null; + } + + @Override + public WiseSaying add(String content, String author) { + return null; + } + + @Override + public void update(WiseSaying wiseSaying) { + + } + + @Override + public ArrayList findAll() { + return null; + } + + @Override + public void remove(WiseSaying wiseSaying) { + + } + +} \ No newline at end of file diff --git a/src/main/java/org/example/wiseSaying/WiseSayingMemRepository.java b/src/main/java/org/example/wiseSaying/WiseSayingMemRepository.java new file mode 100644 index 0000000..fca7fa8 --- /dev/null +++ b/src/main/java/org/example/wiseSaying/WiseSayingMemRepository.java @@ -0,0 +1,37 @@ +package org.example.wiseSaying; + +import java.util.ArrayList; + +public class WiseSayingMemRepository implements WiseSayingRepository { + private final ArrayList wiseSayingList = new ArrayList<>(); + private int lastId = 0; + + public WiseSaying findById(int targetId) { + for (WiseSaying wiseSaying : wiseSayingList) { + if (wiseSaying.getId() == targetId) { + return wiseSaying; + } + } + + return null; + } + + public WiseSaying add(String content, String author) { + int id = ++lastId; + WiseSaying wiseSaying = new WiseSaying(id, content, author); + wiseSayingList.add(wiseSaying); + return wiseSaying; + } + + public ArrayList findAll() { + return wiseSayingList; + } + + public void remove(WiseSaying wiseSaying) { + wiseSayingList.remove(wiseSaying); + } + + public void update(WiseSaying wiseSaying) { + // 현재는 메모리에 저장하기 때문에 별도의 업데이트 코드 없음. + } +} \ No newline at end of file diff --git a/src/main/java/org/example/wiseSaying/WiseSayingRepository.java b/src/main/java/org/example/wiseSaying/WiseSayingRepository.java index cc26db7..e218f13 100644 --- a/src/main/java/org/example/wiseSaying/WiseSayingRepository.java +++ b/src/main/java/org/example/wiseSaying/WiseSayingRepository.java @@ -2,37 +2,10 @@ import java.util.ArrayList; -public class WiseSayingRepository { - private final ArrayList wiseSayingList = new ArrayList<>(); - private int lastId = 0; - - public WiseSaying findById(int targetId) { - for (WiseSaying wiseSaying : wiseSayingList) { - if (wiseSaying.getId() == targetId) { - return wiseSaying; - } - } - - return null; - } - - public WiseSaying add(String content, String author) { - int id = ++lastId; - WiseSaying wiseSaying = new WiseSaying(id, content, author); - wiseSayingList.add(wiseSaying); - return wiseSaying; - } - - public ArrayList findAll() { - return wiseSayingList; - } - - public void remove(WiseSaying wiseSaying) { - wiseSayingList.remove(wiseSaying); - } - - public void update(WiseSaying wiseSaying) { - - // 저장. -> ArrayList 사용 -> 메모리 저장 - } +public interface WiseSayingRepository { + WiseSaying findById(int id); + WiseSaying add(String content, String author); + void update(WiseSaying wiseSaying); + ArrayList findAll(); + void remove(WiseSaying wiseSaying); } \ No newline at end of file diff --git a/src/main/java/org/example/wiseSaying/WiseSayingService.java b/src/main/java/org/example/wiseSaying/WiseSayingService.java index 1b49ad1..c5e94d5 100644 --- a/src/main/java/org/example/wiseSaying/WiseSayingService.java +++ b/src/main/java/org/example/wiseSaying/WiseSayingService.java @@ -1,5 +1,4 @@ package org.example.wiseSaying; - import java.util.ArrayList; public class WiseSayingService { @@ -10,28 +9,23 @@ public WiseSayingService(WiseSayingRepository wiseSayingRepository) { this.wiseSayingRepository = wiseSayingRepository; } - public WiseSaying findById(int targetId) { + public WiseSaying getItem(int targetId) { return wiseSayingRepository.findById(targetId); } - public void update(WiseSaying wiseSaying, String newContent, String newAuthor) { + public void modify(WiseSaying wiseSaying, String newContent, String newAuthor) { - // 비즈니스 로직(객체를 조립하거나 조작하는 것 - 비즈니스 로직) wiseSaying.setContent(newContent); wiseSaying.setAuthor(newAuthor); - // 객체를 저장 -> 데이터 저장 로직 - - // 비즈니스 로직 - 서비스 - // 데이터 입출력 - 레포지터리 wiseSayingRepository.update(wiseSaying); } - public WiseSaying add(String content, String author) { + public WiseSaying write(String content, String author) { return wiseSayingRepository.add(content, author); } - public ArrayList findAll() { + public ArrayList getItems() { return wiseSayingRepository.findAll(); }