no way to compare when less than two revisions
تفاوتها
تفاوت دو نسخهٔ متفاوت از صفحه را مشاهده میکنید.
— | courseware:python_programming:resources:answer:23 [2022/07/19 17:51] (فعلی) – ایجاد شد - ویرایش خارجی 127.0.0.1 | ||
---|---|---|---|
خط 1: | خط 1: | ||
+ | ==== پاسخ تمرین فروشگاه شیگرا ==== | ||
+ | فایل model.py: | ||
+ | |||
+ | |||
+ | <code python line-numbers=" | ||
+ | from time import strftime | ||
+ | import statistics | ||
+ | |||
+ | |||
+ | class User: | ||
+ | """ | ||
+ | User model which includes username of the client | ||
+ | """ | ||
+ | |||
+ | def __init__(self, | ||
+ | self.username = username | ||
+ | |||
+ | def __str__(self): | ||
+ | return self.username | ||
+ | |||
+ | |||
+ | class Product: | ||
+ | """ | ||
+ | Product model which includes products' | ||
+ | """ | ||
+ | |||
+ | num_of_products = 0 | ||
+ | |||
+ | def __init__(self, | ||
+ | Product.num_of_products += 1 | ||
+ | self.id = Product.num_of_products | ||
+ | self.name = name | ||
+ | self.price = price | ||
+ | self.quantities = {} | ||
+ | |||
+ | def __str__(self): | ||
+ | return str((self.id, | ||
+ | |||
+ | def add_quantity(self, | ||
+ | if store in self.quantities.keys(): | ||
+ | new_quantity = self.quantities[store] + number | ||
+ | if new_quantity >= 0: | ||
+ | self.quantities[store] = new_quantity | ||
+ | else: | ||
+ | raise Exception(' | ||
+ | else: | ||
+ | self.quantities[store] = number | ||
+ | |||
+ | @property | ||
+ | def quantity(self): | ||
+ | return sum(self.quantities.values()) | ||
+ | |||
+ | def __str__(self): | ||
+ | return str((self.id, | ||
+ | |||
+ | |||
+ | class Purchase: | ||
+ | """ | ||
+ | Purchase model includes: | ||
+ | - Who purchased? user. | ||
+ | - Which product? product. | ||
+ | - From which store? store. | ||
+ | - How many? number. | ||
+ | - When? date. | ||
+ | |||
+ | And it has a container for all registered purchases. | ||
+ | """ | ||
+ | |||
+ | purchases = list() | ||
+ | |||
+ | def __init__(self, | ||
+ | date: str = strftime(' | ||
+ | if product.quantities[store] - number < 0: | ||
+ | raise Exception(' | ||
+ | self.product = product | ||
+ | self.store = store | ||
+ | self.number = number | ||
+ | self.user = user | ||
+ | self.date = date | ||
+ | |||
+ | product.add_quantity(store, | ||
+ | Purchase.purchases.append(self) | ||
+ | |||
+ | def __str__(self): | ||
+ | return (str(self.product), | ||
+ | |||
+ | @classmethod | ||
+ | def store_revenue(cls, | ||
+ | return sum( | ||
+ | purchase.product.price * purchase.number | ||
+ | for purchase in cls.purchases | ||
+ | if purchase.store == store | ||
+ | ) | ||
+ | |||
+ | @classmethod | ||
+ | def users_purchased_product(cls, | ||
+ | return [purchase.user for purchase in cls.purchases if purchase.product == product] | ||
+ | |||
+ | |||
+ | class Comment: | ||
+ | """ | ||
+ | Comments model includes: | ||
+ | - who wrote the comment? user. | ||
+ | - what rate the user is given? rating. | ||
+ | - comment is written for which product? product. | ||
+ | - text of the comment? text. | ||
+ | """ | ||
+ | |||
+ | comments = list() | ||
+ | |||
+ | def __init__(self, | ||
+ | self.user = user | ||
+ | self.rating = rating | ||
+ | self.product = product | ||
+ | self.text = text | ||
+ | Comment.comments.append(self) | ||
+ | |||
+ | def __str__(self): | ||
+ | return "user: %s \n product: %s \n rating: %d \n text: %s" % (self.user, self.product, | ||
+ | |||
+ | @classmethod | ||
+ | def user_comments(cls, | ||
+ | return [comment for comment in cls.comments if comment.user == user] | ||
+ | |||
+ | @classmethod | ||
+ | def average_rate_purchased_product(cls, | ||
+ | return statistics.mean([comment.rating for comment in cls.comments if comment.product == product]) | ||
+ | |||
+ | </ | ||
+ | |||
+ | فایل store.py: | ||
+ | |||
+ | <code python line-numbers=" | ||
+ | from model import User, Product, Purchase, Comment | ||
+ | |||
+ | |||
+ | def main(): | ||
+ | done = False | ||
+ | while not done: | ||
+ | action = input(""" | ||
+ | 1 add user | ||
+ | 2 purchase | ||
+ | 3 add product | ||
+ | 4 add comment | ||
+ | 5 calculate revenue | ||
+ | 6 print users | ||
+ | 7 end | ||
+ | """ | ||
+ | done = action == 7 | ||
+ | |||
+ | user = User(' | ||
+ | product = Product(' | ||
+ | Purchase(product, | ||
+ | Comment(user, | ||
+ | |||
+ | |||
+ | if __name__ == ' | ||
+ | main() | ||
+ | |||
+ | </ |