Skip to content

Commit ff27c87

Browse files
Refactored Booking.cs
Refactored Booking Class for Better Encapsulation & Method Design Exposed private fields as read-only properties. Marked IsBetween() as static since it does not depend on instance state. Added null-checks in the constructor to ensure valid input. Closes CodelyTV#62
1 parent de397ea commit ff27c87

File tree

1 file changed

+16
-21
lines changed
  • examples/csharp/csharp-booking-03_preserve_whole_object/src/Booking

1 file changed

+16
-21
lines changed

examples/csharp/csharp-booking-03_preserve_whole_object/src/Booking/Booking.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
using System;
1+
using System;
22

33
namespace CodelyTv.Booking
44
{
55
public sealed class Booking
66
{
7-
private readonly BookingId id;
8-
private readonly DateRange dateRange;
9-
private readonly Customer customer;
10-
private readonly BookingType bookingType;
11-
private readonly Discount discount;
12-
private readonly Tax tax;
7+
public BookingId Id { get; }
8+
public DateRange DateRange { get; }
9+
public Customer Customer { get; }
10+
public BookingType BookingType { get; }
11+
public Discount Discount { get; }
12+
public Tax Tax { get; }
1313

1414
public Booking(
1515
BookingId id,
@@ -20,30 +20,25 @@ public Booking(
2020
Tax tax
2121
)
2222
{
23-
this.id = id;
24-
this.dateRange = dateRange;
25-
this.customer = customer;
26-
this.bookingType = bookingType;
27-
this.discount = discount;
28-
this.tax = tax;
23+
Id = id ?? throw new ArgumentNullException(nameof(id), "Booking ID cannot be null.");
24+
DateRange = dateRange ?? throw new ArgumentNullException(nameof(dateRange), "Date range cannot be null.");
25+
Customer = customer ?? throw new ArgumentNullException(nameof(customer), "Customer cannot be null.");
26+
BookingType = bookingType;
27+
Discount = discount ?? throw new ArgumentNullException(nameof(discount), "Discount cannot be null.");
28+
Tax = tax ?? throw new ArgumentNullException(nameof(tax), "Tax cannot be null.");
2929
}
3030

3131
public BookingStatus StatusFor(DateTime date)
3232
{
33-
if (date < dateRange.StartDate)
33+
if (date < DateRange.StartDate)
3434
{
3535
return BookingStatus.NOT_STARTED;
3636
}
3737

38-
if (IsBetween(date, dateRange))
39-
{
40-
return BookingStatus.ACTIVE;
41-
}
42-
43-
return BookingStatus.FINISHED;
38+
return IsBetween(date, DateRange) ? BookingStatus.ACTIVE : BookingStatus.FINISHED;
4439
}
4540

46-
private bool IsBetween(DateTime date, DateRange dateRange)
41+
private static bool IsBetween(DateTime date, DateRange dateRange)
4742
{
4843
return date > dateRange.StartDate && date < dateRange.EndDate;
4944
}

0 commit comments

Comments
 (0)