-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatorLibrary.java
More file actions
281 lines (251 loc) · 9.82 KB
/
gatorLibrary.java
File metadata and controls
281 lines (251 loc) · 9.82 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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class gatorLibrary{
private static RedBlackTree library;
private static PrintWriter output;
private static boolean verbose = false;
/**
* Print the informaiton of the book with given bookId
*/
private static void printBook(int bookId){
Book book = library.getBook(bookId);
if (book != null)
writeToOut(
book.toString()
);
else
writeToOut(
"Book " + bookId + " not found in the Library"
);
}
/**
* Prints all the books in the given range of bookIds
*/
private static void printBooks(int bookId1, int bookId2){
boolean found = false; // Var to check if books found in the given range
for (Book book: library.getBooks(bookId1, bookId2)){
found = true;
writeToOut(book.toString());
}
// When there are no books in the given range
if (!found && verbose)
System.out.println("No books found in the range [" + bookId1 + ", " + bookId2 + "]\n");
}
/**
* Helper method to execute BorrowBook() command
* Let the partron with patronId borrow the book bookId
* or adds the patron to the reservation queue of the book with given priority
*/
private static void borrowBook(int patronId, int bookId, int priority){
// Get the book from the library
Book book = library.getBook(bookId);
if (book == null){
writeToOut(
"Book " + bookId + " not found in the Library"
);
return;
}
// Allot to the patron if available
if (book.available) {
book.available = false;
book.borrowedBy = patronId;
writeToOut(
"Book " + bookId + " Borrowed by Patron " + patronId
);
// Add patron to the reservation queue otherwise
} else {
book.reservationHeap.put(patronId, priority);
writeToOut(
"Book " + bookId + " Reserved by Patron " + patronId
);
}
}
/**
* Helper method to implement ReturnBook() command
* Updates the book's availability status
* Allots the book to top priority in the reservation if available
*/
private static void returnBook(int patronId, int bookId){
// Search and get the book with given Id
Book book = library.getBook(bookId);
// Check if the book is available in the library
if (book == null){
writeToOut(
"Book " + bookId + " not found in the Library"
);
return;
}
// Set the availability status
book.available = true;
writeToOut(
"Book " + bookId + " Returned by Patron " + patronId
);
// If there are patrons waiting in the reservation queue
// Allot the book to them
book.borrowedBy = book.reservationHeap.pop();
if (book.borrowedBy != -1) {
book.available = false;
writeToOut(
"Book " + bookId + " Allotted to Patron " + book.borrowedBy
);
}
}
/**
* Helper method for DeleteBook()
* Deletes the book if present in the tree
* Prints the confirmation along with the reservation heap of the book
* */
private static void deleteBook(int bookId){
// Delete the book from the tree
Book book = library.delete(bookId);
if (book == null){
writeToOut(
"Book " + bookId + " not found in the Library"
);
return;
}
// Print the status of deletion
String out = "Book " + book.id + " is no longer available";
if (book.reservationHeap.size() == 1)
out += ". Reservation made by Patron " + book.reservationHeap + " has been cancelled!";
if (book.reservationHeap.size() > 1)
out += ". Reservations made by Patrons " + book.reservationHeap + " have been cancelled!";
writeToOut(out);
}
/**
* Helper method to implement FindClosest() command
* Finds atmost 2 books with ids nearest to the given targetId and prints
*/
private static void findClosest(int targetId) {
Book[] nearest = library.findNearest(targetId);
if (nearest[0] == null){
System.out.println("No Books in the Library yet!\n");
return;
}
// In case of tie, print both in the id order
if (nearest[1] != null && Math.abs(nearest[0].id - targetId) == Math.abs(nearest[1].id - targetId)){
if (nearest[1].id > nearest[0].id){
writeToOut(nearest[0].toString());
writeToOut(nearest[1].toString());
} else {
writeToOut(nearest[1].toString());
writeToOut(nearest[0].toString());
}
return;
}
writeToOut(nearest[0].toString());
}
/**
* Start of the program
* @param args Expects inputfile path from the command line
*/
public static void main(String[] args){
long startTime = System.currentTimeMillis(); // Optional timer for inference
try {
if (args.length < 1){
System.err.println("Usage: `java gatorLibrary file_name.txt`");
return;
}
if (args.length == 2) verbose = true;
String inputFile = args[0];
if (!inputFile.endsWith(".txt")){
System.err.println("The input file must be a text file (ending with .txt)!");
return;
}
String outputFile = inputFile.substring(0, inputFile.length() - 4) + "_output_file.txt";
Scanner input = new Scanner(new File(inputFile));
output = new PrintWriter(new FileWriter(outputFile));
library = new RedBlackTree();
String cmd, method = "";
// Exit when a Quit() is found in the File
while (!method.equals("Quit") && input.hasNextLine()){
cmd = input.nextLine();
if (verbose) System.out.println("Command read: " + cmd);
// Divide the input text into method and parameters
method = cmd.substring(0, cmd.indexOf("("));
String[] params = cmd.substring(cmd.indexOf("(") + 1, cmd.indexOf(")")).split(", ?");
// Execute based on the input method provided
switch (method) {
case "InsertBook":
library.insert(
new Book(
Integer.parseInt(params[0]),
params[1],
params[2],
params[3].equals("\"Yes\"")
)
);
if (verbose) System.out.println(params[1] + " is added to the library!\n");
break;
case "PrintBook":
printBook(Integer.parseInt(params[0]));
break;
case "PrintBooks":
printBooks(
Integer.parseInt(params[0]),
Integer.parseInt(params[1])
);
break;
case "BorrowBook":
borrowBook(
Integer.parseInt(params[0]),
Integer.parseInt(params[1]),
Integer.parseInt(params[2])
);
break;
case "ReturnBook":
returnBook(
Integer.parseInt(params[0]),
Integer.parseInt(params[1])
);
break;
case "DeleteBook":
deleteBook(Integer.parseInt(params[0]));
break;
case "FindClosestBook":
findClosest(Integer.parseInt(params[0]));
break;
case "ColorFlipCount":
writeToOut("Color Flip Count: " + library.getColorFlipCount());
break;
default:
break;
}
}
// Check if Quit() is in the input
if (!method.equals("Quit")){
if (verbose) System.out.println("'Quit()' is not in input!");
} else
output.print("Program Terminated!!");
input.close();
output.close();
} catch (ArrayIndexOutOfBoundsException e){
/**
* Handle Index out of Bounds Exception
* Occurs when the parameters in input file are specified wrongly!
*/
System.err.println("Check the number of parameters!");
} catch (FileNotFoundException e){
System.out.println(e);
return;
} catch (IOException e){
e.printStackTrace();
} catch (Exception e){
System.err.println("Invalid input in the text file!");
}
long endTime = System.currentTimeMillis();
if (verbose) System.out.printf("Execution time: %.3fs\n", (endTime - startTime) / 1000.0);
}
/**
* Helper method to write the given text to output file and teh stdout
* @param text text to be written to the output
*/
private static void writeToOut(String text){
output.println(text + "\n");
if (verbose) System.out.println(text + "\n");
}
}