-
-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathBook.java
More file actions
321 lines (243 loc) · 10.2 KB
/
Book.java
File metadata and controls
321 lines (243 loc) · 10.2 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
321
package LMS;
import java.io.*;
import java.time.temporal.ChronoUnit;
import java.util.*;
public class Book {
private int bookID; // ID given by a library to a book to make it distinguishable from other books
private String title; // Title of a book
private String subject; // Subject to which a book is related!
private String author; // Author of book!
private boolean isIssued; // this will be true if the book is currently issued to some borrower.
private HoldRequestOperations holdRequests;
static int currentIdNumber = 0; //This will be unique for every book, since it will be incremented when everytime
//when a book is created
public Book(int id,String t, String s, String a, boolean issued) // Parameterise cons.
{
currentIdNumber++;
if(id==-1)
{
bookID = currentIdNumber;
}
else
bookID=id;
title = t;
subject = s;
author = a;
isIssued = issued;
this.holdRequests = holdRequests;
}
// printing all hold req on a book.
public void printHoldRequests()
{
if (!holdRequests.holdRequests.isEmpty())
{
System.out.println("\nHold Requests are: ");
System.out.println("---------------------------------------------------------------------------------------------------------------------------------------");
System.out.println("No.\t\tBook's Title\t\t\tBorrower's Name\t\t\tRequest Date");
System.out.println("---------------------------------------------------------------------------------------------------------------------------------------");
for (int i = 0; i < holdRequests.holdRequests.size(); i++)
{
System.out.print(i + "-" + "\t\t");
holdRequests.holdRequests.get(i).print();
}
}
else
System.out.println("\nNo Hold Requests.");
}
// printing book's Info
public void printInfo()
{
System.out.println(title + "\t\t\t" + author + "\t\t\t" + subject);
}
// changign Info of a Book
public void changeBookInfo() throws IOException
{
Scanner scanner = new Scanner(System.in);
String input;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nUpdate Author? (y/n)");
input = scanner.next();
if(input.equals("y"))
{
System.out.println("\nEnter new Author: ");
author = reader.readLine();
}
System.out.println("\nUpdate Subject? (y/n)");
input = scanner.next();
if(input.equals("y"))
{
System.out.println("\nEnter new Subject: ");
subject = reader.readLine();
}
System.out.println("\nUpdate Title? (y/n)");
input = scanner.next();
if(input.equals("y"))
{
System.out.println("\nEnter new Title: ");
title = reader.readLine();
}
System.out.println("\nBook is successfully updated.");
}
/*------------Getter FUNCs.---------*/
public String getTitle()
{
return title;
}
public String getSubject()
{
return subject;
}
public String getAuthor()
{
return author;
}
public boolean getIssuedStatus()
{
return isIssued;
}
public void setIssuedStatus(boolean s)
{
isIssued = s;
}
public int getID()
{
return bookID;
}
public ArrayList<HoldRequest> getHoldRequests()
{
return holdRequests.holdRequests;
}
/*-----------------------------------*/
// Setter Static Func.
public static void setIDCount(int n)
{
currentIdNumber = n;
}
//-------------------------------------------------------------------//
// Placing book on Hold
public void placeBookOnHold(Borrower bor)
{
HoldRequest hr = new HoldRequest(bor,this, new Date());
holdRequests.addHoldRequest(hr); //Add this hold request to holdRequests queue of this book
bor.addHoldRequest(hr); //Add this hold request to that particular borrower's class as well
System.out.println("\nThe book " + title + " has been successfully placed on hold by borrower " + bor.getName() + ".\n");
}
// Request for Holding a Book
public void makeHoldRequest(Borrower borrower)
{
boolean makeRequest = true;
//If that borrower has already borrowed that particular book. Then he isn't allowed to make request for that book. He will have to renew the issued book in order to extend the return deadline.
for(int i=0;i<borrower.getBorrowedBooks().size();i++)
{
if(borrower.getBorrowedBooks().get(i).getBook()==this)
{
System.out.println("\n" + "You have already borrowed " + title);
return;
}
}
//If that borrower has already requested for that particular book. Then he isn't allowed to make the same request again.
for (int i = 0; i < holdRequests.holdRequests.size(); i++)
{
if ((holdRequests.holdRequests.get(i).getBorrower() == borrower))
{
makeRequest = false;
break;
}
}
if (makeRequest)
{
placeBookOnHold(borrower);
}
else
System.out.println("\nYou already have one hold request for this book.\n");
}
// Getting Info of a Hold Request
public void serviceHoldRequest(HoldRequest hr)
{
holdRequests.removeHoldRequest();
hr.getBorrower().removeHoldRequest(hr);
}
// Issuing a Book
public void issueBook(Borrower borrower, Staff staff)
{
//First deleting the expired hold requests
Date today = new Date();
ArrayList<HoldRequest> hRequests = holdRequests.holdRequests;
for (int i = 0; i < hRequests.size(); i++)
{
HoldRequest hr = hRequests.get(i);
//Remove that hold request which has expired
long days = ChronoUnit.DAYS.between(today.toInstant(), hr.getRequestDate().toInstant());
days = 0-days;
if(days>Library.getInstance().getHoldRequestExpiry())
{
holdRequests.removeHoldRequest();
hr.getBorrower().removeHoldRequest(hr);
}
}
if (isIssued)
{
System.out.println("\nThe book " + title + " is already issued.");
System.out.println("Would you like to place the book on hold? (y/n)");
Scanner sc = new Scanner(System.in);
String choice = sc.next();
if (choice.equals("y"))
{
makeHoldRequest(borrower);
}
}
else
{
if (!holdRequests.holdRequests.isEmpty())
{
boolean hasRequest = false;
for (int i = 0; i < holdRequests.holdRequests.size() && !hasRequest;i++)
{
if (holdRequests.holdRequests.get(i).getBorrower() == borrower)
hasRequest = true;
}
if (hasRequest)
{
//If this particular borrower has the earliest request for this book
if (holdRequests.holdRequests.get(0).getBorrower() == borrower)
serviceHoldRequest(holdRequests.holdRequests.get(0));
else
{
System.out.println("\nSorry some other users have requested for this book earlier than you. So you have to wait until their hold requests are processed.");
return;
}
}
else
{
System.out.println("\nSome users have already placed this book on request and you haven't, so the book can't be issued to you.");
System.out.println("Would you like to place the book on hold? (y/n)");
Scanner sc = new Scanner(System.in);
String choice = sc.next();
if (choice.equals("y"))
{
makeHoldRequest(borrower);
}
return;
}
}
//If there are no hold requests for this book, then simply issue the book.
setIssuedStatus(true);
Loan iHistory = new Loan(borrower,this,staff,null,new Date(),null,false);
Library.getInstance().addLoan(iHistory);
borrower.addBorrowedBook(iHistory);
System.out.println("\nThe book " + title + " is successfully issued to " + borrower.getName() + ".");
System.out.println("\nIssued by: " + staff.getName());
}
}
// Returning a Book
public void returnBook(Borrower borrower, Loan l, Staff staff)
{
l.getBook().setIssuedStatus(false);
l.setReturnedDate(new Date());
l.setReceiver(staff);
borrower.removeBorrowedBook(l);
l.payFine();
System.out.println("\nThe book " + l.getBook().getTitle() + " is successfully returned by " + borrower.getName() + ".");
System.out.println("\nReceived by: " + staff.getName());
}
} // Book Class Closed