기업과제로 자판기 알고리즘을 구현해보았다.
payment_method = [CASH, CARD]
price = [1100, 600, 700]
drink = ["coke", "water", "coffee"]
cash_type = [10000, 5000, 1000, 500, 100]
cash_output = {100 : 0, 500 : 0, 1000 : 0, 5000 : 0, 10000 : 0}
#최초 자판기 내부의 거스름돈 액수는 임의 지정
cash_balance = {100 : 100, 500 : 100, 1000 : 100, 5000 : 100, 10000 : 100}
#최초 자판기 내부의 음료 재고 개수는 임의 지정
drink_balance = {"coke_1" : 10, "coke_2" : 10, "coke_3" : 10, "coke_4" : 10,
"water_1" : 10, "water_2" : 10, "water_3" : 10, "water_4" : 10
"coffee_1" : 10, "coffee_2" : 10, "coffee_3" : 10, "coffee_4" : 10}
IF "coke" in INPUT("음료 선택") THEN
drink_num = 0
ELSE IF "water" in INPUT("음료 선택") THEN
drink_num = 1
ELSE "coffee" in INPUT("음료 선택") THEN
drink_num = 2
input_balance = 0
input_amount = integer(INPUT("투입 금액"))
FUNCTION BalanceCheck(input_amount, drink_num)
cash_amount = 0
FOR key in cash_balance
cash_amount += (cash_balance[key] * key)
END FOR LOOP
IF cash_amount > price[drink_num] THEN
OUTPUT True
ELSE
OUTPUT False
END IF
END FUNCTION
FUNCTION CashOutput(input_amount)
FOR amount in cash_type
WHILE input_amount > amount
cash_output[amount] += 1
input_amount -= amount
END WHILE LOOP
END FOR LOOP
OUTPUT cash_output
END FUNCTION
FUNCTION VendingMachine(input_balance, input_amount, input_drink, payment_method)
WHILE True
input_amount += input_balance
FOR balance in drink_balance
IF drink_balance[balance] != 0 THEN
OUTPUT "음료 버튼 활성화"
END IF input_amount > price[drink_num] THEN
OUTPUT "음료 버튼 활성화"
IF payment_method == CARD THEN
IF CARD_BALANCE < price[drink_num] THEN
CARD_CONNECTION = False
OUTPUT "카드의 잔액이 부족합니다."
ELSE
CARD_BALANCE -= price[drink_num]
drink_balance[input_drink] -= 1
CARD_CONNECTION = False
OUTPUT input_drink
END IF
ELSE
IF input_amount < price[drink_num] THEN
OUTPUT "금액이 부족합니다."
ELSE
IF BalanceCheck(input_amount, drink_num) == False THEN
OUTPUT "자판기 내 거스름돈이 부족합니다"
ELSE
input_amount -= price[drink_num]
input_balance = input_amount
drink_balance[input_drink] -= 1
OUTPUT [input_drink, input_balance]
END IF
END IF
IF INPUT("금액 반환 버튼") == True THEN
IF input_balance == 0 THEN
OUTPUT "반환할 금액이 없습니다."
ELSE
cash_output = CashOutput(input_amount)
FOR amount in cash_type
cash_balance[amount] -= cash_output[amount]
END FOR LOOP
input_balance = 0
OUTPUT [cash_output, input_balance, cash_balance]
END IF
END IF
END WHILE LOOP
END FUNCTION