-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
86 lines (69 loc) · 2.93 KB
/
Copy pathstorage.py
File metadata and controls
86 lines (69 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from typing import Dict, Optional
from enums import ProductSellStatus, BuyProductStatus, NaptienStatus
class Product:
"""Đại diện cho mỗi khay hàng trong máy"""
def __init__(self, name, price, stock):
self.name = name
self.price = price
self.stock = stock
def sell(self) -> ProductSellStatus:
if self.stock < 1:
return ProductSellStatus.NOT_ENOUGH_STOCK
self.stock -= 1
return ProductSellStatus.SUCCESS
def __str__(self):
return f"Sản phẩm: {self.name} | giá {self.price} VND | Còn {self.stock} sản phẩm"
class VendingMachineStorage:
def __init__(self):
# Khởi tạo các khay hàng trong máy với tên sản phầm, giá tiền và số lượng sản phẩm trên những khay đó
self.products: Dict[int, Product] = {
1: Product("Coca-Cola", 50_000, 10), # Testcase: Số tiền lớn
2: Product("Pepsi", 15_000, 0), # Testcase: Hết hàng
3: Product("Sting Dâu", 10_000, 5),
4: Product("Red Bull", 20_000, 2),
5: Product("Nước Suối", 10_000, 20),
6: Product("Trà Xanh Không Độ", 10_000, 7),
7: Product("Café lon", 20_000, 0),
8: Product("Olong Tea", 10_000, 9),
9: Product("Sữa Bắp", 10_000, 3),
10: Product("Nước Cam Ép", 10_000, 12),
11: Product("Revive", 10_000, 4),
12: Product("Aquafina", 10_000, 15),
13: Product("Trà Sữa Đóng Chai", 20_000, 6),
14: Product("7Up", 20_000, 8),
15: Product("Mirinda Cam", 20_000, 5),
# ID 16 and more: TestCase: hàng không tồn tại
}
self.money = 0
def nap_tien(self, money: int) -> NaptienStatus:
if not isinstance(money, int):
return NaptienStatus.INVALID
if money < 10_000:
return NaptienStatus.UNDER_10K
self.money += money
return NaptienStatus.SUCCESS
def get_balance(self):
return self.money
def has_products(self, id_mon_hang: int):
return id_mon_hang in self.products
def get_all_products(self):
return self.products
def get_product(self, id) -> Optional[Product]:
try:
return self.products[id]
except KeyError:
return None
def sell_product(self, id) -> BuyProductStatus:
if not self.has_products(id):
return BuyProductStatus.NOT_EXISTS
product = self.get_product(id)
if not product:
return BuyProductStatus.NOT_EXISTS
if self.money < product.price:
return BuyProductStatus.NOT_ENOUGH_MONEY
if product.stock < 1:
return BuyProductStatus.NOT_ENOUGH_STOCK
if product.sell() == ProductSellStatus.SUCCESS:
self.money -= product.price
return BuyProductStatus.SUCCESS
return BuyProductStatus.FAILED