using System.Collections.ObjectModel; namespace Model; public class Person : IEquatable { public long Id { get; private init; } public string FirstName { get => firstName; set => firstName = !string.IsNullOrWhiteSpace(value) ? value : "Jane"; } private string firstName = null!; public string LastName { get => lastName; set => lastName = value ?? ""; } private string lastName = null!; public ReadOnlyCollection Books { get; private init; } private List books = new List(); public Person(string firstName, string lastName) : this(0, firstName, lastName) { } public Person(long id, string firstName, string lastName) { Id = id; FirstName = firstName; LastName = lastName; Books = new ReadOnlyCollection(books); } public bool BorrowBook(Book book) { if(books.Contains(book)) return false; books.Add(book); return true; } public bool ReturnBook(Book book) { return books.Remove(book); } public bool Equals(Person? other) { if(Id != 0) return Id == (other?.Id ?? 0); else return FirstName.Equals(other?.FirstName) && LastName.Equals(other?.LastName); } public override bool Equals(object? obj) { if(ReferenceEquals(obj, null)) return false; if(ReferenceEquals(this, obj)) return true; if(GetType() != obj?.GetType()) return false; return Equals(obj as Person); } public override int GetHashCode() { if(Id != 0) return (int)(Id % 7879); else return FirstName.GetHashCode() + LastName.GetHashCode(); } public override string ToString() => $"{Id} - {FirstName} {LastName} (#books: {Books.Count})"; }