STUDY ๐Ÿ“š/Java

[Java] ๋ช…์–ธ ์•ฑ ๋งŒ๋“ค๊ธฐ

daxx0ne 2023. 3. 2. 17:15

[wise_saying_app] GitHub ๐Ÿ‘‡๐Ÿป

 

GitHub - daxx0ne/wise_saying_app

Contribute to daxx0ne/wise_saying_app development by creating an account on GitHub.

github.com


๐Ÿ‘€ ๊ธฐ๋Šฅ์„ค๋ช…

๋“ฑ๋ก: ๋ช…์–ธ๊ณผ ์ž‘๊ฐ€ ์ด๋ฆ„์„ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ชฉ๋ก: ๋“ฑ๋ก๋˜์žˆ๋Š” ๋ช…์–ธ๊ณผ  ์ž‘๊ฐ€ ์ด๋ฆ„์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์‚ญ์ œ: ์›ํ•˜๋Š” ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ๋ช…์–ธ์„ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

์ˆ˜์ •: ์›ํ•˜๋Š” ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ๋ช…์–ธ ๋‚ด์šฉ๊ณผ, ์ž‘๊ฐ€ ์ด๋ฆ„์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ข…๋ฃŒ: ๋ช…์–ธ ์•ฑ์„ ์ข…๋ฃŒํ•œ๋‹ค.


๐Ÿ“Œ ๋งŒ๋“ค๊ธฐ ์ „ ํ•ด์•ผํ•  ๊ฒƒ!

  1. ์ƒˆ๋กœ์šด Git ๋ฆฌํฌ์ง€ํ„ฐ๋ฆฌ ์ƒ์„ฑํ•˜๊ธฐ
  2. ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ
    • gradle → IntelliJ ๋กœ ์„ธํŒ…
    • .gitignore ์ถ”๊ฐ€
    • ๋ฆฌํฌ์ง€ํ„ฐ๋ฆฌ ์—ฐ๊ฒฐ
  3. ๋‹จ๊ณ„ ๋ณ„ ์ปค๋ฐ‹ํ•ด์ฃผ๊ธฐ
    • git init
    • git remote add origin ๊นƒํ—ˆ๋ธŒ์ฃผ์†Œ
    • git add .
    • git commit -m “๋ฉ”์„ธ์ง€”
    • git push origin main

<1๋‹จ๊ณ„>

โœ”๏ธ ๋ชฉํ‘œ: ์ข…๋ฃŒ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ

 

[main.java]

package com.example;
import java.util.*;
public class main { // Applicaiton์ด ์ผํ•˜๋„๋ก ํ•จ
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        new Application(sc).run(); // ๊ธฐ๋ณธ์ ์œผ๋กœ Scanner๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Applicaiton
        sc.close();
    }
}

 

[Application.java]

package com.example;
import java.util.*;
import io.micronaut.runtime.Micronaut;

public class Application {
    public final Scanner sc;
    public Application(Scanner sc){
        this.sc = sc;
    }

    public void run() {
        System.out.println("== ๋ช…์–ธ ์•ฑ ==");
        while(true){
            System.out.print("๋ช…๋ น) ");
            String command = sc.nextLine().trim();
            // trim() : ํ˜น์‹œ ์žˆ์„์ง€ ๋ชจ๋ฅผ ์ขŒ์šฐ๊ณต๋ฐฑ์ œ๊ฑฐ๋œ ๋ฒ„์ „์œผ๋กœ ์ฃผ์„ธ์š”!
            if(command.equals("์ข…๋ฃŒ")) { // ์ข…๋ฃŒ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
                break;
            }

        }
    }
}

<2๋‹จ๊ณ„>

โœ”๏ธ ๋ชฉํ‘œ: ๋“ฑ๋ก ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ

 

[Application.java]

package com.example;
import java.util.*;
import io.micronaut.runtime.Micronaut;

public class Application {
    public final Scanner sc;
    public Application(Scanner sc){
        this.sc = sc;
    }

    public void run() {
        System.out.println("== ๋ช…์–ธ ์•ฑ ==");
        while(true){
            System.out.print("๋ช…๋ น) ");
            String command = sc.nextLine().trim();
            // trim() : ํ˜น์‹œ ์žˆ์„์ง€ ๋ชจ๋ฅผ ์ขŒ์šฐ๊ณต๋ฐฑ ์ œ๊ฑฐ๋œ ๋ฒ„์ „์œผ๋กœ ์ฃผ์„ธ์š”!
            if(command.equals("์ข…๋ฃŒ")) { // ์ข…๋ฃŒ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
                break;
            }
            else if (command.equals("๋“ฑ๋ก")){
                System.out.print("๋ช…์–ธ : "); // #2 ๋ช…์–ธ๊ณผ ์ž‘๊ฐ€ ์ด๋ฆ„์„ ์ž…๋ ฅ๋ฐ›๊ธฐ
                String content = sc.nextLine().trim();
                System.out.print("์ž‘๊ฐ€ : ");
                String authorName = sc.nextLine().trim();
            }

        }
    }
}

<3๋‹จ๊ณ„>

