Advertisement
Malnebi

MyLibrary

May 21st, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. package vjezba;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5.  
  6. public class MyLibrary {
  7.  
  8. ArrayList<Book> books = new ArrayList<>();
  9. ArrayList<User> users = new ArrayList<>();
  10. Date date = new Date();
  11.  
  12. public MyLibrary() {
  13. }
  14.  
  15. public ArrayList<Book> getBooks() {
  16. return books;
  17. }
  18.  
  19. public ArrayList<User> getUsers() {
  20. return users;
  21. }
  22.  
  23. public void addBook(Book book) {
  24. this.books.add(book);
  25. }
  26.  
  27. public void addUser(User user) {
  28. this.users.add(user);
  29. }
  30.  
  31. public void removeBook(Book book) {
  32. this.books.remove(book);
  33. }
  34.  
  35. public void removeUser(User user) {
  36. this.users.remove(user);
  37. }
  38.  
  39. /** izdavanje knjige korisniku */
  40. public void checkOutBook(Book book, User user) {
  41. int numberUsersBooks = this.getBooksWithUser(user).size();
  42. if (book.getUser() == null){
  43. if(numberUsersBooks < user.getMaxBooks()) {
  44. this.date = new Date();
  45. book.setUser(user);
  46. System.out.println("The book \"" + book.getTitle()
  47. + "\" is now checked out to " + book.getUser().getName()+". \n Date checking out:" +date);
  48. }
  49. else{System.out.println(user.getName()+" allready has "+user.getMaxBooks()+" books and it is not aloved to borrow more!" );}
  50. } else
  51. System.out.println("The book \"" + book.getTitle()
  52. + "\" is allready checked out to "
  53. + book.getUser().getName()+", so it can't be lend to "+user.getName());
  54. }
  55.  
  56. /** vracanje knjige u biblioteku */
  57. public void checkInBook(Book book) {
  58. if (book.getUser() != null)
  59. book.setUser(null);
  60. else
  61. System.out.println("The book \"" + book.getTitle()
  62. + "\" is allready checkd in!");
  63. // knjiga je vracena
  64. }
  65.  
  66. /** lista knjiga kod jednog korisnika */
  67. public ArrayList<Book> getBooksWithUser(User user) {
  68. ArrayList<Book> usersBooks = new ArrayList<>();
  69.  
  70. for (Book someBook : this.getBooks()) {
  71. if ((someBook.getUser() != null)
  72. && (someBook.getUser().getUserID() == (user.getUserID()))) {
  73. usersBooks.add(someBook);
  74. }
  75. }
  76. return usersBooks;
  77. }
  78.  
  79. /** lista Dostupnih knjiga */
  80. public ArrayList<Book> getAvaliableBooks() {
  81. ArrayList<Book> avaliableBooks = new ArrayList<>();
  82. for (Book book : this.getBooks()) {
  83. if (book.getUser() == null)
  84. avaliableBooks.add(book);
  85. }
  86. return avaliableBooks;
  87. }
  88.  
  89. /** lista NEdostupnih knjiga */
  90. public ArrayList<Book> getUnavaliableBooks() {
  91. ArrayList<Book> unAvaliableBooks = new ArrayList<>();
  92. for (Book book : this.getBooks()) {
  93. if (book.getUser() != null)
  94. unAvaliableBooks.add(book);
  95. }
  96. return unAvaliableBooks;
  97. }
  98.  
  99. @Override
  100. public String toString() {
  101. return "BOOKS: " + Book.getIdBook() + "\n " + books + "\n USERS: "
  102. + User.getIdUser() + " \n" + users + "\n";
  103. }
  104.  
  105. public void printStatus() {
  106. System.out
  107. .println("------------------LIBRARY STATUS REPORT-------------");
  108. System.out.println(this.toString());
  109.  
  110. System.out.println("Number of avaliable books: "
  111. + this.getAvaliableBooks().size());
  112. for (Book book : this.getAvaliableBooks()) {
  113. System.out.println(book.toString());
  114. }
  115.  
  116. System.out.println("Number of unavaliable books: "
  117. + this.getUnavaliableBooks().size());
  118. for (Book book : this.getUnavaliableBooks()) {
  119. System.out.println(book.toString() + " checked out to " + book.getUser().getName()
  120. + " on " + date);
  121. }
  122.  
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement