๐ ์์์ฆ ์ถ๊ฐ , ์ธ๊ธ ํ์จ ์ถ๊ฐ
๐ ๊ธฐ๋ฅ ๊ฐ์ ๋ฐ ๊ด๋ฆฌ์ ๊ถํ ์ถ๊ฐ
๐ ์์๋

๐ Full Code
import csv
class Kiosk:
def __init__(self, mac_list=None, admin_key=9712):
if mac_list is None:
if mac_list is None:
self.load_menu_from_csv('menu.csv')
else:
self.mac_list = mac_list
self.balance = 0
self.select_dict = {}
self.admin_key = admin_key
self.revenue = 0
def load_menu_from_csv(self, csv_filename):
self.mac_list = []
with open(csv_filename, newline="", encoding="utf-8") as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
menu_item = {
"menu": row["menu"],
"price": int(row["price"]),
"quantity": int(row["quantity"])
}
self.mac_list.append(menu_item)
def save_receipt(self):
if not self.select_dict:
print('')
return
total_price_with_tax = self.calculate_total_price_with_tax()
with open('receipt.csv', 'a', newline='', encoding='utf-8') as csv_file:
writer = csv.writer(csv_file)
if csv_file.tell() == 0:
writer.writerow(['์ฃผ๋ฌธ๋ฒํธ', '๋ฉ๋ด', '์๋', '์ด ๊ฐ๊ฒฉ (์ธ๊ธ ํฌํจ)'])
for index, (menu, quantity) in enumerate(self.select_dict.items(), start=1):
menu_index = self.find_menu_index(menu)
price_per_item = self.mac_list[menu_index]['price']
total_price_per_item = price_per_item * quantity
row = [index, menu, quantity, total_price_per_item]
writer.writerow(row)
writer.writerow(['์ด ๊ฐ๊ฒฉ (์ธ๊ธ ํฌํจ) : ' f'{total_price_with_tax:.0f}์'])
print("--------์์์ฆ--------")
for index, (menu, quantity) in enumerate(self.select_dict.items(), start=1):
menu_index = self.find_menu_index(menu)
price_per_item = self.mac_list[menu_index]['price']
total_price_per_item = price_per_item * quantity
print(f"{index}. {menu} {quantity}๊ฐ : {total_price_per_item:}์")
print(f"์ด ๊ฐ๊ฒฉ (์ธ๊ธ ํฌํจ): {int(total_price_with_tax):}์")
print('์์์ฆ์ด ์ถ๋ ฅ๋์์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค.')
self.reset_balance_and_cart()
def run(self):
while True:
print(' ๋ฒ๊ฑฐ์ง ํค์ค์คํฌ')
print()
self.show_menu('ํ๋ฒ๊ฑฐ ์ฝ๋', '๋ถ๊ณ ๊ธฐ ๋ฒ๊ฑฐ', '๋๋ธ ๋๋ธ ๋ฒ๊ฑฐ')
self.show_menu('์๋ฌ๋ ์ฝ๋', '์ ์ฑ ์๋ฌ๋', '์ํ ์๋ฌ๋')
self.show_menu('์๋ฃ ์ฝ๋', '์ฝ๋ผ', '์ฌ์ด๋ค')
print()
continue_order = True
while continue_order:
self.select_item()
continue_order = self.payment_procedure()
if not continue_order:
self.save_receipt()
def show_menu(self, title, start_item, end_item):
print(f' {title}')
flag = False
for i, item in enumerate(self.mac_list):
if item["menu"] == start_item:
flag = True
if flag:
print(f'{i + 1}. {item["menu"]} : {item["price"]}์ ', end=' ')
if item["menu"] == end_item:
flag = False
break
print()
def show_select_menu_items(self):
sorted_items = sorted(self.select_dict.items(), key=lambda item: self.find_menu_index(item[0]))
for menu, quantity in sorted_items:
menu_idx = self.find_menu_index(menu)
for i, item in enumerate(self.mac_list):
if item['menu'] == menu:
menu_idx = i
break
if menu_idx is not None:
print(f'๋ฉ๋ด ๋ฒํธ : {menu_idx + 1}๋ฒ , {menu} : {quantity} ๊ฐ')
def update_balance_and_inventory(self, menu_index, qty):
select_price = self.mac_list[menu_index - 1]['price']
self.balance += select_price * qty
self.revenue += select_price * qty
self.mac_list[menu_index - 1]['quantity'] -= qty
if self.mac_list[menu_index - 1]['menu'] in self.select_dict:
self.select_dict[self.mac_list[menu_index - 1]['menu']] += qty
else:
self.select_dict[self.mac_list[menu_index - 1]['menu']] = qty
selected_item = self.mac_list[menu_index - 1]['menu']
print(f'{selected_item} {qty}๊ฐ๋ฅผ ์ ํํ์
จ์ต๋๋ค.')
print()
def select_item(self):
while True:
try:
select_menu = int(
input('๋ฉ๋ด ๋ฒํธ๋ฅผ ์
๋ ฅ ํด์ฃผ์ธ์ , "0" ์ ์
๋ ฅํ์๋ฉด ๊ฒฐ์ ๋ฅผ ์งํํฉ๋๋ค. "10" ์ ์
๋ ฅํ์๋ฉด ์ ํํ ๋ฉ๋ด๋ฅผ ๋ณด์ฌ๋๋ฆฝ๋๋ค. : '))
print()
if select_menu == self.admin_key:
self.admin_page()
continue
if select_menu == 0:
if not self.select_dict:
print('์ ํํ์ ๋ฉ๋ด๊ฐ ์์ต๋๋ค, ๋ค์ ์
๋ ฅํด์ฃผ์ธ์')
continue
else:
break
if select_menu == 10:
if self.select_dict:
print(f'ํ์ฌ ์ ํํ ๋ฌผํ')
self.show_select_menu_items()
change_menu = int(input('์๋์ ๋ณ๊ฒฝํ ๋ฉ๋ด์ ๋ฒํธ๋ฅผ ์ ํํ์ธ์ , 0 ์ ์
๋ ฅํ์๋ฉด ์ด์ ํ๋ฉด์ผ๋ก ๋๋์๊ฐ๋๋ค. : '))
if change_menu == 0:
print()
self.show_menu('ํ๋ฒ๊ฑฐ ์ฝ๋', '๋ถ๊ณ ๊ธฐ ๋ฒ๊ฑฐ', '๋๋ธ ๋๋ธ ๋ฒ๊ฑฐ')
self.show_menu('์๋ฌ๋ ์ฝ๋', '์ ์ฑ ์๋ฌ๋', '์ํ ์๋ฌ๋')
self.show_menu('์๋ฃ ์ฝ๋', '์ฝ๋ผ', '์ฌ์ด๋ค')
print()
continue
if change_menu <= 0 or change_menu > len(self.mac_list):
print('์ฌ๋ฐ๋ฅธ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ญ์์ค')
continue
menu_name = self.mac_list[change_menu - 1]['menu']
if menu_name not in self.select_dict:
print('ํด๋น ๋ฉ๋ด๊ฐ ๊ตฌ๋งค์ ํ์ ์์ง ์์ต๋๋ค. ๋ค์ ์ ํํด์ฃผ์ธ์')
continue
new_quantity = int(
input(f'๋ณ๊ฒฝํ ์๋์ ์
๋ ฅํ์ธ์ (ํ์ฌ ์๋: {self.select_dict[menu_name]}๊ฐ): '))
if new_quantity < 0:
print('์ฌ๋ฐ๋ฅธ ์๋์ ์
๋ ฅํด์ฃผ์ธ์.')
continue
self.select_dict[menu_name] = new_quantity
print(f"{menu_name}์ ์๋์ด {new_quantity}๊ฐ๋ก ๋ณ๊ฒฝ๋์์ต๋๋ค.")
continue
else:
print('ํ์ฌ ์ ํ๋ ๋ฌผํ์ด ์์ต๋๋ค.')
continue
if select_menu <= 0 or select_menu > len(self.mac_list):
print('์ฌ๋ฐ๋ฅธ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ญ์์ค')
continue
select_quantity = int(input('๋ช ๊ฐ๋ฅผ ๊ตฌ๋งคํ์๊ฒ ์ต๋๊น? 0์ ์
๋ ฅํ์๋ฉด ๊ตฌ๋งค๋ฅผ ์ทจ์ํฉ๋๋ค. : '))
if select_quantity == 0:
print('๊ตฌ๋งค๊ฐ ์ทจ์๋์์ต๋๋ค.')
continue
if self.mac_list[select_menu - 1]['quantity'] < select_quantity:
print('ํด๋น ๋ฉ๋ด์ ์ฌ๊ณ ๊ฐ ๋ถ์กฑํฉ๋๋ค. ๋ค๋ฅธ ๋ฉ๋ด๋ก ๋ค์ ์ ํํด์ฃผ์ธ์.')
continue
self.update_balance_and_inventory(select_menu,select_quantity)
except ValueError:
print('์
๋ ฅ์ด ์๋ชป๋์
จ์ต๋๋ค. ๋ค์ ์
๋ ฅํด์ฃผ์ธ์')
continue
def reset_balance_and_cart(self):
self.balance = 0
self.select_dict = {}
def find_menu_index(self, menu):
for i, item in enumerate(self.mac_list):
if item['menu'] == menu:
return i
return -1
def payment_procedure(self):
while True:
payment = input(
f'์ด ๊ตฌ๋งค ๋ด์ญ์ : {", ".join([f"{menu} {count}๊ฐ" for menu, count in self.select_dict.items()])} ์
๋๋ค.\n'
f'์ด ๊ฐ๊ฒฉ์ {self.balance}์ ์
๋๋ค. ๊ฒฐ์ ์งํ ํ์๊ฒ ์ต๋๊น? '
f'๊ฒฐ์ ํ๋ ค๋ฉด "x" ์
๋ ฅ , ์ถ๊ฐ๋ก ๊ฒฐ์ ํ๋ ค๋ฉด "c"๋ฅผ ์
๋ ฅํด์ฃผ์ญ์์ค. : ')
print()
if payment == 'c':
return True
elif payment == 'x':
while True:
try:
money = int(input('ํ๊ธ์ ๋ฃ์ด์ฃผ์ธ์ : '))
break
except ValueError:
print('์๋ชป๋ ๊ธ์ก์ ์
๋ ฅํ์
จ์ต๋๋ค. ์ ์ ํํ๋ก ๋ค์ ์
๋ ฅํด์ฃผ์ธ์.')
if money < self.balance:
print('ํ๊ธ์ด ๋ถ์กฑํฉ๋๋ค, ๋ค์ ๋ฃ์ด์ฃผ์ธ์')
continue
else:
change = money - self.balance
print(
f'๊ตฌ๋งค ๋ด์ญ์ : {", ".join([f"{menu} {count}๊ฐ" for menu, count in self.select_dict.items()])} ์
๋๋ค. \n'
f'๊ฒฐ์ ์๋ฃ๋์์ต๋๋ค. ์๊ธ : {change} ์
๋๋ค. \n'
f'์ด์ฉํด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค.')
print()
self.save_receipt()
return False
else:
print('์
๋ ฅ์ด ์๋ชป๋์์ต๋๋ค.')
continue
def calculate_total_price_with_tax(self):
total_price = sum(self.mac_list[self.find_menu_index(menu)]['price'] * quantity for menu, quantity in
self.select_dict.items())
tax_rate = 0.1
total_price_with_tax = total_price * (1 + tax_rate)
return total_price_with_tax
def admin_page(self):
print('๊ด๋ฆฌ์ ํ์ด์ง์ ์ ์ํ์์ต๋๋ค.')
while True:
print('1. ์ฌ๊ณ ํ์ธ')
print('2. ์ฌ๊ณ ์ถ๊ฐ')
print('3. ๋งค์ถ ํ์ธ')
print('4. ๋๊ฐ๊ธฐ')
choice = int(input('์ํ๋ ๋ฉ๋ด ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์: '))
if choice == 1:
self.check_inventory()
elif choice == 2:
self.add_inventory()
elif choice == 3:
self.check_revenue()
elif choice == 4:
print()
self.show_menu('ํ๋ฒ๊ฑฐ ์ฝ๋', '๋ถ๊ณ ๊ธฐ ๋ฒ๊ฑฐ', '๋๋ธ ๋๋ธ ๋ฒ๊ฑฐ')
self.show_menu('์๋ฌ๋ ์ฝ๋', '์ ์ฑ ์๋ฌ๋', '์ํ ์๋ฌ๋')
self.show_menu('์๋ฃ ์ฝ๋', '์ฝ๋ผ', '์ฌ์ด๋ค')
print()
break
else:
print('์ฌ๋ฐ๋ฅธ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์.')
def check_inventory(self):
for i, item in enumerate(self.mac_list):
print(f"{i + 1}. {item['menu']} - ์ฌ๊ณ : {item['quantity']}๊ฐ")
print()
def add_inventory(self):
for i, item in enumerate(self.mac_list):
print(f"{i + 1}. {item['menu']}")
item_number = int(input('์ฌ๊ณ ๋ฅผ ์ถ๊ฐํ ๋ฉ๋ด์ ๋ฒํธ๋ฅผ ์ ํํ์ธ์: '))
add_quantity = int(input('์ถ๊ฐํ ์ฌ๊ณ ์ ์์ ์
๋ ฅํ์ธ์: '))
self.mac_list[item_number - 1]['quantity'] += add_quantity
print(f"{self.mac_list[item_number - 1]['menu']}์ ์ฌ๊ณ ๊ฐ {add_quantity}๊ฐ ์ถ๊ฐ๋์์ต๋๋ค.")
print()
def check_revenue(self):
print(f'ํ์ฌ ๋งค์ถ์ {self.revenue}์ ์
๋๋ค.')
receipt_data = []
with open('receipt.csv', 'r', encoding='utf-8') as csv_file:
reader = csv.reader(csv_file)
next(reader)
for row in reader:
receipt_data.append(row)
for order_number, menu, quantity, total_price in receipt_data:
print(f'์ฃผ๋ฌธ๋ฒํธ: {order_number}, ๋ฉ๋ด: {menu}, ์๋: {quantity}, ์ด ๊ฐ๊ฒฉ: {total_price}์')
print()
if len(receipt_data) < len(self.select_dict):
print('์๋ ๋ฉ๋ด์ ๋งค์ถ์ด ๋๋ฝ๋์์ต๋๋ค:')
receipt_menu_names = [row[1] for row in receipt_data]
for menu, quantity in self.select_dict.items():
if menu not in receipt_menu_names:
print(f'{menu} - {quantity}๊ฐ')
if __name__ == '__main__':
kiosk = Kiosk()
kiosk.run()
menu.csv
menu,price,quantity
๋ถ๊ณ ๊ธฐ ๋ฒ๊ฑฐ,3000,100
์ ๋กํผ์ธ ๋ฒ๊ฑฐ,4000,100
๋๋ธ ๋๋ธ ๋ฒ๊ฑฐ,4500,100
์ ์ฑ ์๋ฌ๋,2500,100
๊ฐ์ ์๋ฌ๋,2000,100
์ํ ์๋ฌ๋,2000,100
์ฝ๋ผ,2000,200
์์ดํฌ,2500,200
์ฌ์ด๋ค,2000,200
receipt.csv
์ฃผ๋ฌธ๋ฒํธ,๋ฉ๋ด,์๋,์ด ๊ฐ๊ฒฉ (์ธ๊ธ ํฌํจ)
1,๋ถ๊ณ ๊ธฐ ๋ฒ๊ฑฐ,10,30000
2,์ ๋กํผ์ธ ๋ฒ๊ฑฐ,10,40000
3,์ฝ๋ผ,10,20000
์ด ๊ฐ๊ฒฉ (์ธ๊ธ ํฌํจ) : 99000์
1,๋ถ๊ณ ๊ธฐ ๋ฒ๊ฑฐ,10,30000
2,์ ๋กํผ์ธ ๋ฒ๊ฑฐ,10,40000
3,์ฝ๋ผ,10,20000
์ด ๊ฐ๊ฒฉ (์ธ๊ธ ํฌํจ) : 99000์
1,๋ถ๊ณ ๊ธฐ ๋ฒ๊ฑฐ,10,30000
2,์ ๋กํผ์ธ ๋ฒ๊ฑฐ,10,40000
3,๋๋ธ ๋๋ธ ๋ฒ๊ฑฐ,10,45000
4,์์ดํฌ,10,25000
5,์ฌ์ด๋ค,10,20000
์ด ๊ฐ๊ฒฉ (์ธ๊ธ ํฌํจ) : 176000์
1,๋ถ๊ณ ๊ธฐ ๋ฒ๊ฑฐ,10,30000
2,์ ๋กํผ์ธ ๋ฒ๊ฑฐ,10,40000
3,๋๋ธ ๋๋ธ ๋ฒ๊ฑฐ,10,45000
4,์ฝ๋ผ,100,200000
์ด ๊ฐ๊ฒฉ (์ธ๊ธ ํฌํจ) : 346500์
1,๋ถ๊ณ ๊ธฐ ๋ฒ๊ฑฐ,10,30000
2,์ ๋กํผ์ธ ๋ฒ๊ฑฐ,10,40000
3,์ฝ๋ผ,10,20000
์ด ๊ฐ๊ฒฉ (์ธ๊ธ ํฌํจ) : 99000์
1,๋ถ๊ณ ๊ธฐ ๋ฒ๊ฑฐ,10,30000
2,์ ๋กํผ์ธ ๋ฒ๊ฑฐ,1,4000
3,๋๋ธ ๋๋ธ ๋ฒ๊ฑฐ,2,9000
์ด ๊ฐ๊ฒฉ (์ธ๊ธ ํฌํจ) : 47300์
1,๋ถ๊ณ ๊ธฐ ๋ฒ๊ฑฐ,10,30000
์ด ๊ฐ๊ฒฉ (์ธ๊ธ ํฌํจ) : 33000์