public class Libro {
String titulo;
String autor;
boolean prestado;
public Libro(String titulo, String autor) {
this.titulo = titulo;
this.autor = autor;
this.prestado = false;
}
public void mostrarInfo() {
System.out.println("Título: " + titulo);
System.out.println("Autor: " + autor);
System.out.println("Prestado: " + (prestado ? "Sí" : "No"));
System.out.println("------------------------");
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Biblioteca {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList libros = new ArrayList<>();
int opcion = 0;
while (opcion != 6) {
System.out.println("=== MENÚ BIBLIOTECA ===");
System.out.println("1. Registrar libro");
System.out.println("2. Mostrar libros");
System.out.println("3. Buscar libro por título");
System.out.println("4. Prestar libro");
System.out.println("5. Devolver libro");
System.out.println("6. Salir");
System.out.print("Elige una opción: ");
opcion = scanner.nextInt();
scanner.nextLine();
switch (opcion) {
case 1:
System.out.print("Ingrese el título: ");
String titulo = scanner.nextLine();
System.out.print("Ingrese el autor: ");
String autor = scanner.nextLine();
libros.add(new Libro(titulo, autor));
System.out.println("Libro registrado correctamente.\n");
break;
case 2:
if (libros.isEmpty()) {
System.out.println("No hay libros registrados.\n");
} else {
for (Libro libro : libros) {
libro.mostrarInfo();
}
}
break;
case 3:
System.out.print("Ingrese el título a buscar: ");
String buscarTitulo = scanner.nextLine();
boolean encontrado = false;
for (Libro libro : libros) {
if (libro.titulo.equalsIgnoreCase(buscarTitulo)) {
libro.mostrarInfo();
encontrado = true;
}
}
if (!encontrado) {
System.out.println("Libro no encontrado.\n");
}
break;
case 4:
System.out.print("Ingrese el título del libro a prestar: ");
String prestarTitulo = scanner.nextLine();
boolean prestado = false;
for (Libro libro : libros) {
if (libro.titulo.equalsIgnoreCase(prestarTitulo)) {
if (!libro.prestado) {
libro.prestado = true;
System.out.println("Libro prestado correctamente.\n");
} else {
System.out.println("Ese libro ya está prestado.\n");
}
prestado = true;
}
}
if (!prestado) {
System.out.println("Libro no encontrado.\n");
}
break;
case 5:
System.out.print("Ingrese el título del libro a devolver: ");
String devolverTitulo = scanner.nextLine();
boolean devuelto = false;
for (Libro libro : libros) {
if (libro.titulo.equalsIgnoreCase(devolverTitulo)) {
if (libro.prestado) {
libro.prestado = false;
System.out.println("Libro devuelto correctamente.\n");
} else {
System.out.println("Ese libro no estaba prestado.\n");
}
devuelto = true;
}
}
if (!devuelto) {
System.out.println("Libro no encontrado.\n");
}
break;
case 6:
System.out.println("Saliendo del sistema...");
break;
default:
System.out.println("Opción inválida.\n");
}
}
scanner.close();
}
}