-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookManager.java
More file actions
93 lines (83 loc) · 3.26 KB
/
BookManager.java
File metadata and controls
93 lines (83 loc) · 3.26 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
import java.io.*;
import java.util.*;
public class BookManager {
private static final String FILE_NAME = "libros.txt";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
while (true) {
System.out.println("\nGestión de Libros");
System.out.println("1. Añadir libro");
System.out.println("2. Buscar libro");
System.out.println("3. Listar libros");
System.out.println("4. Salir");
System.out.print("Seleccione una opción: ");
choice = scanner.nextInt();
scanner.nextLine(); // Limpiar el buffer
switch (choice) {
case 1:
addBook(scanner);
break;
case 2:
searchBook(scanner);
break;
case 3:
listBooks();
break;
case 4:
System.out.println("Saliendo...");
return;
default:
System.out.println("Opción no válida.");
}
}
}
// Función para añadir un libro
private static void addBook(Scanner scanner) {
System.out.print("Ingrese el título del libro: ");
String title = scanner.nextLine();
System.out.print("Ingrese el autor del libro: ");
String author = scanner.nextLine();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME, true))) {
writer.write(title + "," + author);
writer.newLine();
System.out.println("Libro añadido exitosamente.");
} catch (IOException e) {
System.out.println("Error al guardar el libro: " + e.getMessage());
}
}
// Función para buscar un libro por su título
private static void searchBook(Scanner scanner) {
System.out.print("Ingrese el título del libro a buscar: ");
String title = scanner.nextLine();
boolean found = false;
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts[0].equalsIgnoreCase(title)) {
System.out.println("Libro encontrado: Título: " + parts[0] + ", Autor: " + parts[1]);
found = true;
break;
}
}
if (!found) {
System.out.println("Libro no encontrado.");
}
} catch (IOException e) {
System.out.println("Error al leer el archivo: " + e.getMessage());
}
}
// Función para listar todos los libros
private static void listBooks() {
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
System.out.println("Título: " + parts[0] + ", Autor: " + parts[1]);
}
} catch (IOException e) {
System.out.println("Error al leer el archivo: " + e.getMessage());
}
}
}