[HW 1] 자료구조 레시피 메모장 만들기

김지현·2023년 10월 17일
2

JAVA

목록 보기
7/14

문제

  • 입력값
    • 저장할 자료구조명을 입력합니다. (List / Set / Map)
    • 내가 좋아하는 요리 제목을 먼저 입력합니다.
    • 이어서 내가 좋아하는 요리 레시피를 한문장씩 입력합니다.
    • 입력을 마쳤으면 마지막에 “끝” 문자를 입력합니다.
  • 출력값
    • 입력이 종료되면 저장한 자료구조 이름과 요리 제목을 괄호로 감싸서 먼저 출력 해줍니다.
    • 이어서, 입력한 모든 문장앞에 번호를 붙여서 입력 순서에 맞게 모두 출력 해줍니다.

풀이

import java.util.*;

public class HW_02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String struct = sc.nextLine();
        String name = sc.nextLine();
        String recipe = "";

        if(struct.equals("List")) {
            ArrayList<String> strList = new ArrayList<String>();
            while(!recipe.equals("끝")){
                recipe = sc.nextLine();
                strList.add(recipe);
            }

            System.out.println("[ " + struct + " 으로 저장된" + name + " ]");

            for(int i = 0; i < strList.size() - 1; i++)
                System.out.println(i+1 + ". " + strList.get(i));
        }
        else if (struct.equals("Set")){
            Set<String> strSet = new LinkedHashSet<String>();
            while(true){
                recipe = sc.nextLine();
                if(recipe.equals("끝")) break;
                strSet.add(recipe);
            }

            System.out.println("[ " + struct + " 으로 저장된" + name + " ]");

            int i = 1;
            for(String value: strSet)
                System.out.println(i++ + ". " + value);
        }
        else{
            Map<Integer, String> strMap = new HashMap<Integer, String >();
            int j = 0;
            while(!recipe.equals("끝")){
                recipe = sc.nextLine();
                strMap.put(j++, recipe);
            }

            System.out.println("[ " + struct + " 으로 저장된" + name + " ]");

            for(int i = 0; i < strMap.size() - 1; i++)
                System.out.println(i+1 + ". " + strMap.get(i));
        }
    }
}
profile
Server Developer

0개의 댓글