Skip to content

Commit aeacce5

Browse files
committed
feat: Lv7 구현, refac: Lv6 삭제 로직 예외처리 및 람다식 적용 (#9)
1 parent 6a58b91 commit aeacce5

2 files changed

Lines changed: 84 additions & 2 deletions

File tree

src/main/java/org/example/Lv7.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.example;
2+
3+
import java.util.*;
4+
5+
class QuoteLv7{
6+
int id; // Lv4의 actCnt처럼 몇번 째로 등록이 아닌, 명언별로 번호를 달아주기위해 추가
7+
String quote;
8+
String author;
9+
10+
//생성자 -> 생성자를 사용하지 않으면 객체 생성 후 별도로 필드를 초기화해야 하므로 번거로움
11+
QuoteLv7(int id, String quote, String name){
12+
this.id=id;
13+
this.quote=quote;
14+
this.author=author;
15+
}
16+
17+
//List나 다른 컬렉션에서 객체를 출력하려고 할 때, 각 객체의 toString() 메서드가 자동으로 호출되므로 toString()을 오버라이딩
18+
//오버라이딩 하지 않으면 클래스명@주소값 이 반환됨.
19+
@Override
20+
public String toString(){
21+
return id+" / "+author+" / "+quote;
22+
}
23+
}
24+
25+
public class Lv7 {
26+
Scanner scan = new Scanner(System.in);
27+
List<QuoteLv7> QLstLv7 = new ArrayList<>();
28+
29+
public void doLv7(){
30+
int id = 1;
31+
System.out.println("== 명언 앱 ==");
32+
33+
while(true){
34+
System.out.printf("명령) ");
35+
String order=scan.nextLine().trim();
36+
if(order.equals("등록")){
37+
System.out.printf("명언 : ");
38+
String quote = scan.nextLine().trim();
39+
System.out.printf("작가 : ");
40+
String author = scan.nextLine().trim();
41+
42+
QLstLv7.add(new QuoteLv7(id,quote,author));
43+
System.out.println(id++ +"번 명언이 등록되었습니다.");
44+
}
45+
else if(order.equals("목록")){
46+
System.out.println("번호 / 작가 / 명언");
47+
System.out.println("----------------------");
48+
if(QLstLv7.isEmpty()) {
49+
System.out.println("등록된 명언이 없습니다.");
50+
}
51+
else{
52+
for(int i=QLstLv7.size()-1;i>=0;i--){
53+
System.out.println(QLstLv7.get(i));
54+
}
55+
}
56+
}
57+
else if(order.equals("종료")){
58+
return;
59+
}
60+
else if(order.startsWith("삭제?id=")){ //startsWith 메서드를 사용.
61+
try{
62+
int deleteId = Integer.parseInt(order.split("=")[1]); //이부분 동작에 대해 예외처리 함.
63+
boolean isRemoved = QLstLv7.removeIf(QLstEle -> QLstEle.id == deleteId);
64+
65+
if (isRemoved) {
66+
System.out.println(deleteId + "번 명언이 삭제되었습니다.");
67+
} else {
68+
System.out.println(deleteId + "번 명언은 존재하지 않습니다.");
69+
}
70+
}
71+
catch(Exception e){
72+
System.out.println("명령어 형식을 재확인 하십시오. 형식 : 삭제?id=숫자 ");
73+
}
74+
}
75+
else{
76+
System.out.println("유효하지 않은 명령어입니다.");
77+
}
78+
}
79+
80+
}
81+
82+
}

src/main/java/org/example/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class Main {
66
public static void main(String[] args) {
77

8-
Lv6 lv6 = new Lv6();
9-
lv6.doLv6();
8+
Lv7 lv7 = new Lv7();
9+
lv7.doLv7();
1010
}
1111
}

0 commit comments

Comments
 (0)