3/2 TIL

큰모래·2023년 3월 2일
0

명언 앱 프로젝트 (계속 리팩토링 중)


1~2단계 (등록 기능 구현)

import java.util.Scanner;

public class LifeQuotesProject {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("== 명언 앱 ==");
        while(true) {
            System.out.print("명령) ");
            String 입력 = sc.nextLine();
            if (입력.equals("종료")) {
                return;
            }
            if (입력.equals("등록")) {
                System.out.print("명언 : ");
                String 명언 = sc.nextLine();
                System.out.print("작가 : ");
                String 작가 = sc.nextLine();
            }
        }

    }
}

3~4단계 (등록할때 마다 명언번호 증가 기능 추가)

  • 등록할 때마다 count 변수를 1씩 증가시킨다.

import java.util.Scanner;

public class LifeQuotesProject {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("== 명언 앱 ==");
        int count = 0;

        while(true) {
            System.out.print("명령) ");
            String 입력 = sc.nextLine();
            if (입력.equals("종료")) {
                return;
            }
            if (입력.equals("등록")) {
                System.out.print("명언 : ");
                String 명언 = sc.nextLine();
                System.out.print("작가 : ");
                String 작가 = sc.nextLine();
                count++;
                System.out.println(count+"번 명언이 등록되었습니다.");
            }
        }

    }
}

5단계 (목록 조회 기능 추가)

  • HashMap 으로 구현했는데 List 쓰는게 더 깔끔했을듯
  • 목록이 내림차순 조회이므로 최대 count 부터 0까지 map 조회
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class LifeQuotesProject {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Map<Integer, 명언및작가> map = new HashMap<>();
        System.out.println("== 명언 앱 ==");
        int count = 0;

        while(true) {
            System.out.print("명령) ");
            String 입력 = sc.nextLine();
            if (입력.equals("종료")) {
                return;
            }
            if (입력.equals("등록")) {
                System.out.print("명언 : ");
                String 명언 = sc.nextLine();
                System.out.print("작가 : ");
                String 작가 = sc.nextLine();
                count++;
                map.put(count, new 명언및작가(명언, 작가));
                System.out.println(count+"번 명언이 등록되었습니다.");
            }
            if (입력.equals("목록")) {
                for (int i = count; i > 0; i--) {
                    System.out.println(i + " / " + map.get(i).작가 + " / " + map.get(i).명언);

                }
            }
        }

    }

    public static class 명언및작가 {
        String 명언;
        String 작가;

        public 명언및작가(String 명언, String 작가) {
            this.명언 = 명언;
            this.작가 = 작가;
        }
    }
}

6~7단계 (명언 삭제 기능 및 예외 처리 추가)

  • 입력 값이 삭제?id=[인덱스] 이므로 substring으로 숫자만 따로 잘랐다.
  • 이렇게 처리했을때, 쿼리문이 만약 추가된다거나 유지보수적인 측면에서 매우 안좋은듯

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class LifeQuotesProject {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Map<Integer, 명언및작가> map = new HashMap<>();
        System.out.println("== 명언 앱 ==");
        int count = 0;

        while(true) {
            System.out.print("명령) ");
            String 입력 = sc.nextLine();
            if (입력.equals("종료")) {
                return;
            }
            if (입력.equals("등록")) {
                System.out.print("명언 : ");
                String 명언 = sc.nextLine();
                System.out.print("작가 : ");
                String 작가 = sc.nextLine();
                count++;
                map.put(count, new 명언및작가(명언, 작가));
                System.out.println(count+"번 명언이 등록되었습니다.");
            }
            if (입력.equals("목록")) {
                for (int i = count; i > 0; i--) {
                    System.out.println(i + " / " + map.get(i).작가 + " / " + map.get(i).명언);

                }
            }

            if (입력.contains("삭제")) {
                String s = 입력.substring(6);
                int 숫자 = Integer.parseInt(s);
                if (!map.containsKey(숫자)) {
                    System.out.println(숫자 + "번 명언은 존재하지 않습니다.");
                }
                else {
                    map.remove(숫자);
                    System.out.println(숫자 + "번 명언이 삭제되었습니다.");
                }
            }
        }

    }

    public static class 명언및작가 {
        String 명언;
        String 작가;

        public 명언및작가(String 명언, String 작가) {
            this.명언 = 명언;
            this.작가 = 작가;
        }
    }
}

