-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibrary.java
201 lines (182 loc) · 6.36 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* @author lioraryepaz
* This class represents a Library, which has various limitations, and foolow up on its books and patrons.
*/
class Library {
/**
* The maximal number of books this library can hold
*/
final int maxBookCapacity;
/**
* The maximal number of books this library allows a single patron to borrow at the same time
*/
int maxBorrowedBooks;
/**
* The maximal number of registered patrons this library can handle
*/
int maxPatronCapacity;
/**
* Books array - index is bookid
*/
Book[] librarybooksArray;
/**
* Patrons array - index is patronid
*/
Patron[] librarypatronsArray;
/**
* follow up on how much books each patron has according to index
*/
int[] borrowedbyPatrons;
/**
* a counter of books registered to the library
*/
int booksInlibrary = 0;
/**
* a counter of patrons registered to the library
*/
int patronsInlibrary = 0;
Library(int libraryMaxBookCapacity, int libraryMaxBorrowedBooks, int libraryMaxPatronCapacity) {
maxBookCapacity = libraryMaxBookCapacity;
maxBorrowedBooks = libraryMaxBorrowedBooks;
maxPatronCapacity = libraryMaxPatronCapacity;
librarybooksArray = new Book[maxBookCapacity];
librarypatronsArray = new Patron[maxPatronCapacity];
borrowedbyPatrons = new int[maxPatronCapacity];
}
/**
* @return a non-negative id number for the book if there was a spot and the book was successfully added, or if the
* book was already in the library; a negative number otherwise.
*/
int addBookToLibrary(Book book) {
for (int i = 0; i < librarybooksArray.length; i++) {
if (librarybooksArray[i] == book) {
return i;
}
}
if (booksInlibrary < maxBookCapacity) {
librarybooksArray[booksInlibrary] = book;
booksInlibrary += 1;
return booksInlibrary - 1;
} else {
return -1;
}
}
boolean isBookIdValid(int bookId) {
if (bookId > maxBookCapacity) {
return false;
} else {
return (librarybooksArray[bookId] != null);
}
}
int getBookId(Book book) {
for (int i = 0; i < librarybooksArray.length; i++) {
if (librarybooksArray[i] == book) {
return i;
}
}
return -1;
}
boolean isBookAvailable(int bookId) {
if (isBookIdValid(bookId)) {
return (librarybooksArray[bookId].getCurrentBorrowerId() == -1);
} else {
return false;
}
}
/**
* @return a non-negative id number for the patron if there was a spot and the patron was successfully registered,
* a negative number otherwise.
*/
int registerPatronToLibrary(Patron patron) {
for (int i = 0; i < librarypatronsArray.length; i++) {
if (librarypatronsArray[i] == patron) {
return i;
}
}
if (patronsInlibrary < maxPatronCapacity) {
librarypatronsArray[patronsInlibrary] = patron;
patronsInlibrary += 1;
return patronsInlibrary - 1;
} else {
return -1;
}
}
boolean isPatronIdValid(int patronId) {
if (patronId > maxPatronCapacity) {
return false;
} else {
return (librarypatronsArray[patronId] != null);
}
}
int getPatronId(Patron patron) {
for (int i = 0; i < librarypatronsArray.length; i++) {
if (librarypatronsArray[i] == patron) {
return i;
}
}
return -1;
}
/**
* Marks the book with the given id number as borrowed by the patron with the given patron id, if this book is
* available, the given patron isn't already borrowing the maximal number of books allowed, and if the patron will
* enjoy this book.
*
* @return true if the book was borrowed successfully, false otherwise.
*/
boolean borrowBook(int bookId, int patronId) {
if ((isBookIdValid(bookId)) && (isPatronIdValid(patronId))) {
Book tempBook = librarybooksArray[bookId];
Patron tempPatron = librarypatronsArray[patronId];
if (tempBook.getCurrentBorrowerId() != -1) {
return false;
} else {
if (borrowedbyPatrons[patronId] >= maxBorrowedBooks) {
return false;
} else {
if (tempPatron.willEnjoyBook(librarybooksArray[bookId])) {
tempBook.setBorrowerId(patronId);
borrowedbyPatrons[patronId] += 1;
return true;
} else {
return false;
}
}
}
} else return false;
}
void returnBook(int bookId) {
if (!isBookIdValid(bookId)) {
return;
}
Book tempBook = librarybooksArray[bookId];
int tempPatronid = tempBook.getCurrentBorrowerId();
borrowedbyPatrons[tempPatronid] -= 1;
tempBook.setBorrowerId(-1);
}
/**
* Suggest the patron with the given id the book he will enjoy the most, out of all available books he will enjoy,
* if any such exist.
*
* @param patronId
* @return The available book the patron with the given will enjoy the most. Null if no book is available
*/
Book suggestBookToPatron(int patronId) {
if (!isPatronIdValid(patronId)) {
return null;
}
Patron tempPatron = librarypatronsArray[patronId];
int[] suggestionList = new int[maxBookCapacity];
Book suggestBook = null;
int answer = 0;
for (Book tempBook : librarybooksArray) {
if ((tempBook.getCurrentBorrowerId() == -1) && (tempPatron.willEnjoyBook(tempBook))) {
int tempScore = tempPatron.getBookScore(tempBook);
if (tempScore > answer) {
answer = tempScore;
suggestBook = tempBook;
}
}
}
return suggestBook;
}
}