-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.java
More file actions
217 lines (174 loc) · 6.21 KB
/
Copy pathmain.java
File metadata and controls
217 lines (174 loc) · 6.21 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
package com.javabooks;
import java.io.*;
import java.util.*;
public class Main {
private static LibraryService library = new Library();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
loadFromFile();
System.out.println("=== JavaBooks PRO System ===");
while (true) {
printMenu();
String input = scanner.nextLine();
switch (input) {
case "1": addBook(); break;
case "2": listBooks(); break;
case "3": searchMenu(); break;
case "4": borrowBook(); break;
case "5": returnBook(); break;
case "6": removeBook(); break;
case "7": sortMenu(); break;
case "8": statistics(); break;
case "9": saveToFile(); break;
case "10": loadFromFile(); break;
case "0": exit(); return;
default: System.out.println("Invalid option!");
}
}
}
private static void printMenu() {
System.out.println("\n===== MENU =====");
System.out.println("1. Add Book");
System.out.println("2. List Books");
System.out.println("3. Search");
System.out.println("4. Borrow Book");
System.out.println("5. Return Book");
System.out.println("6. Remove Book");
System.out.println("7. Sort Books");
System.out.println("8. Statistics");
System.out.println("9. Save");
System.out.println("10. Load");
System.out.println("0. Exit");
System.out.print("Choose: ");
}
private static void addBook() {
try {
System.out.print("Title: ");
String title = scanner.nextLine();
System.out.print("Author: ");
String author = scanner.nextLine();
System.out.print("Year: ");
int year = Integer.parseInt(scanner.nextLine());
System.out.print("ISBN: ");
String isbn = scanner.nextLine();
library.addBook(new Book(title, author, year, isbn));
System.out.println("Added!");
} catch (Exception e) {
System.out.println("Error!");
}
}
private static void listBooks() {
List<Book> books = library.listBooks();
if (books.isEmpty()) {
System.out.println("Empty library!");
return;
}
System.out.println("\n--- BOOKS ---");
for (Book b : books) {
System.out.println(b);
}
}
private static void searchMenu() {
System.out.println("1. By Title");
System.out.println("2. By Author");
String choice = scanner.nextLine();
System.out.print("Query: ");
String query = scanner.nextLine();
List<Book> results;
if (choice.equals("1")) {
results = library.searchByTitle(query);
} else {
results = library.searchByAuthor(query);
}
if (results.isEmpty()) {
System.out.println("No results.");
} else {
results.forEach(System.out::println);
}
}
private static void borrowBook() {
System.out.print("ISBN: ");
String isbn = scanner.nextLine();
Book b = library.findByIsbn(isbn);
if (b == null) {
System.out.println("Not found!");
return;
}
if (b.isBorrowed()) {
System.out.println("Already borrowed!");
return;
}
library.borrowBook(isbn);
System.out.println("Borrowed!");
}
private static void returnBook() {
System.out.print("ISBN: ");
String isbn = scanner.nextLine();
Book b = library.findByIsbn(isbn);
if (b == null) {
System.out.println("Not found!");
return;
}
if (!b.isBorrowed()) {
System.out.println("Not borrowed!");
return;
}
library.returnBook(isbn);
System.out.println("Returned!");
}
private static void removeBook() {
System.out.print("ISBN: ");
String isbn = scanner.nextLine();
library.removeBook(isbn);
System.out.println("Removed.");
}
private static void sortMenu() {
List<Book> books = library.listBooks();
System.out.println("1. By Title");
System.out.println("2. By Year");
String choice = scanner.nextLine();
if (choice.equals("1")) {
books.sort(Comparator.comparing(Book::getTitle));
} else {
books.sort(Comparator.comparingInt(Book::getYear));
}
books.forEach(System.out::println);
}
private static void statistics() {
List<Book> books = library.listBooks();
long total = books.size();
long borrowed = books.stream().filter(Book::isBorrowed).count();
System.out.println("Total books: " + total);
System.out.println("Borrowed: " + borrowed);
System.out.println("Available: " + (total - borrowed));
}
private static void saveToFile() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("books.txt"))) {
for (Book b : library.listBooks()) {
writer.write(b.getTitle() + ";" + b.getAuthor() + ";" + b.getYear() + ";" + b.getIsbn() + ";" + b.isBorrowed());
writer.newLine();
}
System.out.println("Saved!");
} catch (IOException e) {
System.out.println("Save error!");
}
}
private static void loadFromFile() {
try (BufferedReader reader = new BufferedReader(new FileReader("books.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
String[] p = line.split(";");
Book b = new Book(p[0], p[1], Integer.parseInt(p[2]), p[3]);
if (Boolean.parseBoolean(p[4])) b.borrow();
library.addBook(b);
}
System.out.println("Loaded!");
} catch (IOException e) {
System.out.println("No file.");
}
}
private static void exit() {
saveToFile();
System.out.println("Goodbye!");
}
}