-
Notifications
You must be signed in to change notification settings - Fork 2
/
Library.java
154 lines (138 loc) · 4.7 KB
/
Library.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package system;
import exceptions.BookNotFoundException;
import exceptions.DuplicateException;
import exceptions.UserNotFoundException;
import args.Book;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* The Library class is an implementation of the IRemoteLibrary interface, which
* manages a collection of items. Items can be added or removed.
*
* @author Joris Schelfaut
*/
public class Library {
private final List<Book> books;
private final List<User> users;
/**
* Instantiates a new Library object.
*/
public Library() {
super();
this.books = new ArrayList<>();
this.users = new ArrayList<>();
}
/**
* @param book the book to be added to the library.
* @throws NullPointerException if the book resolves as NULL.
* @throws DuplicateException if the book (by this owner) was already in the library.
*/
public synchronized void addBook (Book book)
throws NullPointerException, DuplicateException {
if (book == null) throw new NullPointerException("The book resolved as NULL.");
if (this.books.contains(book)) throw new DuplicateException(book);
this.books.add(book);
}
/**
* @param book the book to be removed.
*/
public void removeBook (Book book) {
this.books.remove(book);
}
/**
* @param isbn the ISBN of the book that will be removed.
* @param owner the owner of the book that will be removed.
*/
public void removeBook (String isbn, String owner) {
for (Book b : this.books) {
if (b.getISBN().equals(isbn) && b.getOwner().equalsIgnoreCase(owner))
this.books.remove(b);
}
}
/**
* @param isbn the ISBN of the book.
* @param owner the owner of the book.
* @return the book with given owner and ISBN.
* @throws BookNotFoundException
*/
public Book lookupBook (String isbn, String owner) throws BookNotFoundException {
for (Book b : this.books) {
if (b.getISBN().equals(isbn) && b.getOwner().equalsIgnoreCase(owner))
return b;
}
throw new BookNotFoundException(isbn);
}
/**
* @return all the books in this library.
*/
public List<Book> getBooks() {
return Collections.unmodifiableList(this.books);
}
/**
* @param user the user to add to the library.
* @throws NullPointerException
* @throws DuplicateException
*/
public synchronized void addUser(User user)
throws NullPointerException, DuplicateException {
if (user == null) throw new NullPointerException("The user resolved as NULL.");
if (this.users.contains(user))
throw new DuplicateException("The user with name '" + user.getName()
+ "' is already in the library.");
this.users.add(user);
}
/**
* @return the users of the library.
*/
public List<User> getUsers() {
return Collections.unmodifiableList(this.users);
}
/**
* @param name the name of the user to look for in the library.
* @return the user with given name.
* @throws UserNotFoundException
*/
public User lookupUser (String name) throws UserNotFoundException {
for (User u : this.users) {
if (u.getName().equals(name)) return u;
}
throw new UserNotFoundException(name);
}
/**
* @param user the user to be removed.
*/
public void removeUser (User user) {
this.users.remove(user);
}
/**
* @param name the name of the user to be removed.
*/
public void removeUser (String name) {
for (User u : this.users) {
if (u.getName().equals(name)) this.users.remove(u);
}
}
/**
* @param isbn the ISBN of the book.
* @return the list of names of owners of the book with given ISBN.
*/
public List<String> getOwnersForBook(String isbn) {
List<String> owners = new ArrayList<>();
for (Book b : this.books) {
if (b.getISBN().equals(isbn)) owners.add(b.getOwner());
}
return owners;
}
/**
* @param owner the owners to get the books from.
* @return the list of books owned by a user given name.
*/
public List<Book> getBooksForOwner(String owner) {
List<Book> booklist = new ArrayList<>();
for (Book b : this.books) {
if (b.getOwner().equals(owner)) booklist.add(b);
}
return booklist;
}
}