forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookingManager.cs
More file actions
58 lines (52 loc) · 1.68 KB
/
BookingManager.cs
File metadata and controls
58 lines (52 loc) · 1.68 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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
namespace AirlineManagementSystem
{
public class BookingManager
{
private static BookingManager instance;
private readonly Dictionary<string, Booking> bookings = new Dictionary<string, Booking>();
private readonly object lockObject = new object();
private static int bookingCounter = 0;
private BookingManager() { }
public static BookingManager Instance
{
get
{
if (instance == null)
{
instance = new BookingManager();
}
return instance;
}
}
public Booking CreateBooking(Flight flight, Passenger passenger, Seat seat, double price)
{
string bookingNumber = GenerateBookingNumber();
var booking = new Booking(bookingNumber, flight, passenger, seat, price);
lock (lockObject)
{
bookings[bookingNumber] = booking;
}
return booking;
}
public void CancelBooking(string bookingNumber)
{
lock (lockObject)
{
if (bookings.TryGetValue(bookingNumber, out var booking))
{
booking.Cancel();
}
}
}
private string GenerateBookingNumber()
{
int bookingId = Interlocked.Increment(ref bookingCounter);
string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture);
return $"BKG{timestamp}{bookingId:D6}";
}
}
}