Guest User

Untitled

a guest
Feb 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. // Av Daniel Larsson och Martin Ekström
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace LibraryProject
  9. {
  10. /// <summary>
  11. /// A service that handles book copies, uses singleton pattern and can only be instantiated/accessed through GetInstance().
  12. /// </summary>
  13. class BookCopyService
  14. {
  15. private static BookCopyService instance;
  16. private Database db;
  17.  
  18. /// <summary>
  19. /// Event for when a book copy is added
  20. /// </summary>
  21. public event Action Updated = delegate { };
  22.  
  23. /// <summary>
  24. /// Default constructor
  25. /// </summary>
  26. private BookCopyService()
  27. {
  28. db = Database.GetInstance();
  29. }
  30.  
  31. /// <summary>
  32. /// Method to instantiate/access the book copy service.
  33. /// </summary>
  34. /// <returns>An instance of BookCopyService</returns>
  35. public static BookCopyService GetInstance()
  36. {
  37. if (instance == null)
  38. instance = new BookCopyService();
  39.  
  40. return instance;
  41. }
  42.  
  43. /// <summary>
  44. /// Method that add a single copy of a book
  45. /// </summary>
  46. /// <param name="book">Book of which the copy should be added</param>
  47. public void AddBookCopy(Book book)
  48. {
  49. Book_Copy copy = new Book_Copy
  50. {
  51. Fk_Books_Id = book.Id
  52. };
  53.  
  54. db.Book_Copies.AddObject(copy);
  55. db.SaveChanges();
  56.  
  57. Updated();
  58. }
  59.  
  60. /// <summary>
  61. /// Method to add a number of copies of a book
  62. /// </summary>
  63. /// <param name="book">Book of which copies should be added</param>
  64. /// <param name="nrOfBooks">Number of copies to add</param>
  65. public void AddBookCopies(Book book, int nrOfBooks)
  66. {
  67. for (int i = 0; i < nrOfBooks; i++)
  68. {
  69. AddBookCopy(book);
  70. }
  71. }
  72.  
  73. /// <summary>
  74. /// Method that returns the number of available copies
  75. /// </summary>
  76. /// <returns>Number of available copies</returns>
  77. public int GetTotalNrOfAvailableCopies()
  78. {
  79. int nrOfBookCopies = db.Book_Copies.Count();
  80. int nrOfLoans = db.Loans.Count();
  81. return nrOfBookCopies - nrOfLoans;
  82. }
  83.  
  84. }
  85. }
Add Comment
Please, Sign In to add comment