โœ”๏ธ ๋ชฉํ‘œ: ๋“ฑ๋ก ์‹œ ์ƒ์„ฑ๋œ ๋ช…์–ธ ๋ฒˆํ˜ธ๋ฅผ ๋…ธ์ถœ์‹œํ‚ค๊ธฐ

 

[Application.java]

package com.example;
import java.util.*;
import io.micronaut.runtime.Micronaut;

public class Application {
    public final Scanner sc;
    public Application(Scanner sc){
        this.sc = sc;
    }

    public void run() {
        System.out.println("== ๋ช…์–ธ ์•ฑ ==");
        while(true){
            System.out.print("๋ช…๋ น) ");
            String command = sc.nextLine().trim();
            // trim() : ํ˜น์‹œ ์žˆ์„์ง€ ๋ชจ๋ฅผ ์ขŒ์šฐ๊ณต๋ฐฑ ์ œ๊ฑฐ๋œ ๋ฒ„์ „์œผ๋กœ ์ฃผ์„ธ์š”!
            if(command.equals("์ข…๋ฃŒ")) { // ์ข…๋ฃŒ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
                break;
            }
            else if (command.equals("๋“ฑ๋ก")){
                System.out.print("๋ช…์–ธ : "); // #2 ๋ช…์–ธ๊ณผ ์ž‘๊ฐ€ ์ด๋ฆ„์„ ์ž…๋ ฅ๋ฐ›๊ธฐ
                String content = sc.nextLine().trim();
                System.out.print("์ž‘๊ฐ€ : ");
                String authorName = sc.nextLine().trim();

                System.out.println("1๋ฒˆ ๋ช…์–ธ์ด ๋“ฑ๋ก๋˜์—ˆ์Šต๋‹ˆ๋‹ค."); // #3 ๋“ฑ๋ก ๋ฌธ๊ตฌ ๋„์šฐ๊ธฐ
            }

        }
    }
}

<4๋‹จ๊ณ„>

โœ”๏ธ ๋ชฉํ‘œ: ๋“ฑ๋กํ•  ๋•Œ๋งˆ๋‹ค ์ƒ์„ฑ๋˜๋Š” ๋ช…์–ธ๋ฒˆํ˜ธ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜๊ธฐ

 

[Application.java]

package com.example;
import java.util.*;
import io.micronaut.runtime.Micronaut;

public class Application {
    public final Scanner sc;
    public Application(Scanner sc){
        this.sc = sc;
    }

    public void run() {
        System.out.println("== ๋ช…์–ธ ์•ฑ ==");

        long lastWiseSayingId = 1; // ์ œ์ผ ๊ฒ‰์— ์žˆ๋Š” ๊ณณ์— ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์•ผ์ง€ ์žฅ๊ธฐ๊ธฐ์–ต!

        while(true){
            System.out.print("๋ช…๋ น) ");
            String command = sc.nextLine().trim();
            // trim() : ํ˜น์‹œ ์žˆ์„์ง€ ๋ชจ๋ฅผ ์ขŒ์šฐ๊ณต๋ฐฑ ์ œ๊ฑฐ๋œ ๋ฒ„์ „์œผ๋กœ ์ฃผ์„ธ์š”!
            if(command.equals("์ข…๋ฃŒ")) { // ์ข…๋ฃŒ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
                break;
            }
            else if (command.equals("๋“ฑ๋ก")){
                System.out.print("๋ช…์–ธ : "); // #2 ๋ช…์–ธ๊ณผ ์ž‘๊ฐ€ ์ด๋ฆ„์„ ์ž…๋ ฅ๋ฐ›๊ธฐ
                String content = sc.nextLine().trim();
                System.out.print("์ž‘๊ฐ€ : ");
                String authorName = sc.nextLine().trim();

                System.out.printf("%d๋ฒˆ ๋ช…์–ธ์ด ๋“ฑ๋ก๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n",lastWiseSayingId); // #3 ๋“ฑ๋ก ๋ฌธ๊ตฌ ๋„์šฐ๊ธฐ
                lastWiseSayingId++; // #4 ๋“ฑ๋กํ•  ๋•Œ๋งˆ๋‹ค ๋ฒˆํ˜ธ ์ฆ๊ฐ€
            }

        }
    }
}

<5๋‹จ๊ณ„>

โœ”๏ธ ๋ชฉํ‘œ: ๋ชฉ๋ก ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ

 

[Wisesaying.java]

package com.example;
public class WiseSaying { // ๋“ฑ๋ก๋œ ๋ช…์–ธ๊ณผ ์ž‘๊ฐ€์ด๋ฆ„์„ ์ €์žฅ
    private long id; // ๋“ฑ๋ก ๋ฒˆํ˜ธ
    private String content; // ๋ช…์–ธ
    private String authorName; // ์ž‘๊ฐ€ ์ด๋ฆ„

    public WiseSaying(long id, String content, String authorName) {
        this.id = id;
        this.content = content;
        this.authorName = authorName;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }

    public String getAuthorName() {
        return authorName;
    }
}

 

[Application.java]

package com.example;
import java.util.*;
import io.micronaut.runtime.Micronaut;

public class Application {
    public final Scanner sc;
    public Application(Scanner sc){
        this.sc = sc;
    }

