Created
February 28, 2019 07:26
-
-
Save tanakahisateru/d2f9e4ca30ae2f9c4163b99afdd01932 to your computer and use it in GitHub Desktop.
牛乳パック1つと卵があれば6つ買うをやるシステム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from shop import Item, Shop | |
shop = Shop('イオン ドームシティ店') | |
cart = shop.cart() | |
cart.add(Item.MILK) | |
# TODO implement more | |
receipt = shop.paycheck(cart) | |
print(receipt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from enum import Enum | |
class Item(Enum): | |
EGG = '卵' | |
MILK = '牛乳パック' | |
class Cart: | |
def __init__(self, shop): | |
self.shop = shop | |
self.items = [] | |
def add(self, item): | |
if self.shop.stock_of(item) >= 1: | |
self.items.append(item) | |
self.shop.stock[item] -= 1 | |
else: | |
raise Exception("%sの在庫がありません" % item.value) | |
def summary(self): | |
s = dict() | |
for item in self.items: | |
if item not in s: | |
s[item] = 0 | |
s[item] += 1 | |
return s | |
class Shop: | |
def __init__(self, name): | |
self.name = name | |
self.stock = dict() | |
# デフォルトの在庫を 100 とする | |
self.stocks(Item.MILK, 100) | |
self.stocks(Item.EGG, 100) | |
def stocks(self, item, amount): | |
self.stock[item] = amount | |
def cart(self): | |
return Cart(self) | |
def stock_of(self, item): | |
return self.stock[item] if item in self.stock else 0 | |
def paycheck(self, cart): | |
receipt = ["%s でのお買い上げ:" % self.name] | |
for (item, amount) in cart.summary().items(): | |
receipt.append(" %s ... %d点" % (item.value, amount,)) | |
return "\n".join(receipt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment