Skip to content

Commit d144cd6

Browse files
authored
Create main.java
1 parent 8144da5 commit d144cd6

File tree

1 file changed

+217
-0
lines changed

1 file changed

+217
-0
lines changed

src/main.java

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
package com.javabooks;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
private static LibraryService library = new Library();
9+
private static Scanner scanner = new Scanner(System.in);
10+
11+
public static void main(String[] args) {
12+
13+
loadFromFile();
14+
15+
System.out.println("=== JavaBooks PRO System ===");
16+
17+
while (true) {
18+
printMenu();
19+
String input = scanner.nextLine();
20+
21+
switch (input) {
22+
case "1": addBook(); break;
23+
case "2": listBooks(); break;
24+
case "3": searchMenu(); break;
25+
case "4": borrowBook(); break;
26+
case "5": returnBook(); break;
27+
case "6": removeBook(); break;
28+
case "7": sortMenu(); break;
29+
case "8": statistics(); break;
30+
case "9": saveToFile(); break;
31+
case "10": loadFromFile(); break;
32+
case "0": exit(); return;
33+
default: System.out.println("Invalid option!");
34+
}
35+
}
36+
}
37+
38+
private static void printMenu() {
39+
System.out.println("\n===== MENU =====");
40+
System.out.println("1. Add Book");
41+
System.out.println("2. List Books");
42+
System.out.println("3. Search");
43+
System.out.println("4. Borrow Book");
44+
System.out.println("5. Return Book");
45+
System.out.println("6. Remove Book");
46+
System.out.println("7. Sort Books");
47+
System.out.println("8. Statistics");
48+
System.out.println("9. Save");
49+
System.out.println("10. Load");
50+
System.out.println("0. Exit");
51+
System.out.print("Choose: ");
52+
}
53+
54+
private static void addBook() {
55+
try {
56+
System.out.print("Title: ");
57+
String title = scanner.nextLine();
58+
59+
System.out.print("Author: ");
60+
String author = scanner.nextLine();
61+
62+
System.out.print("Year: ");
63+
int year = Integer.parseInt(scanner.nextLine());
64+
65+
System.out.print("ISBN: ");
66+
String isbn = scanner.nextLine();
67+
68+
library.addBook(new Book(title, author, year, isbn));
69+
System.out.println("Added!");
70+
} catch (Exception e) {
71+
System.out.println("Error!");
72+
}
73+
}
74+
75+
private static void listBooks() {
76+
List<Book> books = library.listBooks();
77+
if (books.isEmpty()) {
78+
System.out.println("Empty library!");
79+
return;
80+
}
81+
82+
System.out.println("\n--- BOOKS ---");
83+
for (Book b : books) {
84+
System.out.println(b);
85+
}
86+
}
87+
88+
private static void searchMenu() {
89+
System.out.println("1. By Title");
90+
System.out.println("2. By Author");
91+
String choice = scanner.nextLine();
92+
93+
System.out.print("Query: ");
94+
String query = scanner.nextLine();
95+
96+
List<Book> results;
97+
98+
if (choice.equals("1")) {
99+
results = library.searchByTitle(query);
100+
} else {
101+
results = library.searchByAuthor(query);
102+
}
103+
104+
if (results.isEmpty()) {
105+
System.out.println("No results.");
106+
} else {
107+
results.forEach(System.out::println);
108+
}
109+
}
110+
111+
private static void borrowBook() {
112+
System.out.print("ISBN: ");
113+
String isbn = scanner.nextLine();
114+
115+
Book b = library.findByIsbn(isbn);
116+
117+
if (b == null) {
118+
System.out.println("Not found!");
119+
return;
120+
}
121+
122+
if (b.isBorrowed()) {
123+
System.out.println("Already borrowed!");
124+
return;
125+
}
126+
127+
library.borrowBook(isbn);
128+
System.out.println("Borrowed!");
129+
}
130+
131+
private static void returnBook() {
132+
System.out.print("ISBN: ");
133+
String isbn = scanner.nextLine();
134+
135+
Book b = library.findByIsbn(isbn);
136+
137+
if (b == null) {
138+
System.out.println("Not found!");
139+
return;
140+
}
141+
142+
if (!b.isBorrowed()) {
143+
System.out.println("Not borrowed!");
144+
return;
145+
}
146+
147+
library.returnBook(isbn);
148+
System.out.println("Returned!");
149+
}
150+
151+
private static void removeBook() {
152+
System.out.print("ISBN: ");
153+
String isbn = scanner.nextLine();
154+
155+
library.removeBook(isbn);
156+
System.out.println("Removed.");
157+
}
158+
159+
private static void sortMenu() {
160+
List<Book> books = library.listBooks();
161+
162+
System.out.println("1. By Title");
163+
System.out.println("2. By Year");
164+
String choice = scanner.nextLine();
165+
166+
if (choice.equals("1")) {
167+
books.sort(Comparator.comparing(Book::getTitle));
168+
} else {
169+
books.sort(Comparator.comparingInt(Book::getYear));
170+
}
171+
172+
books.forEach(System.out::println);
173+
}
174+
175+
private static void statistics() {
176+
List<Book> books = library.listBooks();
177+
178+
long total = books.size();
179+
long borrowed = books.stream().filter(Book::isBorrowed).count();
180+
181+
System.out.println("Total books: " + total);
182+
System.out.println("Borrowed: " + borrowed);
183+
System.out.println("Available: " + (total - borrowed));
184+
}
185+
186+
private static void saveToFile() {
187+
try (BufferedWriter writer = new BufferedWriter(new FileWriter("books.txt"))) {
188+
for (Book b : library.listBooks()) {
189+
writer.write(b.getTitle() + ";" + b.getAuthor() + ";" + b.getYear() + ";" + b.getIsbn() + ";" + b.isBorrowed());
190+
writer.newLine();
191+
}
192+
System.out.println("Saved!");
193+
} catch (IOException e) {
194+
System.out.println("Save error!");
195+
}
196+
}
197+
198+
private static void loadFromFile() {
199+
try (BufferedReader reader = new BufferedReader(new FileReader("books.txt"))) {
200+
String line;
201+
while ((line = reader.readLine()) != null) {
202+
String[] p = line.split(";");
203+
Book b = new Book(p[0], p[1], Integer.parseInt(p[2]), p[3]);
204+
if (Boolean.parseBoolean(p[4])) b.borrow();
205+
library.addBook(b);
206+
}
207+
System.out.println("Loaded!");
208+
} catch (IOException e) {
209+
System.out.println("No file.");
210+
}
211+
}
212+
213+
private static void exit() {
214+
saveToFile();
215+
System.out.println("Goodbye!");
216+
}
217+
}

0 commit comments

Comments
 (0)