    public void run() {
        System.out.println("== ๋ช…์–ธ ์•ฑ ==");

        long lastWiseSayingId = 0; // ์ œ์ผ ๊ฒ‰์— ์žˆ๋Š” ๊ณณ์— ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์•ผ์ง€ ์žฅ๊ธฐ๊ธฐ์–ต!
        List<WiseSaying> wiseSayings = new ArrayList<>(); // wiseSayings ๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ค€ ํ›„ Arraylist ์ƒ์„ฑ

        while(true){
            System.out.print("๋ช…๋ น) ");
            String command = sc.nextLine().trim();
            // trim() : ํ˜น์‹œ ์žˆ์„์ง€ ๋ชจ๋ฅผ ์ขŒ์šฐ๊ณต๋ฐฑ ์ œ๊ฑฐ๋œ ๋ฒ„์ „์œผ๋กœ ์ฃผ์„ธ์š”!
            if(command.equals("์ข…๋ฃŒ")) { // ์ข…๋ฃŒ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
                break;
            }
            else if (command.equals("๋“ฑ๋ก")){
                long id = lastWiseSayingId + 1; // #4 ๋“ฑ๋กํ•  ๋•Œ๋งˆ๋‹ค ๋ฒˆํ˜ธ ์ฆ๊ฐ€
                System.out.print("๋ช…์–ธ : "); // #2 ๋ช…์–ธ๊ณผ ์ž‘๊ฐ€ ์ด๋ฆ„์„ ์ž…๋ ฅ๋ฐ›๊ธฐ
                String content = sc.nextLine().trim();
                System.out.print("์ž‘๊ฐ€ : ");
                String authorName = sc.nextLine().trim();

                WiseSaying wiseSaying = new WiseSaying(id, content, authorName);
                wiseSayings.add(wiseSaying); // ๋“ฑ๋ก๋œ ๋ช…์–ธ๋“ค์„ WiseSaying ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ

                System.out.printf("%d๋ฒˆ ๋ช…์–ธ์ด ๋“ฑ๋ก๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n", id); // #3 ๋“ฑ๋ก ๋ฌธ๊ตฌ ๋„์šฐ๊ธฐ
                lastWiseSayingId = id; 
            }
            else if (command.equals("๋ชฉ๋ก")) { // #5 ๋“ฑ๋ก๋œ ๋ช…์–ธ์˜ ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ
                System.out.println("๋ฒˆํ˜ธ  /  ์ž‘๊ฐ€  /  ๋ช…์–ธ");
                System.out.println("-".repeat(30)); // -๋ฅผ 30๋ฒˆ ๋ฐ˜๋ณตํ•ด์„œ ์ถœ๋ ฅ -------... ๋ž‘ ๊ฐ™์Œ

                for (int i = wiseSayings.size() - 1; i >= 0; i--){ // ์ž…๋ ฅ์˜ ์—ญ์ˆœ์œผ๋กœ ์ถœ๋ ฅ (๋ชฉ๋ก์˜ 1๋ฒˆ๋ถ€ํ„ฐ)
                    WiseSaying wiseSaying = wiseSayings.get(i);

                    // '๋ชฉ๋ก๋ฒˆํ˜ธ  ์ž‘๊ฐ€์ด๋ฆ„  ๋ช…์–ธ' ์„ ์ถœ๋ ฅ
                    System.out.printf("%d / %s / %s\n", wiseSaying.getId(), wiseSaying.getAuthorName(), wiseSaying.getContent());
                }
            }
        }
    }
}

 


<6๋‹จ๊ณ„> + ๊ฐ ๋ชจ๋“ˆ๋กœ ๊ธฐ๋Šฅ๋ถ„๋ฆฌ ๋ฐ ๊ตฌ์กฐ๊ฐœ์„ 

โœ”๏ธ ๋ชฉํ‘œ: ์‚ญ์ œ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ

 

[main.java]

package com.example;
import java.util.*;
public class main { // Applicaiton์ด ์ผํ•˜๋„๋ก ํ•จ
    public static void main(String[] args) {

        Container.init(); // ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ๋“ค์„ ๋ชจ์•„๋‘๋Š” ๊ณต๊ฐ„(Container) ์ดˆ๊ธฐํ™”

        new Application().run(); // ๊ธฐ๋ณธ์ ์œผ๋กœ Scanner๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Applicaiton

        Container.close();
        // ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ๋“ค์„ ๋ชจ์•„๋‘๋Š” ๊ณต๊ฐ„(Container) ์ž์›ํ•ด์ œ
    }
}

 

[SystemController.java]

package com.example.system.controller;

public class SystemController { // #1 ์ข…๋ฃŒํ•˜๊ธฐ
    public void exit() {
        System.out.println("๋ช…์–ธ์•ฑ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");
    }
}

 

[WiseSayingController.java]

package com.example.wiseSaying.controller;

import com.example.Rq;
import com.example.wiseSaying.entity.WiseSaying;
import com.example.Container;

import java.util.*;
import java.util.stream.IntStream;

public class WiseSayingController {
    private long lastWiseSayingId;
    private final List<WiseSaying> wiseSayings;

