꽃 자판기만들기 java

김창현·2023년 3월 24일
0
import java.util.Scanner;

public class VendingMachineRun {
    public static void main(String[] args) {
        do {
            try {
                FakeFlower fakeFlower = new FakeFlower();
                fakeFlower.createFakeFlower();

                VendingMachine vendingMachine = new VendingMachine();
                vendingMachine.requestUserInput(fakeFlower.getProductName(1), fakeFlower.getProductName(2),
                        fakeFlower.getProductName(3), fakeFlower.getProductName(4));

                int buttonValue = vendingMachine.userInput();

                if (!fakeFlower.getIsInStock(buttonValue)) {
                    vendingMachine.printOutOfStock();
                    continue;
                }

                vendingMachine.printProductInfo(fakeFlower.getProductName(buttonValue),
                        fakeFlower.getProductPrice(buttonValue));

                int buyOrNot = vendingMachine.userInput();

                if (vendingMachine.askBuyProduct(buyOrNot)) {
                    vendingMachine.printRequestMoney();

                    int money = vendingMachine.userInput();

                    if (vendingMachine.isMoneyEnough(fakeFlower, buttonValue, money)) {
                        vendingMachine.printSuccess(fakeFlower, buttonValue, money);
                        vendingMachine.setOutOfStock(buttonValue);
                    }
                    if (!vendingMachine.isMoneyEnough(fakeFlower, buttonValue, money)) {
                        vendingMachine.printFail();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } while (true);
    }
}


class FakeFlower {
    public static boolean isInStock1 = true, isInStock2 = true, isInStock3 = true, isInStock4 = true;
    private String productName1, productName2, productName3, productName4;
    private int productPrice1, productPrice2, productPrice3, productPrice4;

    public void createFakeFlower() {
        productName1 = "장미";
        productName2 = "카네이션";
        productName3 = "무궁화";
        productName4 = "국화";

        productPrice1 = 10000;
        productPrice2 = 20000;
        productPrice3 = 30000;
        productPrice4 = 40000;
    }

    public boolean getIsInStock(int buttonValue) {
        return switch (buttonValue) {
            case 1 -> isInStock1;
            case 2 -> isInStock2;
            case 3 -> isInStock3;
            case 4 -> isInStock4;
            default -> throw new IllegalStateException("Unexpected value: " + buttonValue);
        };
    }

    public String getProductName(int buttonValue) {
        return switch (buttonValue) {
            case 1 -> productName1;
            case 2 -> productName2;
            case 3 -> productName3;
            case 4 -> productName4;
            default -> throw new IllegalStateException("Unexpected value: " + buttonValue);
        };
    }

    public int getProductPrice(int buttonValue) {
        return switch (buttonValue) {
            case 1 -> productPrice1;
            case 2 -> productPrice2;
            case 3 -> productPrice3;
            case 4 -> productPrice4;
            default -> throw new IllegalStateException("Unexpected value: " + buttonValue);
        };
    }
}

class VendingMachine {
    public int userInput() {
        Scanner sc = new Scanner(System.in);
        return sc.nextInt();
    }

    public void setOutOfStock(int buttonValue) {
        if (buttonValue == 1) FakeFlower.isInStock1 = false;
        if (buttonValue == 2) FakeFlower.isInStock2 = false;
        if (buttonValue == 3) FakeFlower.isInStock3 = false;
        if (buttonValue == 4) FakeFlower.isInStock4 = false;
    }

    public boolean isMoneyEnough(FakeFlower fakeFlower, int buttonValue, int money) {
        return money >= fakeFlower.getProductPrice(buttonValue);
    }

    public boolean askBuyProduct(int userInput) {
        return userInput == 1;
    }

    public void requestUserInput(String productName1, String productName2, String productName3, String productName4) {
        System.out.println("************************************************************************");
        System.out.printf("[1번 : %s], [2번 : %s], [3번 : %s], [4번 : %s] 중 제품 번호를 입력해주세요. \n"
                            , productName1, productName2, productName3, productName4);
        System.out.println("************************************************************************");
    }

    public void printProductInfo(String productName, int productPrice) {
        System.out.printf("선택하신 상품은 [%s], 가격은 [%d]입니다.\n", productName, productPrice);
        System.out.println("결제하시겠습니까?");
        System.out.println("[예 : 1], [아니요 : 2]");
    }

    public void printOutOfStock() {
        System.out.println("상품의 재고가 없습니다. 다시 선택해 주세요");
    }

    public void printRequestMoney() {
        System.out.println("금액을 투입해주세요.");
    }

    public void printSuccess(FakeFlower fakeFlower, int buttonValue, int money) {
        System.out.println("투입한 금액은 " + money + "입니다.");
        System.out.println("주문하신 " + fakeFlower.getProductName(buttonValue) + " 나왔습니다.");
        System.out.println("반환된 잔돈은 " + (money - fakeFlower.getProductPrice(buttonValue)) + "입니다.");
    }

    public void printFail() {
        System.out.println("금액이 부족합니다.");
        System.out.println("투입하신 금액을 반환합니다.");
    }
}
profile
클린코드 지향

0개의 댓글