summaryrefslogtreecommitdiff
path: root/slips/src/app/Marina/Domain/LeaseDurations.cs
blob: e87e56710b9ad92dc12df24a1badd4616ae6cd10 (plain)
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
using System;
using System.Collections.Generic;
using Marina.Domain.Interfaces;

namespace Marina.Domain {
	public class LeaseDurations {
		public static readonly ILeaseDuration Daily = new LeaseDuration( "Daily", 1, 1 );
		public static readonly ILeaseDuration Weekly = new LeaseDuration( "Weekly", 2, 7 );
		public static readonly ILeaseDuration Monthly = new LeaseDuration( "Monthly", 3, 30 );
		public static readonly ILeaseDuration Yearly = new LeaseDuration( "Yearly", 4, 365 );
		private static readonly ILeaseDuration NoDuration = new LeaseDuration( "No Duration", -1, 0 );
		private static readonly ILeaseDuration UnknownDuration = new LeaseDuration( "Unknown", -1, 0 );

		private class LeaseDuration : DomainObject, ILeaseDuration {
			private readonly int _days;
			private readonly string _name;

			public LeaseDuration( string name, long id, int days ) : base( id ) {
				_days = days;
				_name = name;
			}

			public DateTime CalculateExpiryDateFrom( DateTime startDate ) {
				return startDate.AddDays( _days ).Date.AddHours( 11 );
			}

			public string Name() {
				return _name;
			}

			public bool IsSatisfiedBy( IDateRange range ) {
				TimeSpan daysInRange = range.End( ).Subtract( range.Start( ) );
				return _days <= daysInRange.Days;
			}
		}

		public static ILeaseDuration FindFor( DateTime startDate, DateTime endDate ) {
			foreach ( ILeaseDuration duration in AllDurations( ) ) {
				if ( duration.IsSatisfiedBy( new DateRange( startDate, endDate ) ) ) {
					return duration;
				}
			}
			return NoDuration;
		}

		private static IEnumerable< ILeaseDuration > AllDurations() {
			yield return Yearly;
			yield return Monthly;
			yield return Weekly;
			yield return Daily;
		}

		public static ILeaseDuration FindBy( string duration ) {
			foreach ( ILeaseDuration leaseDuration in AllDurations( ) ) {
				if ( string.Compare( leaseDuration.Name( ), duration, true ) == 0 ) {
					return leaseDuration;
				}
			}
			return UnknownDuration;
		}
	}
}