๋ฉ๋ด๋ฅผ ์ ํํ ๋, ์ ์๊ฐ ์๋ ๋ฌธ์์ด์ ์
๋ ฅํ ๊ฒฝ์ฐ InputMismatchException
์ด ๋ฐ์ํ๋ค.
์ฒ์์๋ Scanner
๋ก ์
๋ ฅ ์คํธ๋ฆผ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ ๋ sc.hasNextInt()
๋ฅผ ์ด์ฉํด ๋ค์ ๊ฐ์ด ์ ์๋ก ํด์ ๊ฐ๋ฅํ์ง ํ์ธํด ํด๋น ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.
if (!sc.hasNextInt()) {
System.out.println("์ซ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.");
sc.next();
continue;
}
Lv.6 ํค์ค์คํฌ๋ฅผ ์งํํ ๋๋ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉ์์ ์
๋ ฅ์ ๋ฐ๊ณ ์ ์์ธ์ง ํ๋ณํ๋getUserInput()
๋ฉ์๋๋ก ๋ถ๋ฆฌํ๋ค.
private int getUserInput(Scanner sc, String message) {
System.out.print(message);
String input = sc.nextLine();
if (!input.chars().allMatch(Character::isDigit)) {
throw new IllegalArgumentException("์ซ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.");
}
return Integer.parseInt(input);
}
ํนํ input.chars().allMatch(Character::isDigit)
์ผ๋ก ์์ฑํ ๋ถ๋ถ์ ์
๋ ฅ๊ฐ์ด ์ ์์ธ์ง ํ๋ณํ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ด ์๋์ง ์ฐพ์๋ณด๋ค๊ฐ ๋ฐ๊ฒฌํ ๋ฐฉ๋ฒ์ด๋ค.
input.chars()
: String
์ ๊ฐ ๋ฌธ์๋ฅผ ์ ๋์ฝ๋ ๊ฐ์ผ๋ก ๋ณํํ์ฌ IntStream
(์ซ์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํ ์คํธ๋ฆผ์ ํนํ ๋ฒ์ )์ ๋ฐํํ๋ค.allMatch(Character::isDigit)
: ์คํธ๋ฆผ์ ๋ชจ๋ ์์๊ฐ ์ฃผ์ด์ง ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง ํ์ธํ๋ ์ฐ์ฐ์ด๋ค.Character::isDigit
: ๊ฐ ๋ฌธ์๊ฐ ์ซ์(0~9)์ธ์ง ํ์ธํ๋ ๋ฉ์๋ ์ฐธ์กฐ์ด๋ค.๋ฐ๋ผ์ ํด๋น ๋ฉ์๋๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํด ๋ ๊ฐ๊ฒฐํ๊ฒ ์ฌ์ฉ์ ์ ๋ ฅ๊ฐ์ ๋ฐ๊ณ ๊ฒ์ฆํ ์ ์๋ค.
selectedMenu = getUserInput(sc, "๋ฉ์ธ ๋ฉ๋ด๋ฅผ ์ ํํ์ธ์: ");
๋ฉ๋ด ์ ํ ์ IndexOutOfBoundsException
์ด ๋ฐ์ํ๋ค. ๊ทธ๋๋ง ๊ณ ์น๊ธฐ ์ฌ์ด ์๋ฌ๋ผ ๋คํ์ด์๋ค.
๋ค์ ์ฝ๋์์ menus.get(input)
์ ํ ๋ input
์ ๊ทธ๋๋ก ๋ฃ์ ๊ฒ์ด ๋ฌธ์ ์๋ค. ์ธ๋ฑ์ค๋ 0
๋ถํฐ ์์์ธ๋ฐ ๊ธธ์ด๊ฐ 3๊ฐ์ธ ๋ฆฌ์คํธ์์ 3๋ฒ์งธ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ค๊ณ ํ๋ฉด ์ค๋ฅ๊ฐ ๋๋ ๊ฒ์ ๋น์ฐํ๋ค. menus.get(input - 1)
๋ก ์์ ํด ์๋ฌ๋ฅผ ํด๊ฒฐํ์๋ค.
private void showMenuItems(int input) {
System.out.printf("[ %s MENU ]", menus.get(input).getCategory()); // ์๋ฌ ๋ฐ์
System.out.println();
int size = menus.get(input).getMenuItems().size(); // ์๋ฌ ๋ฐ์
for (int i = 0; i < size; i++) {
MenuItem menuItem = menus.get(input).getMenuItems().get(i); // ์๋ฌ ๋ฐ์
System.out.printf("%d. %s | W %.1f | %s%n", i + 1,
menuItem.getName(), menuItem.getPrice(), menuItem.getDescription());
}
System.out.println("0. ๋ค๋ก๊ฐ๊ธฐ");
}
๋ค์์ ์์ ๋ ์ฝ๋์ด๋ค. ๋ฐฐ์ด์ด๋ ๋ฆฌ์คํธ๋ฅผ ์ฌ์ฉํ ๋๋ ์ธ๋ฑ์ค ๋ฒ์๋ฅผ ์ด๊ณผํ๋ ๊ฒ์ ์ฃผ์ํ์!
private void showMenuItems(int input) {
System.out.printf("[ %s MENU ]", menus.get(input - 1).getCategory());
System.out.println();
int size = menus.get(input - 1).getMenuItems().size();
for (int i = 0; i < size; i++) {
MenuItem menuItem = menus.get(input - 1).getMenuItems().get(i);
System.out.printf("%d. %s | W %.1f | %s%n", i + 1,
menuItem.getName(), menuItem.getPricemenuItem.getDescription());
}
System.out.println("0. ๋ค๋ก๊ฐ๊ธฐ");
}
์ฒ์ ๋ฆฌ์คํธ์ ์ ์ฅํ ๋ฉ๋ด๋ค์ ์ถ๋ ฅํ ๋, ๊ธฐ์กด์๋ ๋ค์๊ณผ ๊ฐ์ด ์ถ๋ ฅํ๋ค. ํ์ง๋ง ์ฝ๋๋ ๊ธธ๊ณ ์ง์ ๋ถํด ๋ณด์ฌ์ ๋ง์์ ๋ค์ง ์์๋ค.
private static void showMenu() {
for (int i = 0; i < menus.size(); i++) {
String name = menus.get(i).getName();
double price = menus.get(i).getPrice();
String description = menus.get(i).getDescription();
System.out.println(i+1 + ". " + name + " | W " + price + " | " + description);
}
System.out.println("0. ์ข
๋ฃ | ์ข
๋ฃ");
}
ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ฐพ์๋ณด๋ค๊ฐ ์์์ง์ ์๋ฅผ ํตํด ๋ ๊น๋ํ๊ฒ ์ถ๋ ฅํ๋๋ก ์์ ํ๋ค. ์์์ง์ ์๋ ์๊ณ ๋ ์์ง๋ง ์ ์ฐ์ง ์์์ ์ธ ๋๋ง๋ค ๊น๋จน๋๋ค.
private static void showMenu() {
for (int i = 0; i < menus.size(); i++) {
MenuItem menu = menus.get(i);
System.out.printf("%d. %s | W %.1f | %s%n",
i + 1, menu.getName(), menu.getPrice(), menu.getDescription());
}
System.out.println("0. ์ข
๋ฃ | ์ข
๋ฃ");
}
โ๏ธ ์์ฃผ ์ฌ์ฉ๋๋ ์์ ์ง์ ์
Lv.6 ํค์ค์คํฌ๋ฅผ ๊ตฌํํ๋ค๊ฐ ๋ฉ์ธ ๋ฉ๋ด์ ๋ฉ๋ด ์์ดํ ๋ก์ง์ ์ฌ๋ฌ ๋ฉ์๋๋ก ๋ถ๋ฆฌํ๋ ์์ ์ ํ๋ค. ์๊ตฌ์ฌํญ์ ์๋ ๋ด์ฉ์ ์๋์ง๋ง, ์ ์ ๊น์ด์ง๋ depth๊ฐ ๋งค์ฐ ๊ฑฐ์ฌ๋ฆฌ๊ณ ๊ฐ๋ ์ฑ๋ ์์ข์ ๊ฒ ๊ฐ์ ์ฌ์ค์ ๊ฐ์์์๋ค..๐ซ
์ด๋ ๊ฒ ๊ธธ๊ณ ๊ธด start()
๋ฉ์๋๋ฅผ ์ข ๋ ๊ฐ๊ฒฐํ๊ฒ ์์ ํ๋ค. if๋ฌธ์ด ์ ์ ๋ง์์ ธ์ ๋ด๊ฐ ์ฝ๋๋ฅผ ์ง๋๊ฑด์ง ์ฝ๋๊ฐ ๋๋ฅผ ์ง๋๊ฑด์ง ๋ด๊ฐ ์งฐ๋๋ฐ๋ ํท๊ฐ๋ฆฌ๋ ์ํฉ์ด ์์ ์ผ๋จ ๋ถ๋ฆฌํด๋ดค๋ค.
public void start() {
try (Scanner sc = new Scanner(System.in)) {
while (true) {
// ๋ฉ๋ด ์ถ๋ ฅ
showMenu();
System.out.print("๋ฉ์ธ ๋ฉ๋ด๋ฅผ ์ ํํ์ธ์: ");
if (!sc.hasNextInt()) {
System.out.println("์ซ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.");
sc.next();
continue;
}
// ๋ฉ๋ด ์
๋ ฅ ๋ฐ๊ธฐ
int selectedMenu = sc.nextInt();
if (selectedMenu == 0) {
System.out.println("ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.");
break;
}
if (isValidMenu(selectedMenu)) {
// ๋ฉ๋ด ์์ดํ
์ถ๋ ฅ
showMenuItems(selectedMenu);
// ๋ฉ๋ด ์์ดํ
์
๋ ฅ ๋ฐ๊ธฐ
System.out.print("๋ฉ๋ด๋ฅผ ์ ํํ์ธ์: ");
int selectedMenuItem = sc.nextInt();
// 0 ์
๋ ฅ ์ ๋ค๋ก๊ฐ๊ธฐ(๋ฉ์ธ ๋ฉ๋ด)
if (selectedMenuItem == 0) continue;
// ์ ํํ ๋ฉ๋ด ์์ดํ
์ถ๋ ฅ
if (isValidMenuItem(selectedMenu, selectedMenuItem)) {
displaySelectedMenuItems(selectedMenu, selectedMenuItem);
} else {
System.out.println("์ฌ๋ฐ๋ฅธ ๋ฉ๋ด ์์ดํ
๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.");
}
} else { // ๋ฉ๋ด์ ์๋ ๊ฐ์ ์
๋ ฅํ ๊ฒฝ์ฐ
System.out.println("์ฌ๋ฐ๋ฅธ ๋ฉ๋ด ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.");
}
System.out.println();
}
}
}
๋ณ๊ฒฝํ start()
๋ฉ์๋๋ ์ด๋ ๋ค. ํฌ๊ฒ kioskInit()
๊ณผ cartInit()
์ผ๋ก ๋๋๋ค.
public void start() {
while (isRunning) {
kioskInit();
if (isExitCondition(selectedMenu)) {
System.out.println("ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.");
isRunning = false;
} else {
cartInit();
}
}
}
kioskInit()
์์๋ ๋ฉ๋ด์ ๋ฉ๋ด ์์ดํ
์ ์ธํ
ํ๋ ๋ฉ์๋์ธ settingMenu()
์ settingMenuItem()
๋ฅผ ์ด์ฉํด ๋ฉ๋ด ๋ฆฌ์คํธ์ ๋ฉ๋ด ์์ดํ
๋ฆฌ์คํธ๋ฅผ ์ถ๋ ฅํ๊ณ , ํด๋น ๊ฐ์ ๊ฐ๊ฐ ์
๋ ฅ๋ฐ๋๋ค. ๋ํ settingMenuItem()
์์๋ ์ ํํ ๋ฉ๋ด๋ฅผ ๋ณด์ฌ์ฃผ๋ displaySelectedMenuItems()
๋ ํฌํจ๋์ด ์๋ค.
private void kioskInit() {
settingMenu();
if (selectedMenu == 0) return;
settingMenuItem();
}
private void settingMenuItem() {
showMenuItems(selectedMenu);
getMenuItem();
displaySelectedMenuItems(selectedMenu, selectedMenuItem);
}
private void getMenuItem() {
while (true) {
try {
selectedMenuItem = getUserInput(sc, "๋ฉ๋ด๋ฅผ ์ ํํ์ธ์: ");
if (selectedMenuItem == 0) return;
else if (!isValidMenuItem(selectedMenu, selectedMenuItem)) {
System.out.println("์ฌ๋ฐ๋ฅธ ๋ฉ๋ด ์์ดํ
๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.");
return;
}
break;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
private void displaySelectedMenuItems(int i1, int i2) {
MenuItem menuItem = menus.get(i1 - 1).getMenuItems().get(i2 - 1);
System.out.printf("์ ํํ ๋ฉ๋ด: %s | W %.1f | %s%n",
menuItem.getName(),
menuItem.getPrice(),
menuItem.getDescription());
}
private void settingMenu() {
showMenu();
getMenu();
}
private void getMenu() {
while (true) {
try {
selectedMenu = getUserInput(sc, "๋ฉ์ธ ๋ฉ๋ด๋ฅผ ์ ํํ์ธ์: ");
if (selectedMenu == 0) return;
if (!isValidMenu(selectedMenu)) {
System.out.println("์ฌ๋ฐ๋ฅธ ๋ฉ๋ด ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.");
continue;
}
break;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
cartInit()
๋ฉ์๋๋ ์ฅ๋ฐ๊ตฌ๋์ ์ํ์ ์ถ๊ฐํ๊ณ ์ถ๋ ฅํ๋settingCart()
์ ์ฅ๋ฐ๊ตฌ๋์ ์ํ์ด ์์ ๊ฒฝ์ฐ ๋ฉ๋ด๋ฅผ ์ ํํ๊ณ ์ฃผ๋ฌธํ๋ handleCartWithItems()
๋ฅผ ํฌํจํ๋ค.
private void cartInit() {
settingCart();
handleCartWithItems();
}
settingCart()
๋ ์ฅ๋ฐ๊ตฌ๋ ์ถ๊ฐ ์ฌ๋ถ์ ๋ฐ๋ผ ๊ฒฐ๊ณผ๊ฐ ๋ฌ๋ผ์ง๋ค.
private void settingCart() {
while (true) {
try {
displaySelectedOrderMenu(selectedMenu, selectedMenuItem);
int cartSelection = getUserInput(sc,
"์ ๋ฉ๋ด๋ฅผ ์ฅ๋ฐ๊ตฌ๋์ ์ถ๊ฐํ์๊ฒ ์ต๋๊น?\n1. ํ์ธ\t2. ์ทจ์\n");
if (cartSelection == 2) return;
// ์ฅ๋ฐ๊ตฌ๋์ ์ํ ์ถ๊ฐ ํ ์ถ๊ฐ๋ ์ํ ์ ๋ณด ์ถ๋ ฅ
cart.add(menus.get(selectedMenu-1), selectedMenuItem);
cart.showAddedItem();
cart.showAllCartItems();
break;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
handleCartWithItems()
๋ ๋ฉ์ธ ๋ฉ๋ด์ ์ฃผ๋ฌธ ๋ฉ๋ด๋ฅผ ๋ณด์ฌ์ฃผ๊ณ , ์
๋ ฅ์ ๋ฐ์ ์ฒ๋ฆฌํ๋ค.
private void handleCartWithItems() {
System.out.println("์๋ ๋ฉ๋ดํ์ ๋ณด์๊ณ ๋ฉ๋ด๋ฅผ ๊ณจ๋ผ ์
๋ ฅํด์ฃผ์ธ์.");
showMenu();
showOrderMenu();
getOrder();
}
getOrder()
๋ก ๋ค์ด๊ฐ๋ฉด ์
๋ ฅ๊ฐ์ ๋ฐ๋ผ ์ข
๋ฃ ํน์ ๋ฉ์ธ ๋ฉ๋ด๋ก ๋๋์๊ฐ๊ฑฐ๋ ์ฃผ๋ฌธ์ ์งํํ๊ฒ ๋๋ค.
private void getOrder() {
while (true) {
try {
selectedOrderMenu = getUserInput(sc, "");
if (isExitCondition(selectedOrderMenu)) {
handleExit();
return;
}
if (isValidMainMenu(selectedOrderMenu)) {
handleMainMenuSelection(selectedOrderMenu);
break;
}
handleOrder();
break;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
๋ฉ์๋๋ฅผ ๊ฐ ๊ธฐ๋ฅ์ ๋ฐ๋ผ ๋๋ ๋ณด์๋๋ฐ, ๋ฉ์๋ depth๊ฐ ๋๋ฌด ๊น์ด์ง ๊ฒ ๊ฐ๊ธฐ๋ ํ๋ค. ํํฐ๋์ ์ฝ๋๋ฅผ ๋ณด๋ฉฐ ์ด๋ป๊ฒ ๋๋๋ ๊ฒ์ด ํจ์จ์ ์ผ์ง ๋ค์ ํ ๋ฒ ๊ณ ๋ฏผํด๋ด์ผํ ๊ฒ ๊ฐ๋ค.