forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbooking_manager.py
More file actions
36 lines (30 loc) · 1.08 KB
/
booking_manager.py
File metadata and controls
36 lines (30 loc) · 1.08 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
import datetime
from booking import Booking
from threading import Lock
class BookingManager:
_instance = None
_lock = Lock()
def __new__(cls):
if not cls._instance:
with cls._lock:
if not cls._instance:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
self.bookings = {}
self.booking_counter = 0
def create_booking(self, flight, passenger, seat, price):
booking_number = self._generate_booking_number()
booking = Booking(booking_number, flight, passenger, seat, price)
with self._lock:
self.bookings[booking_number] = booking
return booking
def cancel_booking(self, booking_number):
with self._lock:
booking = self.bookings.get(booking_number)
if booking:
booking.cancel()
def _generate_booking_number(self):
self.booking_counter += 1
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
return f"BKG{timestamp}{self.booking_counter:06d}"