-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventManager.cs
72 lines (61 loc) · 2.41 KB
/
EventManager.cs
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
using EventPlanningSystem.Delegates;
using EventPlanningSystem.Records;
namespace EventPlanningSystem;
/// <summary>
/// Manages events, including validation, notifications, and price calculation.
/// </summary>
public class EventManager
{
/// <summary>
/// Internal storage for events. (Event List)
/// </summary>
private List<Event> _events = new();
/// <summary>
/// Event triggered when a new event is added successfully.
/// Subscribers receive the event details and a success message.
/// </summary>
public event EventNotification OnEventAdded;
/// <summary>
/// List of validation rules for verifying event properties before adding.
/// </summary>
private List<EventValidator> _validationRules = new();
/// <summary>
/// Delegate for calculating event ticket prices.
/// </summary>
private PriceCalculator _priceCalculator;
/// <summary>
/// Adds a new event to the system after validating it against all registered rules.
/// Triggers the <see cref="OnEventAdded"/> event if successful.
/// </summary>
/// <param name="e">The event to be added.</param>
/// <exception cref="InvalidOperationException">
/// Thrown when one or more validation rules fail.
/// </exception>
public void AddEvent(Event e)
{
if (_validationRules.All(rule => rule(e)))
{
_events.Add(e);
OnEventAdded?.Invoke(e, "Event successfully added!");
}
else
{
throw new InvalidOperationException("Event validation failed!");
}
}
/// <summary>
/// Registers a new validation rule for verifying events.
/// </summary>
/// <param name="validator">The validation rule to be added.</param>
public void AddValidationRule(EventValidator validator) => _validationRules.Add(validator);
/// <summary>
/// Sets the delegate for calculating ticket prices based on event details and quantity.
/// </summary>
/// <param name="calculator">The price calculator delegate.</param>
public void SetPriceCalculator(PriceCalculator calculator) => _priceCalculator = calculator;
/// <summary>
/// Retrieves all events currently managed by the system.
/// </summary>
/// <returns>A list of <see cref="Event"/> objects.</returns>
public List<Event> GetEvents() => _events;
}