- Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathBook.java
More file actions
Latest commit
322 lines (285 loc) · 8.96 KB
/
Copy pathBook.java
File metadata and controls
322 lines (285 loc) · 8.96 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
322
importjava.util.Date;
importjava.text.SimpleDateFormat;
/**
* Book Class for Library Management System
* Author: GitHub Copilot
* Description: Represents a book with all its properties and borrowing status
*/
publicclassBook {
// Private fields to store book information
privateintid;
privateStringtitle;
privateStringauthor;
privateStringgenre;
privateintpublicationYear;
privatebooleanborrowed;
privateStringborrowerName;
privateDateborrowDate;
privateDateaddedDate;
// Static formatter for dates
privatestaticSimpleDateFormatdateFormat = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* Constructor to create a new book
* @param id The unique ID for the book
* @param title The book title
* @param author The author name
* @param genre The book genre/category
* @param publicationYear The year the book was published
*/
publicBook(intid, Stringtitle, Stringauthor, Stringgenre, intpublicationYear) {
this.id = id;
this.title = title;
this.author = author;
this.genre = genre;
this.publicationYear = publicationYear;
this.borrowed = false;
this.borrowerName = null;
this.borrowDate = null;
this.addedDate = newDate();
}
/**
* Gets the book ID
* @return The book ID
*/
publicintgetId() {
returnid;
}
/**
* Gets the book title
* @return The book title
*/
publicStringgetTitle() {
returntitle;
}
/**
* Sets the book title
* @param title The new title
*/
publicvoidsetTitle(Stringtitle) {
this.title = title;
}
/**
* Gets the author name
* @return The author name
*/
publicStringgetAuthor() {
returnauthor;
}
/**
* Sets the author name
* @param author The new author name
*/
publicvoidsetAuthor(Stringauthor) {
this.author = author;
}
/**
* Gets the book genre
* @return The book genre
*/
publicStringgetGenre() {
returngenre;
}
/**
* Sets the book genre
* @param genre The new genre
*/
publicvoidsetGenre(Stringgenre) {
this.genre = genre;
}
/**
* Gets the publication year
* @return The publication year
*/
publicintgetPublicationYear() {
returnpublicationYear;
}
/**
* Sets the publication year
* @param publicationYear The new publication year
*/
publicvoidsetPublicationYear(intpublicationYear) {
this.publicationYear = publicationYear;
}
/**
* Checks if the book is currently borrowed
* @return true if borrowed, false if available
*/
publicbooleanisBorrowed() {
returnborrowed;
}
/**
* Sets the borrowing status of the book
* @param borrowed The borrowing status
* @param borrowerName The name of the borrower (null if returning)
*/
publicvoidsetBorrowed(booleanborrowed, StringborrowerName) {
this.borrowed = borrowed;
this.borrowerName = borrowerName;
if (borrowed) {
this.borrowDate = newDate();
} else {
// When returning, keep the borrower name and date for history
// In a real system, you might want to keep a borrowing history
this.borrowDate = null;
}
}
/**
* Gets the name of the current borrower
* @return The borrower name, or null if not borrowed
*/
publicStringgetBorrowerName() {
returnborrowerName;
}
/**
* Gets the borrow date as formatted string
* @return The borrow date, or "Not borrowed" if available
*/
publicStringgetBorrowDate() {
if (borrowDate == null) {
return"Not borrowed";
}
returndateFormat.format(borrowDate);
}
/**
* Gets the date when the book was added to the library
* @return The formatted added date
*/
publicStringgetAddedDate() {
returndateFormat.format(addedDate);
}
/**
* Gets the raw borrow date object
* @return The borrow Date object, or null if not borrowed
*/
publicDategetBorrowDateObject() {
if (borrowDate == null) {
returnnull;
}
returnnewDate(borrowDate.getTime()); // Return copy for security
}
/**
* Gets the raw added date object
* @return The added Date object
*/
publicDategetAddedDateObject() {
returnnewDate(addedDate.getTime()); // Return copy for security
}
/**
* Calculates how many days the book has been borrowed
* @return Number of days borrowed, or 0 if not borrowed
*/
publiclonggetDaysBorrowed() {
if (!borrowed || borrowDate == null) {
return0;
}
Datenow = newDate();
longdiffInMillis = now.getTime() - borrowDate.getTime();
returndiffInMillis / (1000 * 60 * 60 * 24); // Convert to days
}
/**
* Checks if the book is overdue (borrowed for more than specified days)
* @param maxDays Maximum allowed borrowing days
* @return true if overdue, false otherwise
*/
publicbooleanisOverdue(intmaxDays) {
returnborrowed && getDaysBorrowed() > maxDays;
}
/**
* Gets the age of the book in years
* @return Age in years from publication to current year
*/
publicintgetBookAge() {
intcurrentYear = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
returncurrentYear - publicationYear;
}
/**
* Checks if the book matches a search term in title, author, or genre
* @param searchTerm The term to search for
* @return true if any field contains the search term
*/
publicbooleanmatches(StringsearchTerm) {
if (searchTerm == null || searchTerm.trim().isEmpty()) {
returnfalse;
}
Stringterm = searchTerm.toLowerCase();
returntitle.toLowerCase().contains(term) ||
author.toLowerCase().contains(term) ||
genre.toLowerCase().contains(term);
}
/**
* Gets the availability status as a formatted string
* @return Status string with icon
*/
publicStringgetAvailabilityStatus() {
if (borrowed) {
return"🔴 Borrowed by " + borrowerName;
} else {
return"🟢 Available";
}
}
/**
* Gets a short status icon
* @return Status icon
*/
publicStringgetStatusIcon() {
returnborrowed ? "🔴" : "🟢";
}
/**
* Creates a formatted summary of the book
* @return Formatted book summary
*/
publicStringgetSummary() {
Stringstatus = borrowed ? "🔴 Borrowed" : "🟢 Available";
returnString.format("[%d] %s - \"%s\" by %s (%s, %d)",
id, status, title, author, genre, publicationYear);
}
/**
* Creates a detailed description of the book
* @return Detailed book information
*/
publicStringgetDetailedInfo() {
StringBuilderinfo = newStringBuilder();
info.append("Book ID: ").append(id).append("\n");
info.append("Title: ").append(title).append("\n");
info.append("Author: ").append(author).append("\n");
info.append("Genre: ").append(genre).append("\n");
info.append("Publication Year: ").append(publicationYear).append("\n");
info.append("Age: ").append(getBookAge()).append(" years\n");
info.append("Status: ").append(getAvailabilityStatus()).append("\n");
info.append("Added to Library: ").append(getAddedDate()).append("\n");
if (borrowed) {
info.append("Borrowed Date: ").append(getBorrowDate()).append("\n");
info.append("Days Borrowed: ").append(getDaysBorrowed()).append("\n");
}
returninfo.toString();
}
/**
* Returns a string representation of the book
* @return String containing book information
*/
@Override
publicStringtoString() {
returnString.format("Book{id=%d, title='%s', author='%s', genre='%s', year=%d, borrowed=%s}",
id, title, author, genre, publicationYear, borrowed);
}
/**
* Checks if two books are equal based on their ID
* @param obj The object to compare with
* @return true if books have the same ID, false otherwise
*/
@Override
publicbooleanequals(Objectobj) {
if (this == obj) returntrue;
if (obj == null || getClass() != obj.getClass()) returnfalse;
Bookbook = (Book) obj;
returnid == book.id;
}
/**
* Generates hash code based on book ID
* @return Hash code
*/
@Override
publicinthashCode() {
returnInteger.hashCode(id);
}
}