-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorder.py
More file actions
123 lines (100 loc) · 3.88 KB
/
order.py
File metadata and controls
123 lines (100 loc) · 3.88 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from datetime import datetime
class Order:
"""Represents a purchase order for inventory restocking"""
# Class variable for generating unique order IDs
_order_counter = 1000
def __init__(self, product_id, quantity, priority=5, supplier_id=None):
"""
Initialize an Order
Args:
product_id (str): ID of the product to order
quantity (int): Quantity to order
priority (int): Priority level (1-10, where 1 is highest priority)
supplier_id (str): ID of the supplier (optional)
"""
Order._order_counter += 1
self._order_id = f"ORD{Order._order_counter}"
self._product_id = product_id
self._quantity = quantity
self._priority = priority
self._supplier_id = supplier_id
self._status = "Pending" # Pending, Approved, Shipped, Received, Cancelled
self._order_date = datetime.now()
self._received_date = None
# Getters
@property
def order_id(self):
return self._order_id
@property
def product_id(self):
return self._product_id
@property
def quantity(self):
return self._quantity
@property
def priority(self):
return self._priority
@property
def supplier_id(self):
return self._supplier_id
@property
def status(self):
return self._status
@property
def order_date(self):
return self._order_date
@property
def received_date(self):
return self._received_date
# Setters
@quantity.setter
def quantity(self, value):
if value <= 0:
raise ValueError("Quantity must be positive")
self._quantity = value
@priority.setter
def priority(self, value):
if not (1 <= value <= 10):
raise ValueError("Priority must be between 1 and 10")
self._priority = value
@status.setter
def status(self, value):
valid_statuses = ["Pending", "Approved", "Shipped", "Received", "Cancelled"]
if value not in valid_statuses:
raise ValueError(f"Status must be one of {valid_statuses}")
self._status = value
if value == "Received":
self._received_date = datetime.now()
def __lt__(self, other):
"""Less than comparison for priority queue (lower priority number = higher priority)"""
if self._priority != other._priority:
return self._priority < other._priority
return self._order_date < other._order_date
def __le__(self, other):
"""Less than or equal comparison"""
return self.__lt__(other) or self.__eq__(other)
def __gt__(self, other):
"""Greater than comparison"""
return not self.__le__(other)
def __ge__(self, other):
"""Greater than or equal comparison"""
return not self.__lt__(other)
def __eq__(self, other):
"""Equality comparison"""
return self._order_id == other._order_id
def __str__(self):
return f"Order({self._order_id}, Product: {self._product_id}, Qty: {self._quantity}, Priority: {self._priority}, Status: {self._status})"
def __repr__(self):
return self.__str__()
def to_dict(self):
"""Convert order to dictionary"""
return {
'order_id': self._order_id,
'product_id': self._product_id,
'quantity': self._quantity,
'priority': self._priority,
'supplier_id': self._supplier_id,
'status': self._status,
'order_date': self._order_date.strftime('%Y-%m-%d %H:%M:%S'),
'received_date': self._received_date.strftime('%Y-%m-%d %H:%M:%S') if self._received_date else None
}