    public WiseSayingController() {
        lastWiseSayingId = 0;
        wiseSayings = new ArrayList<>();
    }

    public void write() { // #2~4 ๋“ฑ๋กํ•˜๊ธฐ
        long id = lastWiseSayingId + 1; // ๋“ฑ๋กํ•  ๋•Œ๋งˆ๋‹ค ๋ฒˆํ˜ธ ์ฆ๊ฐ€
        System.out.print("๋ช…์–ธ : "); // ๋ช…์–ธ๊ณผ ์ž‘๊ฐ€ ์ด๋ฆ„์„ ์ž…๋ ฅ๋ฐ›๊ธฐ
        String content = Container.getScanner().nextLine().trim();
        System.out.print("์ž‘๊ฐ€ : ");
        String authorName = Container.getScanner().nextLine().trim();

        WiseSaying wiseSaying = new WiseSaying(id, content, authorName);
        wiseSayings.add(wiseSaying); // ๋“ฑ๋ก๋œ ๋ช…์–ธ๋“ค์„ WiseSaying ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ

        System.out.printf("%d๋ฒˆ ๋ช…์–ธ์ด ๋“ฑ๋ก๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n", id); // ๋“ฑ๋ก ๋ฌธ๊ตฌ ๋„์šฐ๊ธฐ
        lastWiseSayingId = id;
    }

    public void list() { // #5 ๋“ฑ๋ก๋œ ๋ช…์–ธ์˜ ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ๊ธฐ
        System.out.println("๋ฒˆํ˜ธ / ์ž‘๊ฐ€ / ๋ช…์–ธ");
        System.out.println("-".repeat(30)); // -๋ฅผ 30๋ฒˆ ๋ฐ˜๋ณตํ•ด์„œ ์ถœ๋ ฅ -------... ๋ž‘ ๊ฐ™์Œ

        for (int i = wiseSayings.size() - 1; i >= 0; i--) { // ์ž…๋ ฅ์˜ ์—ญ์ˆœ์œผ๋กœ ์ถœ๋ ฅ (๋ชฉ๋ก์˜ 1๋ฒˆ๋ถ€ํ„ฐ)
            WiseSaying wiseSaying = wiseSayings.get(i);
            // '๋ชฉ๋ก๋ฒˆํ˜ธ  ์ž‘๊ฐ€์ด๋ฆ„  ๋ช…์–ธ' ์„ ์ถœ๋ ฅ
            System.out.printf("%d / %s / %s\n", wiseSaying.getId(), wiseSaying.getAuthorName(), wiseSaying.getContent());
        }
    }

    public void remove(Rq rq) { // #6 ๋ช…์–ธ ์‚ญ์ œํ•˜๊ธฐ

        int id = rq.getIntParam("id", -1); // id ๊ฐ’์„ ์ •์ˆ˜ํ™” ํ•ด์„œ ๋ณด๋‚ด์ฃผ๊ณ , ๋งŒ์•ฝ ์‹คํŒจํ•˜๋ฉด -1์„ ๋ณด๋‚ด์ค˜!

        if (id == -1) {
            System.out.println("id(์ •์ˆ˜)๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
            return;
        }
        // ์ž…๋ ฅํ•œ id์™€ ์ผ์น˜ํ•˜๋Š” ๋ช…์–ธ ๊ฐ์ฒด๋ฅผ ์ฐพ์Œ
        WiseSaying wiseSaying = findById(id);
        // ์ฐพ์€ ๋ช…์–ธ ๊ฐ์ฒด๋ฅผ ๋ฆฌ์ŠคํŠธ์—์„œ ์ œ๊ฑฐํ•จ
        wiseSayings.remove(wiseSaying);

        System.out.printf("%d๋ฒˆ ๋ช…์–ธ์ด ์‚ญ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n", id);
    }

    private WiseSaying findById(int id) {
        for (WiseSaying wiseSaying : wiseSayings) {
            if (wiseSaying.getId() == id) {
                return wiseSaying;
            }
        }
        return null;
    }
}

 

[WiseSaying.java]

package com.example.wiseSaying.entity;
public class WiseSaying { // ๋“ฑ๋ก๋œ ๋ช…์–ธ๊ณผ ์ž‘๊ฐ€์ด๋ฆ„์„ ์ €์žฅ
    private long id; // ๋“ฑ๋ก ๋ฒˆํ˜ธ
    private String content; // ๋ช…์–ธ
    private String authorName; // ์ž‘๊ฐ€ ์ด๋ฆ„

    public WiseSaying(long id, String content, String authorName) {
        this.id = id;
        this.content = content;
        this.authorName = authorName;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }

