-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibraryManagementSystem.java
More file actions
241 lines (211 loc) · 7.1 KB
/
LibraryManagementSystem.java
File metadata and controls
241 lines (211 loc) · 7.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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
interface Borrowable {
void borrow(String user);
void returnBook();
boolean isBorrowed();
}
abstract class Item {
protected String id, title;
Item(String id, String title) {
this.id = id;
this.title = title;
}
abstract void displayDetails();
}
class InvalidBookException extends Exception {
public InvalidBookException(String message) {
super(message);
}
}
class Book extends Item implements Borrowable {
private String author;
private boolean isBorrowed;
Book(String id, String title, String author) {
super(id, title);
this.author = author;
this.isBorrowed = false;
}
@Override
public void borrow(String user) {
if (!isBorrowed) {
isBorrowed = true;
System.out.println("Book borrowed: " + title);
} else {
System.out.println("Book already borrowed");
}
}
@Override
public void returnBook() {
if (isBorrowed) {
isBorrowed = false;
System.out.println("Book returned: " + title);
}
}
@Override
public boolean isBorrowed() {
return isBorrowed;
}
@Override
void displayDetails() {
System.out.println("ID: " + id + ", Title: " + title + ", Author: " + author + ", Borrowed: " + isBorrowed);
}
String toFileString() {
return id + "," + title + "," + author + "," + isBorrowed;
}
}
class Loan {
private String bookId, user, date;
Loan(String bookId, String user, String date) {
this.bookId = bookId;
this.user = user;
this.date = date;
}
String toFileString() {
return bookId + "," + user + "," + date;
}
void display() {
System.out.println("Loan: Book ID " + bookId + ", User: " + user + ", Date: " + date);
}
}
public class LibraryManagementSystem {
private HashMap<String, Book> books;
private ArrayList<Loan> loans;
LibraryManagementSystem() {
books = new HashMap<>();
loans = new ArrayList<>();
}
void addBook(String id, String title, String author) throws InvalidBookException {
if (id.isEmpty() || title.isEmpty() || author.isEmpty()) {
throw new InvalidBookException("Book details cannot be empty");
}
if (books.containsKey(id)) {
throw new InvalidBookException("Book ID already exists");
}
books.put(id, new Book(id, title, author));
System.out.println("Added book: " + title);
}
void removeBook(String id) {
if (books.remove(id) != null) {
System.out.println("Removed book: " + id);
} else {
System.out.println("Book not found: " + id);
}
}
void borrowBook(String id, String user, String date) throws InvalidBookException {
if (!books.containsKey(id)) {
throw new InvalidBookException("Book not found: " + id);
}
books.get(id).borrow(user);
loans.add(new Loan(id, user, date));
}
void returnBook(String id) throws InvalidBookException {
if (!books.containsKey(id)) {
throw new InvalidBookException("Book not found: " + id);
}
books.get(id).returnBook();
}
void searchBook(String query) {
boolean found = false;
for (Book book : books.values()) {
if (book.title.contains(query) || book.id.equals(query)) {
book.displayDetails();
found = true;
}
}
if (!found) {
System.out.println("No books found for: " + query);
}
}
void displayLoans() {
if (loans.isEmpty()) {
System.out.println("No loans");
return;
}
for (Loan loan : loans) {
loan.display();
}
}
void saveToFile() {
try (BufferedWriter bookWriter = new BufferedWriter(new FileWriter("books.txt"))) {
for (Book book : books.values()) {
bookWriter.write(book.toFileString());
bookWriter.newLine();
}
System.out.println("Books saved");
} catch (IOException e) {
System.out.println("Error saving books: " + e.getMessage());
}
try (BufferedWriter loanWriter = new BufferedWriter(new FileWriter("loans.txt"))) {
for (Loan loan : loans) {
loanWriter.write(loan.toFileString());
loanWriter.newLine();
}
System.out.println("Loans saved");
} catch (IOException e) {
System.out.println("Error saving loans: " + e.getMessage());
}
}
void loadFromFile() {
try (BufferedReader bookReader = new BufferedReader(new FileReader("books.txt"))) {
String line;
while ((line = bookReader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 4) {
Book book = new Book(parts[0], parts[1], parts[2]);
if (Boolean.parseBoolean(parts[3])) {
book.borrow("Unknown");
}
books.put(parts[0], book);
}
}
System.out.println("Books loaded");
} catch (IOException e) {
System.out.println("Error loading books: " + e.getMessage());
}
try (BufferedReader loanReader = new BufferedReader(new FileReader("loans.txt"))) {
String line;
while ((line = loanReader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3) {
loans.add(new Loan(parts[0], parts[1], parts[2]));
}
}
System.out.println("Loans loaded");
} catch (IOException e) {
System.out.println("Error loading loans: " + e.getMessage());
}
}
void listBorrowedBooks() {
boolean found = false;
for (Book book : books.values()) {
if (book.isBorrowed()) {
book.displayDetails();
found = true;
}
}
if (!found) {
System.out.println("No books are borrowed");
}
}
public static void main(String[] args) {
LibraryManagementSystem library = new LibraryManagementSystem();
try {
library.loadFromFile();
library.addBook("B001", "Java Basics", "John Doe");
library.addBook("B002", "OOP Guide", "Jane Smith");
library.borrowBook("B001", "Alice", "2025-09-02");
library.searchBook("Java");
library.displayLoans();
library.listBorrowedBooks();
library.saveToFile();
} catch (InvalidBookException e) {
System.out.println("Error: " + e.getMessage());
}
}
}