view Library/LibrarySystem/Core/Library.cs @ 41:dbd242eb9c33

Initial wave of TDD development
author adminsh@apollo
date Tue, 03 Apr 2012 23:29:32 +0100
parents
children aef06698d9e2
line wrap: on
line source

using System.Collections.Generic;
using System.Linq;
using LibrarySystem.Interfaces;

namespace LibrarySystem.Core
{
    public class Library : ILibrary
    {
        public string Location { get; set; }
        public IList<IStock> StockList { get; set; }

        public Library()
        {
            StockList = new List<IStock>(0);
        }

        public void AddStock(params IBook[] books)
        {
            var stock = new List<IStock>(StockList.Count + books.Length);

            stock.AddRange(StockList);

            foreach (var book in books)
            {
                stock.Add(new Stock() { Book = book, IsAvailable = true });
            }

            StockList = stock;
        }

        public bool IsBookAvailableByISBN(string isbn)
        {
            var qry = from books in StockList
                      where books.Book.ISBN == isbn 
                            & books.IsAvailable
                      select books.IsAvailable;

            return qry.FirstOrDefault();
        }

        public IList<IStock> FindBooks(string searchString)
        {
            searchString = searchString.ToLower();

            var qry = from books in StockList
                      where books.Book.Title.ToLower().Contains(searchString)
                            || books.Book.Author.ToLower().Contains(searchString)
                      select books;

            return qry.ToList();
        }

        public void LendBook(IPerson person, IBook book)
        {
            throw new System.NotImplementedException();
        }
    }
}