    public String getAuthorName() {
        return authorName;
    }
}

 

[Application.java]

package com.example;
import com.example.system.controller.SystemController;
import com.example.wiseSaying.controller.WiseSayingController;

import java.util.*;

public class Application {
    public void run() {
        System.out.println("== ๋ช…์–ธ ์•ฑ ==");

        SystemController systemController = new SystemController(); // ๊ฐ์ฒด ์ƒ์„ฑ
        WiseSayingController wiseSayingController = new WiseSayingController();

        while(true){
            System.out.print("๋ช…๋ น) "); // ์ž…๋ ฅ ๋ฐ›๊ธฐ
            String command = Container.getScanner().nextLine().trim();
            Rq rq = new Rq(command);

            switch (rq.getActionCode()) {
                case "์ข…๋ฃŒ":
                    systemController.exit();
                    return;
                case "๋“ฑ๋ก":
                    wiseSayingController.write();
                    break;
                case "๋ชฉ๋ก":
                    wiseSayingController.list();
                    break;
                case "์‚ญ์ œ":
                    wiseSayingController.remove(rq);
                    break;
            }
        }
    }
}

 

[Rq.java]

package com.example;
import java.util.HashMap;
import java.util.Map;
public class Rq {
    private String actionCode;
    private Map<String, String> params;

    public Rq(String command) {
        String[] commandBits = command.split("\\?", 2);
        actionCode = commandBits[0];

        params = new HashMap<>();

        if (commandBits.length == 1) return;

        String[] paramsBits = commandBits[1].split("&");

        for (String paramStr : paramsBits) {
            String[] paramStrBits = paramStr.split("=", 2);

            if (paramStrBits.length == 1) continue;

            String key = paramStrBits[0];
            String value = paramStrBits[1];

            params.put(key, value);
        }
    }

    public String getActionCode() {
        return actionCode;
    }

    public String getParam(String name) {
        return params.get(name);
    }

    public int getIntParam(String name, int defaultValue) {
        try {
            return Integer.parseInt(getParam(name));
        } catch (NumberFormatException e) {

        }
        return defaultValue;
    }
}

 

[Container.java]

package com.example;

import java.util.Scanner;
public class Container {
    private static Scanner sc;

    public static void init() {
        sc = new Scanner(System.in);
    }

    public static void close() {
        sc.close();
    }

    public static Scanner getScanner() {
        return sc;
    }
}

<7๋‹จ๊ณ„>

โœ”๏ธ ๋ชฉํ‘œ: ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ช…์–ธ์„ ์‚ญ์ œ ํ–ˆ์„ ๋•Œ ์˜ˆ์™ธ์ฒ˜๋ฆฌํ•˜๊ธฐ

 

[WiseSayingController.java]

package com.example.wiseSaying.controller;

import com.example.Rq;
import com.example.wiseSaying.entity.WiseSaying;
import com.example.Container;

import java.util.*;
import java.util.stream.IntStream;

public class WiseSayingController {
    private long lastWiseSayingId;
    private final List<WiseSaying> wiseSayings;

    public WiseSayingController() {
        lastWiseSayingId = 0;
        wiseSayings = new ArrayList<>();
    }

    public void write() { // #2~4 ๋“ฑ๋กํ•˜๊ธฐ
        long id = lastWiseSayingId + 1; // ๋“ฑ๋กํ•  ๋•Œ๋งˆ๋‹ค ๋ฒˆํ˜ธ ์ฆ๊ฐ€
        System.out.print("๋ช…์–ธ : "); // ๋ช…์–ธ๊ณผ ์ž‘๊ฐ€ ์ด๋ฆ„์„ ์ž…๋ ฅ๋ฐ›๊ธฐ
        String content = Container.getScanner().nextLine().trim();
        System.out.print("์ž‘๊ฐ€ : ");
        String authorName = Container.getScanner().nextLine().trim();

        WiseSaying wiseSaying = new WiseSaying(id, content, authorName);
        wiseSayings.add(wiseSaying); // ๋“ฑ๋ก๋œ ๋ช…์–ธ๋“ค์„ WiseSaying ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ

        System.out.printf("%d๋ฒˆ ๋ช…์–ธ์ด ๋“ฑ๋ก๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n", id); // ๋“ฑ๋ก ๋ฌธ๊ตฌ ๋„์šฐ๊ธฐ
        lastWiseSayingId = id;
    }

    public void list() { // #5 ๋“ฑ๋ก๋œ ๋ช…์–ธ์˜ ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ๊ธฐ
        System.out.println("๋ฒˆํ˜ธ / ์ž‘๊ฐ€ / ๋ช…์–ธ");
        System.out.println("-".repeat(30)); // -๋ฅผ 30๋ฒˆ ๋ฐ˜๋ณตํ•ด์„œ ์ถœ๋ ฅ -------... ๋ž‘ ๊ฐ™์Œ

        for (int i = wiseSayings.size() - 1; i >= 0; i--) { // ์ž…๋ ฅ์˜ ์—ญ์ˆœ์œผ๋กœ ์ถœ๋ ฅ (๋ชฉ๋ก์˜ 1๋ฒˆ๋ถ€ํ„ฐ)
            WiseSaying wiseSaying = wiseSayings.get(i);
            // '๋ชฉ๋ก๋ฒˆํ˜ธ  ์ž‘๊ฐ€์ด๋ฆ„  ๋ช…์–ธ' ์„ ์ถœ๋ ฅ
            System.out.printf("%d / %s / %s\n", wiseSaying.getId(), wiseSaying.getAuthorName(), wiseSaying.getContent());
        }
    }

