diff Library/LibrarySystem/Core/Library.cs @ 43:aef06698d9e2 Test

Added the library tests
author Steven Hollidge <stevenhollidge@hotmail.com>
date Wed, 04 Apr 2012 19:20:20 +0100
parents dbd242eb9c33
children 3591c26bd63e
line wrap: on
line diff
--- a/Library/LibrarySystem/Core/Library.cs	Tue Apr 03 23:32:40 2012 +0100
+++ b/Library/LibrarySystem/Core/Library.cs	Wed Apr 04 19:20:20 2012 +0100
@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Linq;
 using LibrarySystem.Interfaces;
 
@@ -6,30 +7,55 @@
 {
     public class Library : ILibrary
     {
-        public string Location { get; set; }
-        public IList<IStock> StockList { get; set; }
+        public string Location { get; private set; }
+        public IList<IStock> StockList { get; private set; }
+
+        private Library()
+        {
+        }
 
-        public Library()
+        public static ILibrary Create(string location)
         {
-            StockList = new List<IStock>(0);
+            if (location == null)
+                throw new ArgumentNullException();
+
+            return new Library() {Location = location, StockList = new List<IStock>(0)};
         }
 
         public void AddStock(params IBook[] books)
         {
+            if (books == null)
+                throw new ArgumentNullException();
+
             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 });
+                if (book == null)
+                    throw new ArgumentNullException();
+
+                ThrowExceptionIfBookIsAlreadyInStock(book);
+
+                stock.Add(Stock.Create(book, true));
             }
 
             StockList = stock;
         }
 
+        private void ThrowExceptionIfBookIsAlreadyInStock(IBook book)
+        {
+            var lookup = ((List<IStock>) StockList).Find(s => s.Book.Id == book.Id);
+            if (lookup != null)
+                throw new Exception(string.Format("Book id {0} is already in stock", book.Id));
+        }
+
         public bool IsBookAvailableByISBN(string isbn)
         {
+            if (isbn == null)
+                throw new ArgumentNullException();
+
             var qry = from books in StockList
                       where books.Book.ISBN == isbn 
                             & books.IsAvailable
@@ -40,6 +66,9 @@
 
         public IList<IStock> FindBooks(string searchString)
         {
+            if (searchString == null)
+                throw new ArgumentNullException();
+
             searchString = searchString.ToLower();
 
             var qry = from books in StockList
@@ -50,9 +79,38 @@
             return qry.ToList();
         }
 
-        public void LendBook(IPerson person, IBook book)
+        public IBook GetBookById(string id)
         {
-            throw new System.NotImplementedException();
+            if (id == null)
+                throw new ArgumentNullException();
+
+            id = id.ToLower();
+
+            var qry = from books in StockList
+                      where books.Book.Id.ToLower() == id
+                      select books.Book;
+
+            return qry.FirstOrDefault();
+        }
+
+
+        public void LendBook(IPerson person, string id)
+        {
+            if (person == null || id == null)
+                throw new ArgumentNullException();
+
+            var stock = ThrowExceptionIfBookIsNotInStock(id);
+            stock.IsAvailable = false;
+
+            // FUTURE: register with person that they have borrowed the book
+        }
+
+        private IStock ThrowExceptionIfBookIsNotInStock(string id)
+        {
+            var lookup = ((List<IStock>) StockList).Find(s => s.Book.Id == id);
+            if (lookup == null)
+                throw new Exception(string.Format("Book with id {0} is not in stock", id));
+            return lookup;
         }
     }
 }