You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.0 KiB
78 lines
2.0 KiB
using System.Collections.ObjectModel;
|
|
namespace Model;
|
|
|
|
public class Person : IEquatable<Person>
|
|
{
|
|
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<Book> Books { get; private init; }
|
|
private List<Book> books = new List<Book>();
|
|
|
|
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<Book>(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})";
|
|
}
|