
출력 내용
Sol2 클래스는 위의 요구사항을 구현하고 있다. 메인 메서드에서는 사용자의 입력을 처리하고, 입력한 자료구조에 따라 요리 레시피를 저장하는 로직이 구현되어 있다.
LinkedList를 사용하여 요리 레시피를 순서대로 저장하고 출력한다.LinkedHashSet을 사용하여 요리 레시피를 저장하고 중복을 허용하지 않으면서 순서대로 출력한다.HashMap을 사용하여 요리 레시피를 저장하고, 키로는 입력 순서를, 값으로는 레시피 내용을 저장한다.package week02;
import java.util.*;
public class Sol2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String choiceDataStructure = sc.nextLine();
String cookingName = sc.nextLine();
int i = 1,j=0;
if(Objects.equals(choiceDataStructure,"List")){
LinkedList<String> recipeList = new LinkedList<String>();
while(true){
String rows = sc.nextLine();
if(Objects.equals(rows,"끝"))
break;
recipeList.add(rows);
}
System.out.println("[ "+choiceDataStructure+" 으로 저장된 "+cookingName + " ]");
while(!recipeList.isEmpty()){
System.out.println(i+". "+recipeList.poll());
i++;
}
}
else if (Objects.equals(choiceDataStructure,"Set")) {
LinkedHashSet<String> recipeSet = new LinkedHashSet<>();
while(true){
String rows = sc.nextLine();
if(Objects.equals(rows,"끝"))
break;
recipeSet.add(rows);
}
Iterator iterator = recipeSet.iterator();
System.out.println("[ "+choiceDataStructure+" 으로 저장된 "+cookingName + " ]");
while(j<recipeSet.size()){
System.out.println(i+". "+iterator.next());
i++;
j++;
}
}
else if (Objects.equals(choiceDataStructure,"Map")) {
Map<Integer,String> recipeMap = new HashMap<>();
while(true){
String rows = sc.nextLine();
if(Objects.equals(rows,"끝"))
break;
recipeMap.put(j,rows);
j++;
}
System.out.println("[ "+choiceDataStructure+" 으로 저장된 "+cookingName + " ]");
for (int k = 0; k < recipeMap.size(); k++) {
System.out.println((k+1)+". "+recipeMap.get(k));
}
}
}
}