summaryrefslogtreecommitdiff
path: root/slips/src/app/Marina/Domain/Customer.cs
blob: 8b9911c3985582bb45211463018d2f1f37ed64ca (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using Marina.Domain.Exceptions;
using Marina.Domain.Interfaces;
using Marina.Infrastructure;

namespace Marina.Domain {
	public class Customer : DomainObject, ICustomer {
		public Customer()
			: this( -1, new RichList< IBoat >( ), new RichList< ISlipLease >( ), CustomerRegistration.BlankRegistration( ) ) {}

		public Customer( long id, IEnumerable< IBoat > registeredBoats, IEnumerable< ISlipLease > leases,
		                 IRegistration registration ) : base( id ) {
			_registeredBoats = ListFactory.From( registeredBoats );
			_leases = ListFactory.From( leases );
			_registration = registration;
		}

		public IRegistration Registration() {
			return _registration;
		}

		public void RegisterAccount( string username, string password, string firstName, string lastName, string phoneNumber,
		                             string city ) {
			_registration = new CustomerRegistration( username, password, firstName, lastName, phoneNumber, city );
		}

		public void UpdateRegistrationTo( string username, string password, string firstName, string lastName,
		                                  string phoneNumber,
		                                  string city ) {
			UpdateRegistrationTo( new CustomerRegistration( username, password, firstName, lastName, phoneNumber, city ) );
		}

		public void UpdateRegistrationTo( IRegistration registration ) {
			_registration = registration ?? _registration;
		}

		public void RegisterBoat( string registrationNumber, string manufacturer, DateTime yearOfModel, long length ) {
			RegisterBoat( new Boat( registrationNumber, manufacturer, yearOfModel, length ) );
		}

		public void RegisterBoat( IBoat unregisteredBoat ) {
			if ( !IsBoatRegistered( unregisteredBoat ) ) {
				_registeredBoats.Add( unregisteredBoat );
			}
		}

		public IEnumerable< IBoat > RegisteredBoats() {
			return _registeredBoats.All( );
		}

		public void Lease( ISlip slip, ILeaseDuration duration ) {
			EnsureSlipIsNotLeased( slip );
			_leases.Add( slip.LeaseTo( this, duration ) );
		}

		public IEnumerable< ISlipLease > Leases() {
			return _leases.All( );
		}

		private void EnsureSlipIsNotLeased( ISlip slip ) {
			if ( slip.IsLeased( ) ) {
				throw new SlipIsAlreadyLeasedException( );
			}
		}

		private bool IsBoatRegistered( IBoat boat ) {
			return _registeredBoats.Contains( boat );
		}

		private readonly IRichList< IBoat > _registeredBoats;
		private readonly IRichList< ISlipLease > _leases;
		private IRegistration _registration;
		public static readonly ICustomer Unknown = new UnknownCustomer( );
	}
}