    public void remove(Rq rq) { // #6 ๋ช…์–ธ ์‚ญ์ œํ•˜๊ธฐ

        int id = rq.getIntParam("id", -1); // id ๊ฐ’์„ ์ •์ˆ˜ํ™” ํ•ด์„œ ๋ณด๋‚ด์ฃผ๊ณ , ๋งŒ์•ฝ ์‹คํŒจํ•˜๋ฉด -1์„ ๋ณด๋‚ด์ค˜!

        if (id == -1) {
            System.out.println("id(์ •์ˆ˜)๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
            return;
        }
        // ์ž…๋ ฅํ•œ id์™€ ์ผ์น˜ํ•˜๋Š” ๋ช…์–ธ ๊ฐ์ฒด๋ฅผ ์ฐพ์Œ
        WiseSaying wiseSaying = findById(id);
        // ์ฐพ์€ ๋ช…์–ธ ๊ฐ์ฒด๋ฅผ ๋ฆฌ์ŠคํŠธ์—์„œ ์ œ๊ฑฐํ•จ
        if (wiseSaying == null) { // #7 ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ช…์–ธ์‚ญ์ œ์— ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ
            System.out.printf("%d๋ฒˆ ๋ช…์–ธ์€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.\n", id);
            return;
        }
        // ์ฐพ์€ ๋ช…์–ธ๊ฐ์ฒด๋ฅผ ๋ฆฌ์ŠคํŠธ์—์„œ ์ œ๊ฑฐ
        wiseSayings.remove(wiseSaying);
        System.out.printf("%d๋ฒˆ ๋ช…์–ธ์ด ์‚ญ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n", id);
    }

    private WiseSaying findById(int id) {
        for (WiseSaying wiseSaying : wiseSayings) {
            if (wiseSaying.getId() == id) {
                return wiseSaying;
            }
        }
        return null;
    }
}

<8๋‹จ๊ณ„>

โœ”๏ธ ๋ชฉํ‘œ: ์ˆ˜์ • ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ

 

[Application.java]

package com.example;
import com.example.system.controller.SystemController;
import com.example.wiseSaying.controller.WiseSayingController;

import java.util.*;

public class Application {
    public void run() {
        System.out.println("== ๋ช…์–ธ ์•ฑ ==");

        SystemController systemController = new SystemController(); // ๊ฐ์ฒด ์ƒ์„ฑ
        WiseSayingController wiseSayingController = new WiseSayingController();

        while(true){
            System.out.print("๋ช…๋ น) "); // ์ž…๋ ฅ ๋ฐ›๊ธฐ
            String command = Container.getScanner().nextLine().trim();
            Rq rq = new Rq(command);

            switch (rq.getActionCode()) {
                case "์ข…๋ฃŒ":
                    systemController.exit();
                    return;
                case "๋“ฑ๋ก":
                    wiseSayingController.write();
                    break;
                case "๋ชฉ๋ก":
                    wiseSayingController.list();
                    break;
                case "์‚ญ์ œ":
                    wiseSayingController.remove(rq);
                    break;
                case "์ˆ˜์ •":
                    wiseSayingController.modify(rq);
                    break;
            }
        }
    }
}

 

[WiseSayingController.java]

package com.example.wiseSaying.controller;

import com.example.Rq;
import com.example.wiseSaying.entity.WiseSaying;
import com.example.Container;

import java.util.*;
import java.util.stream.IntStream;

public class WiseSayingController {
    private long lastWiseSayingId;
    private final List<WiseSaying> wiseSayings;

    public WiseSayingController() {
        lastWiseSayingId = 0;
        wiseSayings = new ArrayList<>();
    }

    private WiseSaying findById(int id) {
        for (WiseSaying wiseSaying : wiseSayings) {
            if (wiseSaying.getId() == id) {
                return wiseSaying;
            }
        }

        return null;
    }

    public void write() { // #2~4 ๋“ฑ๋กํ•˜๊ธฐ
        long id = lastWiseSayingId + 1; // ๋“ฑ๋กํ•  ๋•Œ๋งˆ๋‹ค ๋ฒˆํ˜ธ ์ฆ๊ฐ€
        System.out.print("๋ช…์–ธ : "); // ๋ช…์–ธ๊ณผ ์ž‘๊ฐ€ ์ด๋ฆ„์„ ์ž…๋ ฅ๋ฐ›๊ธฐ
        String content = Container.getScanner().nextLine().trim();
        System.out.print("์ž‘๊ฐ€ : ");
        String authorName = Container.getScanner().nextLine().trim();

        WiseSaying wiseSaying = new WiseSaying(id, content, authorName);
        wiseSayings.add(wiseSaying); // ๋“ฑ๋ก๋œ ๋ช…์–ธ๋“ค์„ WiseSaying ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ

        System.out.printf("%d๋ฒˆ ๋ช…์–ธ์ด ๋“ฑ๋ก๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n", id); // ๋“ฑ๋ก ๋ฌธ๊ตฌ ๋„์šฐ๊ธฐ
        lastWiseSayingId = id;
    }

