-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook.java
More file actions
263 lines (224 loc) · 7.57 KB
/
Copy pathBook.java
File metadata and controls
263 lines (224 loc) · 7.57 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
package models;
import java.io.Serializable;
import java.util.Objects;
/**
* Book.java - Entity class representing a Library Book
*
* OOP Principles Applied:
* - Encapsulation: Private fields with public getters/setters
* - Immutability options: Can make certain fields final
* - Proper equals() and hashCode() for collections
*
* Design: Follows JavaBean conventions for database mapping
*/
public class Book implements Serializable, Comparable<Book> {
private static final long serialVersionUID = 1L;
// Core book information
private int bookId; // Auto-generated primary key
private String title; // Book title (required)
private String author; // Author name (required)
private String isbn; // ISBN unique identifier (required)
private String publisher; // Publishing company
private int publicationYear; // Year of publication
private String category; // Category/Genre
private double price; // Book price
// Inventory management
private int totalCopies; // Total copies in library
private int availableCopies; // Available for issue
// Constructors
/**
* No-argument constructor for framework compatibility
*/
public Book() {
this.bookId = 0;
this.totalCopies = 0;
this.availableCopies = 0;
}
/**
* Constructor for new book entry (bookId is auto-generated)
*/
public Book(String title, String author, String isbn, String publisher,
int publicationYear, int totalCopies, String category, double price) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.publisher = publisher;
this.publicationYear = publicationYear;
this.totalCopies = totalCopies;
this.availableCopies = totalCopies;
this.category = category;
this.price = price;
}
/**
* Full constructor with all fields (used when retrieving from DB)
*/
public Book(int bookId, String title, String author, String isbn,
String publisher, int publicationYear, int totalCopies,
int availableCopies, String category, double price) {
this.bookId = bookId;
this.title = title;
this.author = author;
this.isbn = isbn;
this.publisher = publisher;
this.publicationYear = publicationYear;
this.totalCopies = totalCopies;
this.availableCopies = availableCopies;
this.category = category;
this.price = price;
}
// Getters and Setters (Encapsulation)
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
if (bookId < 0) {
throw new IllegalArgumentException("Book ID must be positive");
}
this.bookId = bookId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
if (title == null || title.trim().isEmpty()) {
throw new IllegalArgumentException("Title cannot be empty");
}
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
if (author == null || author.trim().isEmpty()) {
throw new IllegalArgumentException("Author name cannot be empty");
}
this.author = author;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
if (isbn == null || isbn.trim().isEmpty()) {
throw new IllegalArgumentException("ISBN cannot be empty");
}
this.isbn = isbn;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher != null ? publisher : "";
}
public int getPublicationYear() {
return publicationYear;
}
public void setPublicationYear(int year) {
int currentYear = java.time.Year.now().getValue();
if (year < 1000 || year > currentYear) {
throw new IllegalArgumentException("Publication year must be between 1000 and " + currentYear);
}
this.publicationYear = year;
}
public int getTotalCopies() {
return totalCopies;
}
public void setTotalCopies(int totalCopies) {
if (totalCopies < availableCopies) {
throw new IllegalArgumentException("Total copies cannot be less than available copies");
}
this.totalCopies = totalCopies;
}
public int getAvailableCopies() {
return availableCopies;
}
public void setAvailableCopies(int availableCopies) {
if (availableCopies < 0 || availableCopies > totalCopies) {
throw new IllegalArgumentException("Available copies must be between 0 and " + totalCopies);
}
this.availableCopies = availableCopies;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category != null ? category : "General";
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
if (price < 0) {
throw new IllegalArgumentException("Price cannot be negative");
}
this.price = price;
}
// Business Logic Methods
/**
* Issues a book to a member (decrements available copies)
*
* @throws IllegalStateException if no copies available
*/
public void issueBook() {
if (availableCopies <= 0) {
throw new IllegalStateException("No copies available for '" + title + "'");
}
availableCopies--;
}
/**
* Returns a book from a member (increments available copies)
*
* @throws IllegalStateException if all copies already returned
*/
public void returnBook() {
if (availableCopies >= totalCopies) {
throw new IllegalStateException("All copies of '" + title + "' already returned");
}
availableCopies++;
}
/**
* Checks if book is available
*
* @return true if at least one copy available
*/
public boolean isAvailable() {
return availableCopies > 0;
}
// Object Methods
@Override
public String toString() {
return "Book{" +
"bookId=" + bookId +
", title='" + title + '\'' +
", author='" + author + '\'' +
", isbn='" + isbn + '\'' +
", publisher='" + publisher + '\'' +
", publicationYear=" + publicationYear +
", totalCopies=" + totalCopies +
", availableCopies=" + availableCopies +
", category='" + category + '\'' +
", price=" + price +
'}';
}
/**
* ISBN-based equality check
* Two books are equal if they have the same ISBN
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return Objects.equals(isbn, book.isbn);
}
@Override
public int hashCode() {
return Objects.hash(isbn);
}
/**
* Comparable implementation for sorting by title
*/
@Override
public int compareTo(Book other) {
return this.title.compareToIgnoreCase(other.title);
}
}