comparison 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
comparison
equal deleted inserted replaced
42:0d4aff4f699d 43:aef06698d9e2
1 using System.Collections.Generic; 1 using System;
2 using System.Collections.Generic;
2 using System.Linq; 3 using System.Linq;
3 using LibrarySystem.Interfaces; 4 using LibrarySystem.Interfaces;
4 5
5 namespace LibrarySystem.Core 6 namespace LibrarySystem.Core
6 { 7 {
7 public class Library : ILibrary 8 public class Library : ILibrary
8 { 9 {
9 public string Location { get; set; } 10 public string Location { get; private set; }
10 public IList<IStock> StockList { get; set; } 11 public IList<IStock> StockList { get; private set; }
11 12
12 public Library() 13 private Library()
13 { 14 {
14 StockList = new List<IStock>(0); 15 }
16
17 public static ILibrary Create(string location)
18 {
19 if (location == null)
20 throw new ArgumentNullException();
21
22 return new Library() {Location = location, StockList = new List<IStock>(0)};
15 } 23 }
16 24
17 public void AddStock(params IBook[] books) 25 public void AddStock(params IBook[] books)
18 { 26 {
27 if (books == null)
28 throw new ArgumentNullException();
29
19 var stock = new List<IStock>(StockList.Count + books.Length); 30 var stock = new List<IStock>(StockList.Count + books.Length);
20 31
21 stock.AddRange(StockList); 32 stock.AddRange(StockList);
22 33
23 foreach (var book in books) 34 foreach (var book in books)
24 { 35 {
25 stock.Add(new Stock() { Book = book, IsAvailable = true }); 36 if (book == null)
37 throw new ArgumentNullException();
38
39 ThrowExceptionIfBookIsAlreadyInStock(book);
40
41 stock.Add(Stock.Create(book, true));
26 } 42 }
27 43
28 StockList = stock; 44 StockList = stock;
29 } 45 }
30 46
47 private void ThrowExceptionIfBookIsAlreadyInStock(IBook book)
48 {
49 var lookup = ((List<IStock>) StockList).Find(s => s.Book.Id == book.Id);
50 if (lookup != null)
51 throw new Exception(string.Format("Book id {0} is already in stock", book.Id));
52 }
53
31 public bool IsBookAvailableByISBN(string isbn) 54 public bool IsBookAvailableByISBN(string isbn)
32 { 55 {
56 if (isbn == null)
57 throw new ArgumentNullException();
58
33 var qry = from books in StockList 59 var qry = from books in StockList
34 where books.Book.ISBN == isbn 60 where books.Book.ISBN == isbn
35 & books.IsAvailable 61 & books.IsAvailable
36 select books.IsAvailable; 62 select books.IsAvailable;
37 63
38 return qry.FirstOrDefault(); 64 return qry.FirstOrDefault();
39 } 65 }
40 66
41 public IList<IStock> FindBooks(string searchString) 67 public IList<IStock> FindBooks(string searchString)
42 { 68 {
69 if (searchString == null)
70 throw new ArgumentNullException();
71
43 searchString = searchString.ToLower(); 72 searchString = searchString.ToLower();
44 73
45 var qry = from books in StockList 74 var qry = from books in StockList
46 where books.Book.Title.ToLower().Contains(searchString) 75 where books.Book.Title.ToLower().Contains(searchString)
47 || books.Book.Author.ToLower().Contains(searchString) 76 || books.Book.Author.ToLower().Contains(searchString)
48 select books; 77 select books;
49 78
50 return qry.ToList(); 79 return qry.ToList();
51 } 80 }
52 81
53 public void LendBook(IPerson person, IBook book) 82 public IBook GetBookById(string id)
54 { 83 {
55 throw new System.NotImplementedException(); 84 if (id == null)
85 throw new ArgumentNullException();
86
87 id = id.ToLower();
88
89 var qry = from books in StockList
90 where books.Book.Id.ToLower() == id
91 select books.Book;
92
93 return qry.FirstOrDefault();
94 }
95
96
97 public void LendBook(IPerson person, string id)
98 {
99 if (person == null || id == null)
100 throw new ArgumentNullException();
101
102 var stock = ThrowExceptionIfBookIsNotInStock(id);
103 stock.IsAvailable = false;
104
105 // FUTURE: register with person that they have borrowed the book
106 }
107
108 private IStock ThrowExceptionIfBookIsNotInStock(string id)
109 {
110 var lookup = ((List<IStock>) StockList).Find(s => s.Book.Id == id);
111 if (lookup == null)
112 throw new Exception(string.Format("Book with id {0} is not in stock", id));
113 return lookup;
56 } 114 }
57 } 115 }
58 } 116 }