-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibrarian.java
More file actions
320 lines (249 loc) · 14.1 KB
/
Copy pathLibrarian.java
File metadata and controls
320 lines (249 loc) · 14.1 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package lms;
import java.sql.Connection;
/**
* Collection of functions for librarians
* @author DannyGalaxy
*
*/
public class Librarian {
/*ADDITIONAL FUNCTION
* UPDATE PATRON FINE (Manual input value)
*/
// just a place to organize/store our functions
// no instance variables
// no static variables
// no constructors
/**
* Calls addToTable() to construct INSERT INTO query to add a book to the 'books' table
* @param connection Connection (Object) to use for database
* @param table Name of table to be queried (in this case it should always be `books`)
* @param title Title of the book to be added
* @param author Author of the book to be added
* @param genre Genre of the book to be added
*/
public static void addBook(Connection connection, String table, String title, String author, String genre) {
// create String array to pass into the INSERT INTO query as values
String[] values = {title, author, genre};
// Call addToTable() with value array
DatabaseQueries.addToTable(connection, table, values);
}
/**
* Calls removeFromTable() to construct a DELETE query which removes a book from a table, given an ID
* @param connection Connection (Object) to use for database
* @param table Name of table to be queried
* @param bookID ID of the book to remove. This should be acquired through a separate function that retrieves available IDs
* given the composite key of author and title
*/
public static void removeBook(Connection connection, String table, int bookID) {
// Create the keyword, or the column we are matching against our ID
String keyword = "book_ID";
// Create the criterion, which is the specific ID we are passing in. Cast it to a string to work with the generalized
// removeFromTable method
String criterion = Integer.toString(bookID);
// Call removeFromTable to construct the DELETE query
DatabaseQueries.removeFromTable(connection, table, keyword, criterion);
}
/**
* Calls removeBook() to remove book from all tables in database
* @param connection Connection (Object) to use for database
* @param bookID ID of the book to remove. This should be acquired through a separate function that retrieves available IDs
* given the composite key of author and title
*/
public static void removeBookFromAllTables(Connection connection, int bookID) {
// Remove book from holds and checkouts tables first, then remove from books table
removeBook(connection, "holds" , bookID);
removeBook(connection, "checkouts" , bookID);
removeBook(connection, "books" , bookID);
}
/**
* Calls removeFromTable() to construct a DELETE query which removes a patron from the 'patrons' table, given an ID
* Note: This method could be combined with removeBook fairly easily (by just adding a String keyword parameter), but we
* thought it was a bit cleaner for LibraryManagementSystem and for JavaDocs if we just had separate methods
* @param connection Connection (Object) to use for database
* @param table Name of table to be queried (in this case it should always be `patrons`)
* @param patronID ID of the patron to remove. This should be acquired through a separate function that retrieves available IDs
* given the composite key of first and last name
*/
public static void removePatron(Connection connection, String table, int patronID) {
// Create the keyword, or the column we are matching against our ID
String keyword = "patron_ID";
// Create the criterion, which is the specific ID we are passing in. Cast it to a string to work with the generalized
// removeFromTable method
String criterion = Integer.toString(patronID);
// Call removeFromTable to construct the DELETE query
DatabaseQueries.removeFromTable(connection, table, keyword, criterion);
}
/**
* Calls removePatron() to remove patron from all tables in database
* @param connection Connection (Object) to use for database
* @param patronID ID of the patron to remove. This should be acquired through a separate function that retrieves available IDs
* given the composite key of first and last name
*/
public static void removePatronFromAllTables(Connection connection, int patronID) {
// Remove from holds and checkouts tables first, then remove from patrons table
removeBook(connection, "holds" , patronID);
removeBook(connection, "checkouts" , patronID);
removeBook(connection, "patrons" , patronID);
}
/**
* Constructs SELECT query to get a list of holds
* @param connection Connection (Object) to use for database
* @param holdsView view from which to query (should always be 'holdsview')
* @param keyword column on which to match. Leave out this and criterion to select everything.
* @param criterion criterion to match on. Leave out this and keyword to select everything.
*/
public static void getHoldsList(Connection connection, String holdsView, String keyword, String criterion) {
//SELECT (Whateverwesaid) where keyword = criterion
// default values are for getting all holds or checkouts (not based on a search criterion)
// if keyword is default, then use a Select *
// otherwise
/*
LIST HOLDS view: (we want all columns)
Title | Book ID | Patron | Patron ID | Request Date
*/
// 12/07 addition
// *** SORT by Patron then Author then Title ****
// Construct query string
String getHoldsQuery = "SELECT *"
+ " FROM " + holdsView
+ " WHERE " + keyword + "= \"" + criterion + "\" "
+ " ORDER BY firstName, lastName, author, title;";
// Create array of columns that will be returned (to eventually pass into printResult())
String[] returnColumns = {"book_ID", "title", "author", "checkedOut", "requestDate", "patron_ID", "firstName", "lastName"};
// read from db; calls printResults method (results displayed in window)
DatabaseQueries.printFromDatabase(connection, getHoldsQuery, returnColumns);
}
/**
* Overloaded getHoldsList, with default keyword and criterion (use to select all).
* Constructs SELECT query to get a list of holds
* @param connection Connection (Object) to use for database
* @param holdsView view from which to query (should always be 'holdsview')
*/
public static void getHoldsList(Connection connection, String holdsView) {
//SELECT (Whateverwesaid) where keyword = criterion
// default values are for getting all holds or checkouts (not based on a search criterion)
// if keyword is default, then use a Select *
// otherwise
/*
LIST HOLDS view: (we want all columns)
Title | Book ID | Patron | Patron ID | Request Date
*/
// 12/07 addition
// *** SORT by Patron then Author then Title ****
// Construct query string
String getHoldsQuery = "SELECT *"
+ " FROM " + holdsView
+ " ORDER BY firstName, lastName, author, title;";
// Create array of columns that will be returned (to eventually pass into printResult())
String[] returnColumns = {"book_ID", "title", "author", "checkedOut", "requestDate", "patron_ID", "firstName", "lastName"};
// read from db; calls printResults method (results displayed in window)
DatabaseQueries.printFromDatabase(connection, getHoldsQuery, returnColumns);
}
/**
* Constructs SELECT query to get a list of checked out books, along with their return due dates, potential days late, and fine amounts
* @param connection Connection (Object) to use for database
* @param checkOutsView view from which to query (should always be 'checkoutsview')
*/
public static void getCheckOutsList(Connection connection, String checkOutsView, String keyword, String criterion) {
// keyword and criterion allow for filtering by title, author, patron
/* *** view--> built in database by double join; select query of view
LIST CHECK-OUTS view:
1 patron_ID, 2firstName, 3lastName, 4book_ID, 5title, 6onHold, 7checkOutDate, 8dailyFineAmount, 9dueDate, 10daysLate, 11fineAmount
desiredColumns = {1,2,3,4,5,6,7,9,10,11}
// SO, this method may actually end up being the same as the getOverdue where we end up passing the keyword and criterion
// and for overdues, the keyword is overdues or whwatever the name of the view is; and the criterion is ""/null
^^ when we ask for check outs or overdue, we'll just pass the column numbers we want [hard coded] (see below)
*/
// Construct query string
String getCheckoutsQuery = "SELECT *"
+ " FROM " + checkOutsView
+ " WHERE " + keyword + " = \"" + criterion + "\" "
+ " ORDER BY firstName, lastName, author, title;";
// Create array of columns that will be returned (to eventually pass into printResult())
String[] returnColumns = {"patron_ID", "firstName", "lastName", "book_ID", "title", "author",
"onHold", "checkOutDate", "dailyFineAmount", "dueDate", "daysLate", "fineAmount"};
// read from db; calls printResults method (results displayed in window)
DatabaseQueries.printFromDatabase(connection, getCheckoutsQuery, returnColumns);
}
/**
* Overloaded getCheckOutsList, with default keyword and criterion (use to select all)
* Constructs SELECT query to get a list of checked out books, along with their return due dates, potential days late, and fine amounts
* @param connection Connection (Object) to use for database
* @param checkOutsView view from which to query (should always be 'checkoutsview')
* @param keyword column on which to match. Leave out this and criterion to select everything.
* @param criterion criterion to match on. Leave out this and keyword to select everything.
*/
public static void getCheckOutsList(Connection connection, String checkOutsView) {
// keyword and criterion allow for filtering by title, author, patron
/* *** view--> built in database by double join; select query of view
LIST CHECK-OUTS view:
1 patron_ID, 2firstName, 3lastName, 4book_ID, 5title, 6onHold, 7checkOutDate, 8dailyFineAmount, 9dueDate, 10daysLate, 11fineAmount
desiredColumns = {1,2,3,4,5,6,7,9,10,11}
// SO, this method may actually end up being the same as the getOverdue where we end up passing the keyword and criterion
// and for overdues, the keyword is overdues or whwatever the name of the view is; and the criterion is ""/null
^^ when we ask for check outs or overdue, we'll just pass the column numbers we want [hard coded] (see below)
*/
// Construct query string
String getCheckoutsQuery = "SELECT *"
+ " FROM " + checkOutsView
+ " ORDER BY firstName, lastName, author, title;";
// Create array of columns that will be returned (to eventually pass into printResult())
String[] returnColumns = {"patron_ID", "firstName", "lastName", "book_ID", "title", "author",
"onHold", "checkOutDate", "dailyFineAmount", "dueDate", "daysLate", "fineAmount"};
// read from db; calls printResults method (results displayed in window)
DatabaseQueries.printFromDatabase(connection, getCheckoutsQuery, returnColumns);
}
/**
* Constructs SELECT query to get all overdue books
* @param connection Connection (Object) to use for database
* @param checkOutsView view from which to query (should always be 'checkoutsview')
* @param keyword column on which to match. Leave out this and criterion to select everything.
* @param criterion criterion to match on. Leave out this and keyword to select everything.
*/
public static void getOverdueList(Connection connection, String checkOutsView, String keyword, String criterion) { // special case filtering of above based on date???
// SELECT from VIEW
// goal: list of over due books
/* display the following columns from the CheckOutsList view
* where daysLate > 0
* desiredColums = all but 6 desiredColumns = {1,2,3,4,5,7,9,10,11}
* *** unless we group by patron_ID and just get the total that each patron owes?
*/
// Construct query string
String getOverdueQuery = "SELECT patron_ID, firstName, lastName, book_ID, title, author,"
+ " checkOutDate, dailyFineAmount, dueDate, daysLate, fineAmount"
+ " FROM " + checkOutsView
+ " WHERE daysLate > 0 && " + keyword + " = \"" + criterion + "\""
+ " ORDER BY firstName, lastName, author, title;";
// Create array of columns that will be returned (to eventually pass into printResult())
String[] returnColumns = {"patron_ID", "firstName", "lastName", "book_ID", "title", "author",
"checkOutDate", "dailyFineAmount", "dueDate", "daysLate", "fineAmount"};
// read from db; calls printResults method (results displayed in window)
DatabaseQueries.printFromDatabase(connection, getOverdueQuery, returnColumns);
}
/**
* Overloaded getOverdueList method to get all overdue books
* Constructs SELECT query to get all overdue books
* @param connection Connection (Object) to use for database
* @param checkOutsView view from which to query (should always be 'checkoutsview')
*/
public static void getOverdueList(Connection connection, String checkOutsView) { // special case filtering of above based on date???
// SELECT from VIEW
// goal: list of over due books
/* display the following columns from the CheckOutsList view
* where daysLate > 0
* desiredColums = all but 6 desiredColumns = {1,2,3,4,5,7,9,10,11}
* *** unless we group by patron_ID and just get the total that each patron owes?
*/
// Construct query string
String getOverdueQuery = "SELECT patron_ID, firstName, lastName, book_ID, title, author,"
+ " checkOutDate, dailyFineAmount, dueDate, daysLate, fineAmount"
+ " FROM " + checkOutsView
+ " WHERE daysLate > 0"
+ " ORDER BY firstName, lastName, author, title;";
// Create array of columns that will be returned (to eventually pass into printResult())
String[] returnColumns = {"patron_ID", "firstName", "lastName", "book_ID", "title", "author",
"checkOutDate", "dailyFineAmount", "dueDate", "daysLate", "fineAmount"};
// read from db; calls printResults method (results displayed in window)
DatabaseQueries.printFromDatabase(connection, getOverdueQuery, returnColumns);
}
}