Skip to content

1203 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/java/menu/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package menu;

import menu.controller.PersonController;
import menu.service.MenuService;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
MenuService saveMenuService = new MenuService();

PersonController personController = new PersonController(saveMenuService);
personController.run();
}
}
70 changes: 70 additions & 0 deletions src/main/java/menu/controller/PersonController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package menu.controller;

import menu.model.Menu;
import menu.model.Menus;
import menu.model.People;
import menu.service.MenuService;
import menu.view.InputView;
import menu.view.OutputView;

import java.util.ArrayList;
import java.util.List;

public class PersonController {
private final MenuService menuService;


public PersonController(MenuService saveMenuService) {
this.menuService = saveMenuService;
}

public void run() {
// 한식 : 비빔밥, 김밥, ...
Menus menus = saveMenu();
// 감독 이름 : 신홍규, 김민수, ...
People people = getCoachNames();

makeCategory(menus, people);
}

public Menus saveMenu() {
List<Menu> menusInput = new ArrayList<>();
Menus menus = menuService.saveMenu(menusInput);
// OutputView.printMenu(menus);
return menus;
}

public People getCoachNames() {
String coachNames = InputView.getCoachNames();
String[] manyCoachNames = coachNames.split(",");

List<String[]> cantFoods = InputView.getCantFood(manyCoachNames);
People people = menuService.saveCantFoods(manyCoachNames, cantFoods);
// OutputView.printPeople(people);
return people;
}

public void makeCategory(Menus menus, People people) {
while (true) {
// 5일 식사 카테고리(한식, 일식 ...)
List<String> category = menuService.makeCategory();
boolean checkRepeat = menuService.checkCategory(category);
if(checkRepeat) {
// true : 검증 통과
People peopleForAnswer = menuService.makeMenu(menus, category, people);
boolean checkHateMenu = menuService.checkHateMenu(peopleForAnswer, people);
boolean checkMenuRepeat = menuService.checkMenuRepeat(peopleForAnswer);
// OutputView.printPeople(peopleForAnswer);

if(checkHateMenu && checkMenuRepeat) {
OutputView.result(peopleForAnswer, category);
break;
}

}
break;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 되면 랜덤뽑기한 카테고리가 요구사항에 맞지 않으면 (= !checkRepeat면) 카테고리를 다시 뽑는 게 아니라 그냥 메서드가 끝나버리는 것 같은데 맞나유?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

먼가 continue가 맞을 것 같은데.. 확실하진 않네요 한번 확인 부탁드립니당

}


}
}
19 changes: 19 additions & 0 deletions src/main/java/menu/model/Menu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package menu.model;

public class Menu {
private final String menuCountry;
private final String[] menuNames;

public Menu(String menuCountry, String[] menuNames) {
this.menuCountry = menuCountry;
this.menuNames = menuNames;
}

public String getMenuCountry() {
return menuCountry;
}

public String[] getMenuNames() {
return menuNames;
}
}
29 changes: 29 additions & 0 deletions src/main/java/menu/model/Menus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package menu.model;

import java.util.List;

public class Menus {

private final List<Menu> menus;

public Menus(List<Menu> menus) {
this.menus = menus;
}

public void saveMenu(Menu menu) {
menus.add(menu);
}

public List<Menu> getMenus() {
return menus;
}

public Menu getSameMenu(String menuName) {
for (Menu menu : menus) {
if (menu.getMenuCountry().equals(menuName)) {
return menu;
}
}
return null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

만약 개발 중에 실수로 메뉴명이 잘못 들어가면 에러 대신 null이 반환되고, nullpointer 문제가 생기기 때문에 이상황에선 대신에 명확한 에러사유와 함께 에러를 날려줘도 좋을 것 같아요!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 그렇군요
반복문 흐름 제어까지 꼼꼼하게 봐주셔서 감사합니다

}
}
20 changes: 20 additions & 0 deletions src/main/java/menu/model/People.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package menu.model;