    public void list() { // #5 ๋“ฑ๋ก๋œ ๋ช…์–ธ์˜ ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ๊ธฐ
        System.out.println("๋ฒˆํ˜ธ / ์ž‘๊ฐ€ / ๋ช…์–ธ");
        System.out.println("-".repeat(30)); // -๋ฅผ 30๋ฒˆ ๋ฐ˜๋ณตํ•ด์„œ ์ถœ๋ ฅ -------... ๋ž‘ ๊ฐ™์Œ

        for (int i = wiseSayings.size() - 1; i >= 0; i--) { // ์ž…๋ ฅ์˜ ์—ญ์ˆœ์œผ๋กœ ์ถœ๋ ฅ (๋ชฉ๋ก์˜ 1๋ฒˆ๋ถ€ํ„ฐ)
            WiseSaying wiseSaying = wiseSayings.get(i);
            // '๋ชฉ๋ก๋ฒˆํ˜ธ  ์ž‘๊ฐ€์ด๋ฆ„  ๋ช…์–ธ' ์„ ์ถœ๋ ฅ
            System.out.printf("%d / %s / %s\n", wiseSaying.getId(), wiseSaying.getAuthorName(), wiseSaying.getContent());
        }
    }

    public void remove(Rq rq) { // #6 ๋ช…์–ธ ์‚ญ์ œํ•˜๊ธฐ

        int id = rq.getIntParam("id", -1); // id ๊ฐ’์„ ์ •์ˆ˜ํ™” ํ•ด์„œ ๋ณด๋‚ด์ฃผ๊ณ , ๋งŒ์•ฝ ์‹คํŒจํ•˜๋ฉด -1์„ ๋ณด๋‚ด์ค˜!

        if (id == -1) {
            System.out.println("id(์ •์ˆ˜)๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
            return;
        }
        // ์ž…๋ ฅํ•œ id์™€ ์ผ์น˜ํ•˜๋Š” ๋ช…์–ธ ๊ฐ์ฒด๋ฅผ ์ฐพ์Œ
        WiseSaying wiseSaying = findById(id);

        if (wiseSaying == null) { // #7 ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ช…์–ธ์‚ญ์ œ์— ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ
            System.out.printf("%d๋ฒˆ ๋ช…์–ธ์€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.\n", id);
            return;
        }
        // ์ฐพ์€ ๋ช…์–ธ๊ฐ์ฒด๋ฅผ ๋ฆฌ์ŠคํŠธ์—์„œ ์ œ๊ฑฐ
        wiseSayings.remove(wiseSaying);

        System.out.printf("%d๋ฒˆ ๋ช…์–ธ์ด ์‚ญ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n", id);
    }

    public void modify(Rq rq) {
        int id = rq.getIntParam("id", -1);

        if (id == -1) {
            System.out.println("id(์ •์ˆ˜)๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
            return;
        }

        WiseSaying wiseSaying = findById(id); // ์ž…๋ ฅํ•œ id์™€ ์ผ์น˜ํ•˜๋Š” ๋ช…์–ธ ๊ฐ์ฒด๋ฅผ ์ฐพ์Œ

        if (wiseSaying == null) { // ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ช…์–ธ์‚ญ์ œ์— ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ (์ˆ˜์ •ํ•  ๋•Œ๋„)
            System.out.printf("%d๋ฒˆ ๋ช…์–ธ์€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.\n", id);
            return;
        }
        // #8 ๋ช…์–ธ ์ˆ˜์ •ํ•˜๊ธฐ
        System.out.printf("๋ช…์–ธ(๊ธฐ์กด) : %s\n", wiseSaying.getContent());
        System.out.print("๋ช…์–ธ : ");
        String content = Container.getScanner().nextLine().trim();

        System.out.printf("์ž‘๊ฐ€(๊ธฐ์กด) : %s\n", wiseSaying.getAuthorName());
        System.out.print("์ž‘๊ฐ€ : ");
        String authorName = Container.getScanner().nextLine().trim();

        wiseSaying.setContent(content);
        wiseSaying.setAuthorName(authorName);

        System.out.printf("%d๋ฒˆ ๋ช…์–ธ์ด ์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n", id);
    }
}

 

[WiseSaying.java]

package com.example.wiseSaying.entity;
public class WiseSaying { // ๋“ฑ๋ก๋œ ๋ช…์–ธ๊ณผ ์ž‘๊ฐ€์ด๋ฆ„์„ ์ €์žฅ
    private long id; // ๋“ฑ๋ก ๋ฒˆํ˜ธ
    private String content; // ๋ช…์–ธ
    private String authorName; // ์ž‘๊ฐ€ ์ด๋ฆ„

    public WiseSaying(long id, String content, String authorName) {
        this.id = id;
        this.content = content;
        this.authorName = authorName;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }

    public String getAuthorName() {
        return authorName;
    }

    public void setContent(String content) {
        this.content = content;
    }
    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }
}

 


๐ŸŽ‰ ๊ฒฐ๊ณผ๋ฌผ


๐ŸŒŸ ๋ณต์Šตํ•˜๊ธฐ

 

๐Ÿ’ก ๋ฆฌํŽ™ํ† ๋ง

- ์†Œ์Šค์ฝ”๋“œ๋Š” ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์‰ฌ์–ด์•ผ ํ•œ๋‹ค.

