java (8) - 오점뭐

ysh·2023년 7월 7일
0

Spring Boot

목록 보기
23/53
post-custom-banner

오늘 점심 뭐먹지

package hello;

import java.util.List;

public interface Menu {
    // 작성된 메뉴 출력
    public List<String> printMenu();

    // 사용자의 입력
    public String select(List<String> foodList1);
    
    // 입력된 값에 따라 메뉴 출력
    public void printSelectedMenu(List<String> foodList1, String selectedMenu);
}
package hello;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MenuImpl implements Menu{

    @Override
    public List<String> printMenu() {
        List<String> foodList1 = new ArrayList<>();
        foodList1.add("햄버거");
        foodList1.add("파스타");
        foodList1.add("제육볶음");
        foodList1.add("돈까스");
        foodList1.add("김치찌개");

        int idx = 1;
        for (String menu: foodList1){
            System.out.println(idx++ + ". " + menu);
        }
        System.out.println();
        System.out.println("무작위로 추천 받으시고 싶다면 '랜덤'을 입력해주세요");
        return foodList1;
    }

    @Override
    public String select(List<String> foodList1) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.printf("드시고 싶으신 메뉴를 입력해주세요 : ");
        String selectedMenu = scanner.nextLine();

        if(!foodList1.contains(selectedMenu)){
            if(!selectedMenu.equals("랜덤")){
                selectedMenu = foodList1.get(Integer.parseInt(selectedMenu)-1);
            }
        }
        scanner.close();
        return selectedMenu;
    }

    @Override
    public void printSelectedMenu(List<String> foodList1, String selectedMenu) {
        
        if(selectedMenu.equals("랜덤")){
            int randomIdx = (int)Math.random() * 10;
            selectedMenu = foodList1.get(randomIdx % foodList1.size());
            System.out.println("인공지능이 추천 하는 점심 : " + selectedMenu);
        }

        String googleSearch = "https://www.google.com/search?q=";

        // 선택된 메뉴 출력w
        System.out.println("구글 링크 : " + googleSearch + "주위+" + selectedMenu + "+맛집");
        try {
			Desktop.getDesktop().browse(new URI(googleSearch + "주위+" + selectedMenu + "+맛집"));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}       
    }
}

App.java (메인 클래스)

package hello;

import java.util.List;

public class App {
    public static void main(String[] args) {

        System.out.println("오점뭐");

        Menu menu = new MenuImpl();

        // 작성된 메뉴 출력
        List<String> fList = menu.printMenu();

        // 사용자의 입력
        String selectedMenu =  menu.select(fList);

        // 입력된 값에 따라 메뉴 출력
        menu.printSelectedMenu(fList, selectedMenu);
    }
}

+ 생성자

package hello;

public class MyClass {
    



}

public class App {
    public static void main(String[] args) {
        MyClass mc = new MyClass();
    }
}

MyClass 안의 코드가 보이면 됨 (생성자)

profile
유승한
post-custom-banner

0개의 댓글