import java.util.List;
import java.util.Map;

public class People {
private final List<Person> people;

public People(List<Person> people) {
this.people = people;
}

public void savePerson(Person person) {
people.add(person);
}

public List<Person> getPeople() {
return people;
}
}
20 changes: 20 additions & 0 deletions src/main/java/menu/model/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package menu.model;

public class Person {
String name;
String[] cantMenu;

public Person(String name, String[] cantMenu) {
this.name = name;
this.cantMenu = cantMenu;
}

public String getPersonName() {
return name;
}

public String[] getCantFoods() {
return cantMenu;
}

}
174 changes: 174 additions & 0 deletions src/main/java/menu/service/MenuService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package menu.service;

import camp.nextstep.edu.missionutils.Randoms;
import menu.model.Menu;
import menu.model.Menus;
import menu.model.People;
import menu.model.Person;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;


public class MenuService {

public Menus saveMenu(List<Menu> menusInput) {
String[] menuCountries = {"일식", "한식", "중식", "아시안", "양식"};
String[][] menuNames = {
{"규동", "우동", "미소시루", "스시", "가츠동", "오니기리", "하이라이스", "라멘", "오코노미야끼"},
{"김밥", "김치찌개", "쌈밥", "된장찌개", "비빔밥", "칼국수", "불고기", "떡볶이", "제육볶음"},
{"깐풍기", "볶음면", "동파육", "짜장면", "짬뽕", "마파두부", "탕수육", "토마토 달걀볶음", "고추잡채"},
{"팟타이", "카오 팟", "나시고렝", "파인애플 볶음밥", "쌀국수", "똠얌꿍", "반미", "월남쌈", "분짜"},
{"라자냐", "그라탱", "뇨끼", "끼슈", "프렌치 토스트", "바게트", "스파게티", "피자", "파니니"}
};
Menus menus = new Menus(menusInput);

for (int i = 0; i < 5; i++) {
String menuCountry = menuCountries[i];
Menu menu = new Menu(menuCountry, menuNames[i]);
menus.saveMenu(menu);
}
return menus;
}

public List<String> makeCategory02() {
List<String> candidate = Arrays.asList("일식", "한식", "중식", "아시안", "양식");
List<String> category = new ArrayList<>();
for (int i = 0; i < 5; i++) {
String firstCategory = Randoms.shuffle(candidate).get(0);
category.add(firstCategory);
}
return category;
}

public List<String> makeCategory() {
List<String> candidate = Arrays.asList("일식", "한식", "중식", "아시안", "양식");

candidate.add(0,"");

List<String> category = new ArrayList<>();
for (int i = 0; i < 5; i++) {
String firstCategory = candidate.get(Randoms.pickNumberInRange(1,5));
category.add(firstCategory);
}
return category;
}

public boolean checkCategory(List<String> category) {
for(String name : category) {
}
for (String categoryItem : category) {
if (Collections.frequency(category, categoryItem) >= 3) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

우왕 이런 기능도 있군요 유용하네요 배워갑니다

return false;
}
}
return true;
}

public People saveCantFoods(String[] coachNames, List<String[]> cantFoods) {
List<Person> person = new ArrayList<>();

People people = new People(person);

for(int i=0; i<coachNames.length; i++) {
people.savePerson(new Person(coachNames[i], cantFoods.get(i)));
}
return people;
}

public People makeMenu(Menus menus, List<String> category, People people) {
// menus : 각 나라에 대한 음식 저장
// category : 5일간의 식사 카테고리
// people : 감독 이름과 싫어하는 음식 저장

List<Person> peopleForAnswer = new ArrayList<>();

for (Person person : people.getPeople()) {
String coachName = person.getPersonName();
String[] eatFoods = new String[5];
Person personForAnswer = chooseEatMenu(menus, category, person);
// 사람 이름 : 메뉴 5개 목록을 반환받음
peopleForAnswer.add(personForAnswer);
}
People temp = new People(peopleForAnswer);


return temp;

}

