๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
TIL

01/09(ํ™”) ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„ Java 12์ผ์ฐจ TIL - ํ‚ค์˜ค์Šคํฌ ๋งŒ๋“ค๊ธฐ

by ๊ฐ์ž๊ฐœ๋ฐœ๊พผ 2024. 1. 9.

๐Ÿ“–  ์˜ค๋Š˜์˜ ํ•™์Šต ํ‚ค์›Œ๋“œ

  • ํ‚ค์˜ค์Šคํฌ

 

๐Ÿฅ”  KIOSK

package try1;

import java.util.ArrayList;
import java.util.Scanner;


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

        Scanner scanner = new Scanner(System.in);

        String menuTitle = "\"SHAKESHACK BURGER ์— ์˜ค์‹ ๊ฑธ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.\"\n" +
                "์•„๋ž˜ ๋ฉ”๋‰ดํŒ์„ ๋ณด์‹œ๊ณ  ๋ฉ”๋‰ด๋ฅผ ๊ณจ๋ผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.\n";

        ArrayList<String> Menu = new ArrayList<>();
        Menu.add("Burgers         | ์•ต๊ฑฐ์Šค ๋น„ํ”„ ํ†ต์‚ด์„ ๋‹ค์ ธ๋งŒ๋“  ๋ฒ„๊ฑฐ");
        Menu.add("Drinks          | ๋งค์žฅ์—์„œ ์ง์ ‘ ๋งŒ๋“œ๋Š” ์Œ๋ฃŒ");
        Menu.add("Beer            | ๋‰ด์š• ๋ธŒ๋ฃจํด๋ฆฐ ๋ธŒ๋ฃจ์–ด๋ฆฌ์—์„œ ์–‘์กฐํ•œ ๋งฅ์ฃผ");

        ArrayList<String> burgerMenu = new ArrayList<>();
        burgerMenu.add("ShackBurger   | W 6.9 | ํ† ๋งˆํ† , ์–‘์ƒ์ถ”, ์‰‘์†Œ์Šค๊ฐ€ ํ† ํ•‘๋œ ์น˜์ฆˆ๋ฒ„๊ฑฐ");
        burgerMenu.add("Cheeseburger  | W 6.9 | ํฌํ…Œ์ดํ†  ๋ฒˆ๊ณผ ๋น„ํ”„ํŒจํ‹ฐ, ์น˜์ฆˆ๊ฐ€ ํ† ํ•‘๋œ ์น˜์ฆˆ๋ฒ„๊ฑฐ");
        burgerMenu.add("Hamburger     | W 5.4 | ๋น„ํ”„ํŒจํ‹ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์•ผ์ฑ„๊ฐ€ ๋“ค์–ด๊ฐ„ ๊ธฐ๋ณธ๋ฒ„๊ฑฐ");

        ArrayList<String> drinkMenu = new ArrayList<>();
        drinkMenu.add("CoKe          | W 3.5 | ๋“œ๋Ÿฝ๊ฒŒ ๋น„์‹ผ ์ฝœ๋ผ");
        drinkMenu.add("LemonAde      | W 4.5 | ๋“œ๋Ÿฝ๊ฒŒ ๋น„์‹ผ ๋ ˆ๋ชฌ์—์ด๋“œ");
        drinkMenu.add("Shake         | W 6.5 | ๊ฐํŠ€์— ์ฐ์–ด๋จน๋Š” ๋ฐ€ํฌ์‰์ดํฌ");

        ArrayList<String> beerMenu = new ArrayList<>();
        beerMenu.add("Beer          | W 5.0 | ํ–„๋ฒ„๊ฑฐ์— ๋งฅ์ฃผ?");
        beerMenu.add("Cass          | W 4.5 | ํ–„๋ฒ„๊ฑฐ์— ๋งฅ์ฃผ?");
        beerMenu.add("Tera          | W 4.5 | ํ–„๋ฒ„๊ฑฐ์— ๋งฅ์ฃผ?");

        ArrayList<String> basket = new ArrayList<>();

        int WaitNum = 1;


        while (true) {
            System.out.println(menuTitle);

            System.out.println("[ SHACKSHACK MENU ]");
            for (int i = 0; i < Menu.size(); i++) {
                System.out.println((i + 1) + ". " + Menu.get(i));
            }

            System.out.println();
            System.out.println("[ ORDER MENU ]");
            System.out.println("4. Order       | ์žฅ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ํ™•์ธ ํ›„ ์ฃผ๋ฌธํ•ฉ๋‹ˆ๋‹ค.");
            System.out.println("5. Cancel      | ์ง„ํ–‰์ค‘์ธ ์ฃผ๋ฌธ์„ ์ทจ์†Œํ•ฉ๋‹ˆ๋‹ค.");
            System.out.println("6. Exit        | ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.\n");

            System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•˜์„ธ์š”: ");
            int menuChoice = scanner.nextInt();
            scanner.nextLine();
            System.out.println();

            if (menuChoice == 1) { // ํ–„๋ฒ„๊ฑฐ ์„ ํƒ
                System.out.println(menuTitle);
                System.out.println("[ Burgers MENU ]");
                for (int i = 0; i < burgerMenu.size(); i++) {
                    System.out.println((i + 1) + ". " + burgerMenu.get(i));
                }
                System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”: ");
                int burgerChoice = scanner.nextInt();
                System.out.println();

                if (burgerChoice == 1) { // ํ–„๋ฒ„๊ฑฐ ์ข…๋ฅ˜ ์„ ํƒ
                    System.out.println("\"" + burgerMenu.get(0) + "\"");
                    System.out.println("์œ„ ๋ฉ”๋‰ด๋ฅผ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
                    System.out.println("1. ํ™•์ธ        2. ์ทจ์†Œ");

                    System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”: ");
                    int martChoice = scanner.nextInt();
                    System.out.println();

                    if (martChoice == 1) {
                        System.out.println("ShackBurger ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                        basket.add(burgerMenu.get(0));
                        System.out.println();
                    }


                } else if (burgerChoice == 2) {
                    System.out.println("\"" + burgerMenu.get(1) + "\"");
                    System.out.println("์œ„ ๋ฉ”๋‰ด๋ฅผ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
                    System.out.println("1. ํ™•์ธ        2. ์ทจ์†Œ");

                    System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”: ");
                    int martChoice = scanner.nextInt();
                    System.out.println();

                    if (martChoice == 1) {
                        System.out.println("Cheeseburger ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                        basket.add(burgerMenu.get(1));
                        System.out.println();
                    }


                } else if (burgerChoice == 3) {
                    System.out.println("\"" + burgerMenu.get(2) + "\"");
                    System.out.println("์œ„ ๋ฉ”๋‰ด๋ฅผ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
                    System.out.println("1. ํ™•์ธ        2. ์ทจ์†Œ");

                    System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”: ");
                    int martChoice = scanner.nextInt();
                    System.out.println();

                    if (martChoice == 1) {
                        System.out.println("Hamburger ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                        basket.add(burgerMenu.get(2));
                        System.out.println();
                    }
                }
            }
            if (menuChoice == 2) { // ์Œ๋ฃŒ์ˆ˜ ์„ ํƒ
                System.out.println(menuTitle);
                System.out.println("[ Drinks MENU ]");
                for (int i = 0; i < drinkMenu.size(); i++) {
                    System.out.println((i + 1) + ". " + drinkMenu.get(i));
                }
                System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”: ");
                int drinkChoice = scanner.nextInt();
                System.out.println();

                if (drinkChoice == 1) { // ์Œ๋ฃŒ์ˆ˜ ์ข…๋ฅ˜ ์„ ํƒ
                    System.out.println("\"" + drinkMenu.get(0) + "\"");
                    System.out.println("์œ„ ๋ฉ”๋‰ด๋ฅผ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
                    System.out.println("1. ํ™•์ธ        2. ์ทจ์†Œ");

                    System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”: ");
                    int martChoice = scanner.nextInt();
                    System.out.println();

                    if (martChoice == 1) {
                        System.out.println("CoKe ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                        basket.add(drinkMenu.get(0));
                        System.out.println();
                    }


                } else if (drinkChoice == 2) {
                    System.out.println("\"" + drinkMenu.get(1) + "\"");
                    System.out.println("์œ„ ๋ฉ”๋‰ด๋ฅผ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
                    System.out.println("1. ํ™•์ธ        2. ์ทจ์†Œ");

                    System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”: ");
                    int martChoice = scanner.nextInt();
                    System.out.println();

                    if (martChoice == 1) {
                        System.out.println("LemonAde ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                        basket.add(drinkMenu.get(1));
                        System.out.println();
                    }


                } else if (drinkChoice == 3) {
                    System.out.println("\"" + drinkMenu.get(2) + "\"");
                    System.out.println("์œ„ ๋ฉ”๋‰ด๋ฅผ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
                    System.out.println("1. ํ™•์ธ        2. ์ทจ์†Œ");

                    System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”: ");
                    int martChoice = scanner.nextInt();
                    System.out.println();

                    if (martChoice == 1) {
                        System.out.println("Shake ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                        basket.add(drinkMenu.get(2));
                        System.out.println();
                    }
                }
            }

            if (menuChoice == 3) { // ์ฃผ๋ฅ˜ ์„ ํƒ
                System.out.println(menuTitle);
                System.out.println("[ Beers MENU ]");
                for (int i = 0; i < beerMenu.size(); i++) {
                    System.out.println((i + 1) + ". " + beerMenu.get(i));
                }
                System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”: ");
                int beerChoice = scanner.nextInt();
                System.out.println();

                if (beerChoice == 1) { // ์ฃผ๋ฅ˜ ์ข…๋ฅ˜ ์„ ํƒ
                    System.out.println("\"" + beerMenu.get(0) + "\"");
                    System.out.println("์œ„ ๋ฉ”๋‰ด๋ฅผ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
                    System.out.println("1. ํ™•์ธ        2. ์ทจ์†Œ");

                    System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”: ");
                    int martChoice = scanner.nextInt();
                    System.out.println();

                    if (martChoice == 1) {
                        System.out.println("SoJu ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                        basket.add(beerMenu.get(0));
                        System.out.println();
                    }

                } else if (beerChoice == 2) {
                    System.out.println("\"" + beerMenu.get(1) + "\"");
                    System.out.println("์œ„ ๋ฉ”๋‰ด๋ฅผ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
                    System.out.println("1. ํ™•์ธ        2. ์ทจ์†Œ");

                    System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”: ");
                    int martChoice = scanner.nextInt();
                    System.out.println();

                    if (martChoice == 1) {
                        System.out.println("Cass ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                        basket.add(beerMenu.get(1));
                        System.out.println();
                    }

                } else if (beerChoice == 3) {
                    System.out.println("\"" + beerMenu.get(2) + "\"");
                    System.out.println("์œ„ ๋ฉ”๋‰ด๋ฅผ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
                    System.out.println("1. ํ™•์ธ        2. ์ทจ์†Œ");

                    System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”: ");
                    int martChoice = scanner.nextInt();
                    System.out.println();

                    if (martChoice == 1) {
                        System.out.println("Tera ๊ฐ€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                        basket.add(beerMenu.get(2));
                        System.out.println();
                    }
                }
            }

            if (menuChoice == 4) {
                System.out.println("์•„๋ž˜์™€ ๊ฐ™์ด ์ฃผ๋ฌธ ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?\n");
                System.out.println("[ Orders ]");
                for (String a : basket) {
                    System.out.println(a);
                }
                System.out.println();

                System.out.println("[ Total ]");
                double total = 0;
                for (int i = 0; i < basket.size(); i++) {
                    String cash = basket.get(i).substring(18, 21);
                    Double money = Double.parseDouble(cash);
                    total += money;
                }
                System.out.println("W " + total);
                System.out.println();
                System.out.println("1. ์ฃผ๋ฌธ     2. ๋ฉ”๋‰ดํŒ");

                System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•˜์„ธ์š”: ");
                int orderChoice = scanner.nextInt();
                scanner.nextLine();

                if (orderChoice == 1) {
                    System.out.println("์ฃผ๋ฌธ์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!\n");
                    System.out.println("๋Œ€๊ธฐ๋ฒˆํ˜ธ๋Š” [ " + WaitNum + " ] ๋ฒˆ ์ž…๋‹ˆ๋‹ค");
                    System.out.println("(3์ดˆํ›„ ๋ฉ”๋‰ดํŒ์œผ๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค.)\n");
                    WaitNum++;
                    basket.clear();
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

            if (menuChoice == 5) {
                System.out.println("์ง„ํ–‰ํ•˜๋˜ ์ฃผ๋ฌธ์„ ์ทจ์†Œํ•˜๊ธฐ์…Œ์Šต๋‹ˆ๊นŒ?");
                System.out.println("1. ํ™•์ธ      2. ์ทจ์†Œ");

                System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”: ");
                int cancelChoice = scanner.nextInt();
                if (cancelChoice == 1) {
                    basket.clear();
                    System.out.println("์ง„ํ–‰ํ•˜๋˜ ์ฃผ๋ฌธ์ด ์ทจ์†Œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                }
            }
            if (menuChoice == 6) {
                System.out.println("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
                System.out.println("1. ํ™•์ธ      2. ์ทจ์†Œ\n");

                System.out.print("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”: ");
                int exitChoice = scanner.nextInt();
                if (exitChoice == 1) {
                    break;
                }
            }
        }
    }
}

 

 

 

๐Ÿ“š  ์˜ค๋Š˜์˜ ํšŒ๊ณ 

ํด๋ž˜์Šค๋ฅผ ๋ฐฐ์›Œ์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์— ์žˆ์–ด์„œ๋„ ์„ค๋ ์„ค๋  ์“ธ ์ˆ˜ ์žˆ์„ ์ค„ ์•Œ์•˜์ง€๋งŒ,

์ƒ๊ฐ๋ณด๋‹จ ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์–ด๋–ค ์‹์œผ๋กœ ๋‚˜๋ˆ ์•ผ ํ• ์ง€ ๊ฐ์ด ์•ˆ ์žกํ˜€์„œ ์ผ๋‹จ ๊ตฌํ˜„์ด๋ผ๋„ ํ•  ์ˆ˜ ์žˆ์„์ง€ ๋งŒ๋“ค์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๋ฉ”์ธ์— ๋•Œ๋ ค ๋„ฃ์€ ์ฝ”๋“œ.. ๋ฉ”๋•Œ์ฝ”

300์ค„ ์ •๋„ ๋‚˜์™”๋„ค์š”..

 

๋ฐฐ์—ด์— ๋“ค์–ด๊ฐ€๋Š” ๋ชจ๋“  ๊ฒƒ๋“ค์„ String ํ˜•์‹์œผ๋กœ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—

์ด๊ธˆ์•ก์„ ๊ตฌํ•˜๋Š” ๊ฒŒ ์‰ฝ์ง€ ์•Š์•„์„œ ์–ด๋–ค ์‹์œผ๋กœ ๋นผ์„œ ๋”ํ• ์ง€ ๊ณ ๋ฏผํ•˜๋˜ ์™€์ค‘

๊ทธ ๋ถ€๋ถ„์˜ ๋ฌธ์ž์—ด์„ ๋นผ๋‚ด์„œ, ๋นผ๋‚ธ ๋ฌธ์ž์—ด์„ ๋”๋ธ”๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์“ฐ๋ฉด ๋˜๊ฒ ๋‹ค ์ƒ๊ฐ์ด ๋“ค์–ด์„œ

substring์œผ๋กœ ๋นผ๋‚ด๊ณ  Double.parseDouble๋กœ ๊ฐ•์ œ ํ˜•๋ณ€ํ™˜์„ ์‹œ์ผœ์„œ ์ด๊ธˆ์•ก์„ ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค.

 

์ด์ œ ํด๋ž˜์Šค๋กœ ๋‚˜๋ˆ„๋Š” ๊ณผ์ •์„ ํ†ตํ•ด์„œ ํด๋ž˜์Šค๋ฅผ ๋” ์ดํ•ดํ•˜๊ณ  ํ™œ์šฉํ•˜๋ฉด์„œ

์„ฑ์žฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.