  • ํฐ ๋ฉ์–ด๋ฆฌ์˜ ํ”„๋กœ๊ทธ๋žจ์€ ๋‚˜์ค‘์— ์œ ์ง€๋ณด์ˆ˜ ํ•˜๊ธฐ ์–ด๋ ค์›€!

- ์ ์ ˆํ•œ ๋‹จ์œ„๋กœ ๋‚˜๋ˆ ์•ผ ์ข‹์Œ

- ์ž‘์€ ํ”„๋กœ๊ทธ๋žจ์˜ ์กฐํ•ฉ์œผ๋กœ ๋งŒ๋“ค๊ธฐ

- ๋‚˜๋ˆˆ ํ”„๋กœ๊ทธ๋žจ ๋ผ๋ฆฌ ํ†ต์‹ ์„ ํ•  ๋•Œ ๊ทœ์น™์ด ์žˆ์Œ

  • ์•ˆ์ชฝ์— ์žˆ๋Š” ํ†ต์‹  ์ฑ„๋„์˜ ๊ฐœ์ˆ˜๋Š” ์ ์„ ์ˆ˜๋ก ์ข‹๋‹ค
  • ๋ช…๋ น์˜ ํ๋ฆ„์€ ์ˆ˜์ง์ ์ด๋ฉด ์ข‹๋‹ค
    • ํŒŒ์•…ํ•˜๊ธฐ ์‰ฝ๊ธฐ ๋•Œ๋ฌธ

 

๐Ÿ’ก ์ปจํŠธ๋กค๋Ÿฌ

  • Appicaiton์—์„œ ์ฃผ๋Š” ๋ช…๋ น์–ด๋ฅผ ๋ฐ›์•„์„œ, ์ถœ๋ ฅํ•ด์ฃผ๋Š” ์—ญํ• 
  • ์™ธ๋ถ€์—์„œ ๋“ค์–ด์˜ค๋Š” ์ž…๋ ฅ์„ ๋‹ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„

 

๐Ÿ’ก Rq ํด๋ž˜์Šค

: request ์š”์ฒญ

  • Rq ํด๋ž˜์Šค๋Š” ์™œ ์“ฐ๋Š”๊ฑธ๊นŒ? (์žฌ๋ฐŒ๋Š” ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด์ž!)
    • ๊ฐ€์ •์ง‘๊ณผ ์†Œ ํ•œ ๋งˆ๋ฆฌ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž!
    • ์†Œ๊ณ ๊ธฐ๋ฅผ ๋จน๊ณ ์‹ถ์„ ๋•Œ, ์šฐ๋ฆฌ๋Š” ์†Œ๋ฅผ ๋„์ถ•์—…์ž์—๊ฒŒ ์ฃผ๊ณ  ๋ฐ›์•„์˜จ ๊ณ ๊ธฐ๋ฅผ ๊ตฌ์›Œ๋จน๋Š”๋‹ค.
    • ์–ด๋А๋‚  ๋งˆ์„ ์˜†์— ์–ด๋–ค ์ƒ์ ์ด ์ƒ๊ธฐ๋ฉด์„œ ์œก๋ฅ˜ ์š”๋ฆฌ์‚ฌ๊ฐ€ ์™”๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž!
    • ์ด์ œ๋Š” ์†Œ๊ณ ๊ธฐ๊ฐ€ ๋จน๊ณ  ์‹ถ์„ ๋•Œ, ์†Œ๋ฅผ ์œก๋ฅ˜ ์š”๋ฆฌ์‚ฌ์—๊ฒŒ ๊ฑด๋„ค์ฃผ๋ฉด ์•Œ์•„์„œ ์šฉ๋„์— ๋งž์ถฐ์„œ ์ž๋ฅด๊ณ  ๋ณด๊ด€ํ•˜๊ณ  ์š”๋ฆฌํ•ด์ค€๋‹ค!!!
    • ์ด๋•Œ, ์œก๋ฅ˜ ์š”๋ฆฌ์‚ฌ์˜ ์—ญํ• ์ด Rq ํด๋ž˜์Šค๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
    → ์›๋ž˜๋Š” ๋ฐ›์€ ๊ณ ๊ธฐ๋“ค์„ ๊ฐ๊ฐ์˜ ์ง‘์—์„œ ์Šค์Šค๋กœ ๊ด€๋ฆฌํ–ˆ๋Š”๋ฐ, ์œก๋ฅ˜ ์š”๋ฆฌ์‚ฌ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†์–ด์ง„ ๊ฒƒ์ด๋‹ค.

*Rq ํด๋ž˜์Šค๋ฅผ ์ „์ฒด์ ์ธ ๋ช…๋ น์–ด์— ๋„์ž…ํ•˜์—ฌ ๋ผ์šฐํŒ… ์ฝ”๋“œ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์ ์„ ์ˆ˜ ์žˆ๋‹ค.

*getIntPharam์„ ๋„์ž…ํ•˜์—ฌ ์œ ํšจ์„ฑ ์ฒดํฌ ๋กœ์ง์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

“์‚ฌ์šฉ๋ฒ•์„ ๊ผญ ์ดํ•ดํ•˜๊ณ  ์“ฐ์ž!”