8단계 (명언 수정 기능 추가)

  • HashMapkey 값이 중복이 안되므로 기존 인덱스만 알면 수정이 쉬웠다.

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class LifeQuotesProject {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Map<Integer, 명언및작가> map = new HashMap<>();
        System.out.println("== 명언 앱 ==");
        int count = 0;

        while(true) {
            System.out.print("명령) ");
            String 입력 = sc.nextLine();
            if (입력.equals("종료")) {
                return;
            }
            if (입력.equals("등록")) {
                System.out.print("명언 : ");
                String 명언 = sc.nextLine();
                System.out.print("작가 : ");
                String 작가 = sc.nextLine();
                count++;
                map.put(count, new 명언및작가(명언, 작가));
                System.out.println(count+"번 명언이 등록되었습니다.");
            }
            if (입력.equals("목록")) {
                for (int i = count; i > 0; i--) {
                    if (!map.containsKey(i)) {
                        continue;
                    }
                    System.out.println(i + " / " + map.get(i).작가 + " / " + map.get(i).명언);

                }
            }

            if (입력.contains("삭제")) {
                String s = 입력.substring(6);
                int 숫자 = Integer.parseInt(s);
                if (!map.containsKey(숫자)) {
                    System.out.println(숫자 + "번 명언은 존재하지 않습니다.");
                }
                else {
                    map.remove(숫자);
                    System.out.println(숫자 + "번 명언이 삭제되었습니다.");
                }
            }

            if (입력.contains("수정")) {
                String s = 입력.substring(6);
                int 숫자 = Integer.parseInt(s);
                System.out.println("명언(기존) : " + map.get(숫자).명언);
                System.out.print("명언 : ");
                String 명언 = sc.nextLine();
                System.out.println("작가(기존) : " + map.get(숫자).작가);
                System.out.print("작가 : ");
                String 작가 = sc.nextLine();
                map.put(숫자, new 명언및작가(명언, 작가));
            }
        }

    }

    public static class 명언및작가 {
        String 명언;
        String 작가;

        public 명언및작가(String 명언, String 작가) {
            this.명언 = 명언;
            this.작가 = 작가;
        }
    }
}

10단계 (data.json 빌드) (9단계는 스킵)

  • json-simple 라이브러리를 dependency에 추가해 json 파일을 읽고 썼다.
  • 코드를 상당히 비효율적으로 짜서 상당한 리팩토링이 필요한 것으로 예상됨.

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class LifeQuotesProject {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        Map<Integer, 명언및작가> map = new HashMap<>();
        String path = "/Users/bigsand/IdeaProjects/LikeLion/src/main/java/data.json";
        JSONArray jsonArray = new JSONArray();
        JSONObject json;
        FileWriter fw = new FileWriter(path);
        System.out.println("== 명언 앱 ==");
        int count = 0;
        int count2 = 0;

        while(true) {
            System.out.print("명령) ");
            String 입력 = sc.nextLine();
            if (입력.equals("종료")) {
                return;
            }
            if (입력.equals("등록")) {
                System.out.print("명언 : ");
                String 명언 = sc.nextLine();
                System.out.print("작가 : ");
                String 작가 = sc.nextLine();
                count++;
                map.put(count, new 명언및작가(count,명언, 작가));
                json = new JSONObject();
                json.put("id", count);
                json.put("content", 명언);
                json.put("author", 작가);
                jsonArray.add(json);
                System.out.println(count+"번 명언이 등록되었습니다.");
            }
            if (입력.equals("목록")) {
                for (int i = count; i > 0; i--) {
                    if (!map.containsKey(i)) {
                        continue;
                    }
                    System.out.println(i + " / " + map.get(i).작가 + " / " + map.get(i).명언);

                }
            }

            if (입력.contains("삭제")) {
                String s = 입력.substring(6);
                int 숫자 = Integer.parseInt(s);
                if (!map.containsKey(숫자)) {
                    System.out.println(숫자 + "번 명언은 존재하지 않습니다.");
                }
                else {
                    map.remove(숫자);
                    jsonArray.remove(숫자 - 1);
                    System.out.println(숫자 + "번 명언이 삭제되었습니다.");
                    count2++;
                }
            }

            if (입력.contains("수정")) {
                String s = 입력.substring(6);
                int 숫자 = Integer.parseInt(s);
                System.out.println("명언(기존) : " + map.get(숫자).명언);
                System.out.print("명언 : ");
                String 명언 = sc.nextLine();
                System.out.println("작가(기존) : " + map.get(숫자).작가);
                System.out.print("작가 : ");
                String 작가 = sc.nextLine();
                map.put(숫자, new 명언및작가(숫자,명언, 작가));
                jsonArray.remove(숫자-(1+count2));
                JSONObject newJson = new JSONObject();
                newJson.put("id", 숫자);
                newJson.put("content", 명언);
                newJson.put("author", 작가);
                jsonArray.add(newJson);
            }

            if (입력.equals("빌드")) {
                System.out.println("data.json 파일의 내용이 갱신되었습니다.");
                fw.write(jsonArray.toJSONString());
                fw.close();
            }

        }

    }

    public static class 명언및작가 {
        int 인덱스;
        String 명언;
        String 작가;

        public 명언및작가(int 인덱스, String 명언, String 작가) {
            this.인덱스 = 인덱스;
            this.명언 = 명언;
            this.작가 = 작가;
        }
    }
}
profile
큰모래

0개의 댓글