using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LibrarySystem { public abstract class Item { // Instance variables. private string title; protected DateTime? dateBorrowed = null; private Member borrower = null; // Constructor. public Item(string title) { this.title = title; } // Business methods / properties. public bool Borrowed { get { return borrower != null; } } public virtual bool CanBeBorrowedBy(Member member) { return true; } public bool BorrowItemBy(Member member) { // Has the item not been borrowed yet, and is the specified member allowed to borrow it? if (!Borrowed && CanBeBorrowedBy(member)) { // Record the fact that this item is now borrowed by the member. borrower = member; dateBorrowed = DateTime.Today; borrower.BorrowedItem(); return true; } else { return false; } } public virtual void ReturnItem() { // Record the fact that this item is no longer borrowed by the member. borrower.ReturnedItem(); borrower = null; dateBorrowed = null; } public abstract DateTime? DateDueBack { get; } // string representation. public override string ToString() { if (borrower != null) { return string.Format("{0} is on loan to {1} [borrowed at {2}].", title, borrower.Name, dateBorrowed.Value.ToShortDateString()); } else { return string.Format("{0} is not on loan.", title); } } } }