I following the book "C# Tutorial" (https://video2.skills-academy.com/en-us/dotnet/csharp/tutorials/default-interface-methods-versions).
Since the book doesn't mention where the code should be placed, I just paste the code on a new tab "customer-relationship.cs".
The other codes are as follows.
ICustomer.cs
using System;
using System.Collections.Generic;
namespace customer_relationship
{
// <SnippetICustomerVersion1>
public interface ICustomer
{
IEnumerable<IOrder> PreviousOrders { get; }
DateTime DateJoined { get; }
DateTime? LastOrder { get; }
string Name { get; }
IDictionary<DateTime, string> Reminders { get; }
}
// </SnippetICustomerVersion1>
}
IOrder.cs
using System;
namespace customer_relationship
{
// <SnippetIOrderVersion1>
public interface IOrder
{
DateTime Purchased { get; }
decimal Cost { get; }
}
// </SnippetIOrderVersion1>
}
SampleCustomer.cs
using System;
using System.Collections.Generic;
namespace customer_relationship
{
public class SampleCustomer : ICustomer
{
public SampleCustomer(string name, DateTime dateJoined) =>
(Name, DateJoined) = (name, dateJoined);
private readonly List<IOrder> allOrders = new List<IOrder>();
public IEnumerable<IOrder> PreviousOrders => allOrders;
public DateTime DateJoined { get; }
public DateTime? LastOrder { get; private set; }
public string Name { get; }
private readonly Dictionary<DateTime, string> reminders = new Dictionary<DateTime, string>();
public IDictionary<DateTime, string> Reminders => reminders;
public void AddOrder(IOrder order)
{
if (order.Purchased > (LastOrder ?? DateTime.MinValue))
LastOrder = order.Purchased;
allOrders.Add(order);
}
}
}
Program.cs
using System;
namespace customer_relationship
{
// <SnippetIOrderVersion1>
public interface IOrder
{
DateTime Purchased { get; }
decimal Cost { get; }
}
// </SnippetIOrderVersion1>
}