본문 바로가기
❤️‍🔥TIL (Today I Learned)

[TIL] 2022-11-23(18day)

by elicho91 2022. 11. 23.

메모장 만들기(미니 프로젝트)


👉 Memo

더보기
public class Memo implements Comparable<Memo> {

    private int number;
    private String name;
    private String password;
    private String content;
    private String date;

    public Memo(int number, String name, String password, String content, String date) {
        this.number = number;
        this.name = name;
        this.password = password;
        this.content = content;
        this.date = date;
    }

    public int getNumber() {
        return number;
    }

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    public String getContent() {
        return content;
    }

    public String getDate() {
        return date;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPassword(String password) {
        this.password = password;
    }

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

    public void setDate(String date) {
        this.date = date;
    }

    @Override
    public int compareTo(Memo memo) {
        return this.date.compareTo(memo.date);
    }
}

 

👉 MemoList

더보기
import java.text.SimpleDateFormat;
import java.util.*;

public class MemoList {

    private final List<Memo> memoList;

    public MemoList() {
        this.memoList = new ArrayList<>();
    }

    public void create() {
        Scanner scanner = new Scanner(System.in);
        int number = this.memoList.size() + 1;

        System.out.print("Enter your name: ");
        String name = scanner.nextLine();

        System.out.print("Enter your password: ");
        String password = scanner.nextLine();

        System.out.print("Enter the content: ");
        String content = scanner.nextLine();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.KOREA);
        String date = simpleDateFormat.format(Calendar.getInstance().getTime());

        this.memoList.add(new Memo(number, name, password, content, date));
    }

    public void update() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the memo number.");

        int memoNumber = Integer.parseInt(scanner.nextLine());
        Memo memo = getMemo(memoNumber);

        if (memo != null) {
            System.out.println("Enter your password.");
            String inputPassword = scanner.nextLine();
            if (inputPassword.equals(memo.getPassword())) {
                System.out.println("Enter the content.");
                String inputContent = scanner.nextLine();
                memo.setContent(inputContent);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.KOREA);
                memo.setDate(simpleDateFormat.format(Calendar.getInstance().getTime()));
            } else {
                System.out.println("Incorrect password.");
            }
        } else {
            System.out.println("Not exists.");
        }
    }

    public void delete() {
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("삭제하실 글의 번호를 입력해주십쇼. 해당 기능을 종료하시려면 0000를 눌러주시길바랍니다.");
            String input = sc.nextLine();
            // 되돌아가기
            if (input.equals("0000")) {
                break;
            }
            // 삭제 구현
            int memoNumber = Integer.parseInt(input);
            Memo memoAction = getMemo(memoNumber);
            if (memoAction != null) {
                System.out.println("해당 글의 비밀번호를 입력해주십쇼");
                String pw = sc.nextLine();

                if (memoAction.getPassword().equals(pw)) { // 글의 비밀번호와 pw가 맞을 경우
                    memoList.remove(memoNumber - 1); // 글을 지운다
                    System.out.println("삭제가 완료되었습니다.");
                    int len = this.memoList.size();
                    for (int i = 0; i < len; i++) {
                        if (this.memoList.get(i).getNumber() - 1 != i) {
                            this.memoList.get(i).setNumber(i + 1);
                        }
                    }
                    break;
                } else {
                    System.out.println("비밀번호가 틀렸습니다. 처음부터 다시 진행해주십쇼");
                }
            } else {
                System.out.println("해당 글의 번호가 존재하지 않습니다.");
            }
        }
    }

    public void showAll() {
        List<Memo> sortedByDate = new ArrayList<>(this.memoList);
        Collections.sort(sortedByDate);
        int size = sortedByDate.size();

        for (int i = size - 1; i >= 0; i--) {
            Memo memo = sortedByDate.get(i);
            System.out.println("=============================================");
            System.out.printf("| No.%3d | by %7s | %19s |%n", memo.getNumber(), memo.getName(), memo.getDate());
            System.out.println("---------------------------------------------");

            String content = memo.getContent();
            int len = memo.getContent().length();
            int width = 41;
            
            for (int j = 0; j < len; j += width) {
                System.out.printf("| %-41s |%n", content.substring(j, Math.min(j + width, len)));
            }
            System.out.println("=============================================");

            if (i > 0) {
                System.out.println("|                                           |");
            }
        }
    }

    public Memo getMemo(int memoNumber) {
        if (0 < memoNumber && memoNumber < this.memoList.size()) {
            for (Memo memo : this.memoList) {
                if (memoNumber == memo.getNumber()) {
                    return memo;
                }
            }
        }
        throw new RuntimeException("Not exists.");
    }
}

 

👉 MemoApplication

더보기
import java.util.Scanner;

public class MemoApplication {

    public static void main(String[] args) {
        MemoList memoList = new MemoList();
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome to Memo!");
        do {
            System.out.println("================");
            System.out.println("|     Menu     |");
            System.out.println("================");
            System.out.println("| 1. Create    |");
            System.out.println("| 2. Show all  |");
            System.out.println("| 3. Update    |");
            System.out.println("| 4. Delete    |");
            System.out.println("| 5. Exit      |");
            System.out.println("================");
            System.out.println("Please enter a number from 1 to 5.");

            String input = scanner.nextLine();

            try {
                if (input.equals("1")) {
                    memoList.create();
                } else if (input.equals("2")) {
                    memoList.showAll();
                } else if (input.equals("3")) {
                    memoList.update();
                } else if (input.equals("4")) {
                    memoList.delete();
                } else if (input.equals("5")) {
                    break;
                } else {
                    System.out.println("Invalid input.");
                    System.out.println("Please enter a number from 1 to 5.");
                }
            } catch (RuntimeException e) {
                System.out.println(e.getMessage());
            }

        } while (true);

        scanner.close();
    }
}

1. Create

 

2. Show All

 

3. Update

 

4. Delete


🙋‍♂️ 소감 : 

간단하네~ 라고 생각했던건데... 왜이렇게 헤맸는지... 
뭔가 DB연동 없이 콘솔창으로만 출력되게 작업하는게 어색하다.
뭔가 계속 sql문을 적어줘야할것 같고...ㅎㅎ
팀원분들의 도움으로 나머지 메서드들도 완성 할 수 있었다 :)

😈 아는 내용이라고 그냥 넘어가지 않기! 😈

'❤️‍🔥TIL (Today I Learned)' 카테고리의 다른 글

[TIL] 2022-11-25(20day)  (0) 2022.11.27
[TIL] 2022-11-24(19day)  (0) 2022.11.24
[TIL] 2022-11-22(17day)  (0) 2022.11.22
[TIL] 2022-11-21(16day)  (0) 2022.11.21
[TIL] 2022-11-18(15day)  (0) 2022.11.18

댓글