Skip to content

Commit

Permalink
Refactored Booking.cs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Mykyta-Panchenko authored Feb 7, 2025
1 parent de397ea commit ff27c87
Showing 1 changed file with 16 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System;
using System;

namespace CodelyTv.Booking
{
public sealed class Booking
{
private readonly BookingId id;
private readonly DateRange dateRange;
private readonly Customer customer;
private readonly BookingType bookingType;
private readonly Discount discount;
private readonly Tax tax;
public BookingId Id { get; }
public DateRange DateRange { get; }
public Customer Customer { get; }
public BookingType BookingType { get; }
public Discount Discount { get; }
public Tax Tax { get; }

public Booking(
BookingId id,
Expand All @@ -20,30 +20,25 @@ public Booking(
Tax tax
)
{
this.id = id;
this.dateRange = dateRange;
this.customer = customer;
this.bookingType = bookingType;
this.discount = discount;
this.tax = tax;
Id = id ?? throw new ArgumentNullException(nameof(id), "Booking ID cannot be null.");
DateRange = dateRange ?? throw new ArgumentNullException(nameof(dateRange), "Date range cannot be null.");
Customer = customer ?? throw new ArgumentNullException(nameof(customer), "Customer cannot be null.");
BookingType = bookingType;
Discount = discount ?? throw new ArgumentNullException(nameof(discount), "Discount cannot be null.");
Tax = tax ?? throw new ArgumentNullException(nameof(tax), "Tax cannot be null.");
}

public BookingStatus StatusFor(DateTime date)
{
if (date < dateRange.StartDate)
if (date < DateRange.StartDate)
{
return BookingStatus.NOT_STARTED;
}

if (IsBetween(date, dateRange))
{
return BookingStatus.ACTIVE;
}

return BookingStatus.FINISHED;
return IsBetween(date, DateRange) ? BookingStatus.ACTIVE : BookingStatus.FINISHED;
}

private bool IsBetween(DateTime date, DateRange dateRange)
private static bool IsBetween(DateTime date, DateRange dateRange)
{
return date > dateRange.StartDate && date < dateRange.EndDate;
}
Expand Down

0 comments on commit ff27c87

Please sign in to comment.