-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.java
More file actions
163 lines (140 loc) · 5.02 KB
/
Copy pathLibrary.java
File metadata and controls
163 lines (140 loc) · 5.02 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
import java.io.*;
import java.util.*;
public class Library {
private ArrayList<Book> books = new ArrayList<>();
private static final String DATA_FILE = "library_data.json";
public Library() {
loadData();
}
// Add Book
public void addBook(Scanner sc) {
System.out.print("Enter book title: ");
String title = sc.nextLine();
System.out.print("Enter author name: ");
String author = sc.nextLine();
System.out.print("Enter ISBN: ");
String isbn = sc.nextLine();
if (findBookByISBN(isbn) != null) {
System.out.println("Book with this ISBN already exists!");
return;
}
books.add(new Book(title, author, isbn));
System.out.println("Book added successfully!");
saveData();
}
// Search Book
public void searchBook(Scanner sc) {
System.out.println("Search by: 1. Title 2. Author 3. ISBN");
String choice = sc.nextLine();
System.out.print("Enter search term: ");
String query = sc.nextLine().toLowerCase();
boolean found = false;
for (Book book : books) {
boolean match = switch (choice) {
case "1" -> book.getTitle().toLowerCase().contains(query);
case "2" -> book.getAuthor().toLowerCase().contains(query);
case "3" -> book.getIsbn().equalsIgnoreCase(query);
default -> false;
};
if (match) {
System.out.println(book);
found = true;
}
}
if (!found) System.out.println("No matching book found.");
}
// Borrow Book
public void borrowBook(Scanner sc) {
System.out.print("Enter ISBN of book to borrow: ");
String isbn = sc.nextLine();
Book book = findBookByISBN(isbn);
if (book == null) System.out.println("Book not found!");
else if (book.borrowBook()) {
System.out.println("Borrowed successfully!");
saveData();
} else System.out.println("Already borrowed!");
}
// Return Book
public void returnBook(Scanner sc) {
System.out.print("Enter ISBN of book to return: ");
String isbn = sc.nextLine();
Book book = findBookByISBN(isbn);
if (book == null) System.out.println("Book not found!");
else if (book.returnBook()) {
System.out.println("Returned successfully!");
saveData();
} else System.out.println("Book wasn’t borrowed.");
}
// Remove Book
public void removeBook(Scanner sc) {
System.out.print("Enter ISBN of book to remove: ");
String isbn = sc.nextLine();
Book book = findBookByISBN(isbn);
if (book == null) {
System.out.println("Book not found!");
return;
}
books.remove(book);
System.out.println("Book removed successfully!");
saveData();
}
// List All Books
public void listAllBooks() {
if (books.isEmpty()) {
System.out.println("No books in library!");
return;
}
System.out.println("\n=== All Books ===");
for (Book book : books) System.out.println(book);
}
// Top Borrowed Books
public void showTopBorrowed() {
if (books.isEmpty()) {
System.out.println("No books yet!");
return;
}
books.sort((a, b) -> Integer.compare(b.getBorrowCount(), a.getBorrowCount()));
System.out.println("\n=== Top Borrowed Books ===");
for (int i = 0; i < Math.min(5, books.size()); i++) {
System.out.println((i + 1) + ". " + books.get(i));
}
}
private Book findBookByISBN(String isbn) {
for (Book b : books)
if (b.getIsbn().equalsIgnoreCase(isbn))
return b;
return null;
}
// Save data as JSON
private void saveData() {
try (PrintWriter pw = new PrintWriter(new FileWriter(DATA_FILE))) {
pw.println("[");
for (int i = 0; i < books.size(); i++) {
pw.print(" " + books.get(i).toJson());
if (i < books.size() - 1) pw.println(",");
else pw.println();
}
pw.println("]");
} catch (IOException e) {
System.out.println("Error saving: " + e.getMessage());
}
}
// Load data from JSON
private void loadData() {
File file = new File(DATA_FILE);
if (!file.exists()) return;
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
books.clear();
String line;
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.startsWith("{")) {
if (line.endsWith(",")) line = line.substring(0, line.length() - 1);
books.add(Book.fromJson(line));
}
}
} catch (IOException e) {
System.out.println("Error loading: " + e.getMessage());
}
}
}