본문 바로가기
TIL/내배캠 과제

23.06.03

by J1-H00N 2023. 6. 5.

팀과제 메모장 만들기에서 memoList 클래스를 만들기로 했다.

package memoList;

import java.util.ArrayList;
import java.util.Scanner;

public class MemoList {
    // 입력한 순서대로 출력하기에는 ArrayList가 제일 깔끔한 것 같아 ArrayList를 활용했습니다.
    private ArrayList<Memo> memos = new ArrayList<>();

    // 메모 전체를 조회하고 입력, 수정하기 위한 필수 요구 사항 Gettr/Setter
    public ArrayList<Memo> getMemos() {
        return memos;
    }

    public void setMemos(ArrayList<Memo> memos) {
        this.memos = memos;
    }

    // 수정/삭제할 때 선택한 메모를 초기화 하기 전에 미리 해당 메모의 번호를 저장해두는 메서드
    private int saveMemoNumber(int selectNumber) {
        int saveMemoNumber = selectNumber;
        return saveMemoNumber;
    }

    // 메모 전체 출력하기
    public void printMemos() {
        while (true) {
            for (Memo memo : getMemos()) {
                System.out.println(memo.toString(memo.getMemoNumber(), memo.getWriterName(), memo.getWritedMemo(), memo.getWritedTime()));
            }
            System.out.println("\n------------------------------------------------\n");
            System.out.println("1. 메모 추가     2. 메모 수정      3. 메모 삭제");

            Scanner sc = new Scanner(System.in);
            int check = sc.nextInt(); // 아래에서 메모 작업을 수행할 때 받을 번호 입력
            switch (check) {
                case 1 :
                    // 메모 추가 메서드
                    break;
                case 2 :
                    // 메모 수정 메서드
                    break;
                case 3 :
                    // 메모 삭제 메서드
                case 0 :
                    return; // 메모 창 작업 닫기
                default:
                    System.out.println("잘못 입력하였습니다. 다시 입력해주세요");
            }
        }
    }
}

위와 같이 만들다 보니 기존 아영님이 만드신 코드에서 수정이 필요해 임의로 수정함

package memoList;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

// 메모 한 건에 대한 정보를 가진 클래스
public class Memo {
    private int memoNumber; // 글 번호
    private String writerName; // 작성자명
    private String password; // 비밀번호
    private String writedMemo; // 글 내용 // 한지훈 : 제가 기억하기 편한 변수명으로 수정했습니다.
    private String writedTime; // 작성일 (컴퓨터 시스템의 날짜, 시간을 자동으로 저장)

    // getter/setter는 완성 후 사용하지 않는 것을 지워주세요


    public int getMemoNumber() {
        return memoNumber;
    }

    public void setMemoNumber(int memoNumber) {
        this.memoNumber = memoNumber;
    }

    public String getWriterName() {
        return writerName;
    }

    public void setWriterName(String writerName) {
        this.writerName = writerName;
    }

    public String getPassword() {
        return password;
    }

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

    public String getWritedMemo() {
        return writedMemo;
    }

    public void setWritedMemo(String writedMemo) {
        this.writedMemo = writedMemo;
    }

    public String getWritedTime() {
        return writedTime;
    }

    public void setWritedTime(String writedTime) {
        this.writedTime = writedTime;
    }

    // 리스트에 입력할 때 변수를 받는 생성자 메서드
    public Memo(int memoNumber, String writerName, String password, String writedMemo, String writedTime) {
        this.memoNumber = memoNumber;
        this.writerName = writerName;
        this.writedTime = writedTime;
        this.password = password;
        this.writedMemo = writedMemo;
    }

    public String toString(int memoNumber, String writerName, String writedMemo, String writedTime) { // 비밀번호는 출력 X
        return String.format("%d.  %s \t: %s \t\t %s",memoNumber, writerName, writedMemo, writedTime); // 1.  한지훈   : 안녕하세요         2023/06/03 14:56:27
    }

    /*
     * 생성 시 현재 시간을 저장하도록 해둠
     * 입력 시 저장하는 것으로 변경하고 싶으시면 아래 코드 복사해서 입력 시 값 넣어주세요!
     */

    public void editWtritedTime(){ // 기본 생성자는 변수들을 입력받는데 사용하고 싶어 메서드명을 수정했습니다.
        LocalDateTime now = LocalDateTime.now();  // 현재 시간
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        // 포맷 정의 포맷은 연/월/일 시간:분:초 >> 변경 가능
        //ex) 2023/06/03 13:42:10
        this.writedTime = now.format(formatter); // 포맷 적용, 변수 저장
        System.out.println(writedTime); //테스트용 출력 / 추후 삭제 해주세요
    }
}
package memoList;

public class Main {
    public static void main(String args[]) {
//        Memo memo = new Memo(); 메모 추가 및 수정은 MomoList 내에서 처리할거라 필요 없을겁니다.

        MemoList memoList = new MemoList();
        memoList.printMemos();
    }
}

 

위와 같이 목록을 ArrayList로 만들면 조회에서 O(N)이 된다는 단점이 있고, 

Map으로 만들면 삭제가 O(N)이 된다는 단점이 있다.

어차피 둘 다 비슷한 복잡도를 가지지만 Map은 삭제과정에서 각 키의 값들을 당기는 과정이 필요하기에 이 과정이 상당히 비효율적이고 구현하기도 난이도가 있는 부분이라 ArrayList를 유지하기로 했다.

'TIL > 내배캠 과제' 카테고리의 다른 글

23.06.07  (0) 2023.06.07
23.06.05  (0) 2023.06.05
23.06.02  (0) 2023.06.02
23.06.01  (0) 2023.06.01
23.05.31  (0) 2023.05.31