private Person chooseEatMenu(Menus menus, List<String> category, Person person) {
// 전체 메뉴 저장된 menus
// 5일 카테고리인 category
// 감독 이름과 싫어하는 음식 저장된 person
// 반환을 위해 객체 생성
List<String> eatFoods = new ArrayList<>();
String name = person.getPersonName();
for (String categoryMenu : category) {
Menu sameMenu = menus.getSameMenu(categoryMenu);
String[] sameMenus = sameMenu.getMenuNames();
// 그 요일에 맞는 요리 목록 가져옴
String menu = Randoms.shuffle(Arrays.asList(sameMenus)).get(0);
eatFoods.add(menu);
}

return new Person(name, eatFoods.toArray(new String[0]));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존에 있던 People객체에 정보를 추가하기보단 새로운 People 객체를 만드신 것 같은데 이유가 뭔가욤?
지금 Person의 필드가 cantFood인데 여기에 추천받은 메뉴를 저장하는 건 잘못된 저장 아닐까 싶어서요!
저라면 setter를 사용해서 추천메뉴 정보를 추가하거나, 아예 다른 객체를 만들어서 정보를 저장할 것 같은데 혹시 어떻게 생각하시나용


}

public boolean checkHateMenu(People peopleForAnswer, People people) {
// people에서 싫어하는 음식 꺼내서 ForAnswer와 대조하기
// 선택한 음식 5개 중에서 싫어하는 음식 확인
for (int i = 0; i < people.getPeople().size(); i++) {
return checkFood(peopleForAnswer.getPeople().get(i), people.getPeople().get(i));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 이해하기론 이렇게 되면 int i = 0일때 checkFood를 연산하고 메서드가 끝나버리지 않나요??
return문을 넣어버리니까 i=0일때 return되고 i=1로 넘어가지 않을 것 같은데... 괜찮던가유?

}
return false;
}

private boolean checkFood(Person person, Person person1) {
String[] candidateFoods = person.getCantFoods();
String[] cantFoods = person1.getCantFoods();
for (String candidateFood : candidateFoods) {
for (String cantFood : cantFoods) {
if (candidateFood.equals(cantFood)) {
return false;
}
}
}
return true;
}

private boolean checkHateFood(Person person, Person person1) {
String[] eatFoods = person.getCantFoods();
String[] cantFoods = person1.getCantFoods();
for (String eatFood : eatFoods) {
for (String cantFood : cantFoods) {
if (eatFood.equals(cantFood)) {
return false;
}
}
}
return true;
}

public boolean checkMenuRepeat(People peopleForAnswer) {


for (Person person : peopleForAnswer.getPeople()) {

List<String> eatFoods = new ArrayList<>();


String[] eatFood = person.getCantFoods();
for (String s : eatFood) {
if (eatFoods.contains(s)) {
return false;
}
eatFoods.add(s);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

중복 확인 용이라면
if (new Hashset<>(List.of(person.getCantFoods())) != person.getCantFoods().length) {
return false;
}
와 같은 방법도 간단할 것 같습니다!

}
}
return true;
}
}
27 changes: 27 additions & 0 deletions src/main/java/menu/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package menu.view;

import java.io.Console;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class InputView {
public static String getCoachNames() {
System.out.println("점심 메뉴 추천을 시작합니다.\n" +
"코치의 이름을 입력해 주세요. (, 로 구분)");
Scanner scanner = new Scanner(System.in);
String coachNames = scanner.nextLine();
return coachNames;
}

public static List<String[]> getCantFood(String[] coachNames) {
List<String[]> cantFoods = new ArrayList<>();
for (String coachName : coachNames) {
System.out.println(coachName + "(이)가 못 먹는 메뉴를 입력해 주세요.");
Scanner scanner = new Scanner(System.in);
String cantFood = scanner.nextLine();
cantFoods.add(cantFood.split(","));
}
return cantFoods;
}
}
Loading