Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Practico1;
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.Scanner;
- import javax.swing.Spring;
- import java.io.IOException;
- public class Ejercicio6 {
- static Random aleatorio = new Random();
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- menu();
- }
- public static void menu() {
- ArrayList<Libros> biblioteca = new ArrayList<Libros>();
- Libros temporal = new Libros();
- int opcion;
- do {
- System.out.println("..........................................");
- System.out.println("1 - Cargar Libros de Forma Manual");
- System.out.println("2 - Cargar Libros Aleatorios");
- System.out.println("3 - Buscar libros por...");
- System.out.println("4 - Buscar Libros con precio menor a X");
- System.out.println("5 - Mostrar una cantidad");
- System.out.println("6 - Finalizar");
- System.out.println("..........................................");
- opcion=Helper.getPositiveInt("Ingrese una opcion ");
- switch(opcion){
- case 1:
- do {
- temporal = cargarLibro();
- biblioteca.add(temporal);
- System.out.println("Libro Cargado Correctamente!!");
- opcion = Helper.getPositiveInt("Cargar otro libro???(1=Si/2=No)");
- } while (opcion!=2);
- break;
- case 2:
- do {
- temporal = nuevoLibroAleatorio();
- biblioteca.add(temporal);
- System.out.println("Libro Cargado Correctamente!!");
- opcion = Helper.getPositiveInt("Cargar otro libro???(1=Si/2=No)");
- } while (opcion!=2);
- break;
- case 3:
- Scanner ingresar = new Scanner(System.in);
- String buscar;
- do {
- System.out.println("-----------------------------------------");
- System.out.println("1 - Buscar por nombre de Autor");
- System.out.println("2 - Buscar por nombre de Editorial");
- System.out.println("3 - Retornar Menu Principal");
- System.out.println("-----------------------------------------");
- opcion =Helper.getPositiveInt("Ingrese una opcion de busqueda ");
- switch(opcion) {
- case 1:
- System.out.println("Busqueda por Autor");
- System.out.println("Ingrese autor que esta buscando: ");
- buscar = ingresar.nextLine();
- System.out.println("Titulos del autor " + buscar);
- buscarPorAutor(biblioteca,buscar);
- espera();
- break;
- case 2:
- System.out.println("Busqueda por Editorial");
- System.out.println("Ingrese editorial que esta buscando: ");
- buscar = ingresar.nextLine();
- System.out.println("Titulos publicados por la Editorial " + buscar);
- buscarPorEditorial(biblioteca, buscar);
- espera();
- break;
- default:
- System.out.println("Metodo de busqueda desconocido....");
- }
- }while(opcion != 3);
- break;
- case 4:
- float x = Helper.getFloat("Ingrese un precio maximo");
- buscarPrecio(biblioteca, x);
- espera();
- break;
- case 5:
- int X = Helper.getPositiveInt("Ingrese la cantidad de libros que desea ver");
- mostrar(biblioteca, X);
- espera();
- break;
- case 6:
- System.out.println("FIN DEL PROGRAMA");
- break;
- default:
- System.out.println("ERROR-LA OPCION INGRESADA ES INCORRECTA");;
- }
- } while (opcion!=6);
- System.out.println("Gracias :D");
- }
- public static Libros cargarLibro() {
- Scanner ingreso = new Scanner(System.in);
- Libros unLibro = new Libros();
- int año;
- Spring titulo, autor, editorail;
- String genero =null;
- float precio;
- boolean si = false;
- System.out.println("Datos del libro");
- System.out.println("Ingrese el Titulo del Libro");
- unLibro.setTitulo(ingreso.nextLine());
- System.out.println("Ingrese el Autor del Libro");
- unLibro.setAutores(cargarAutores());
- System.out.println("Ingrese la Editorial del Libro");
- unLibro.setEditorial(ingreso.nextLine());
- while (true) {
- año = Helper.getPositiveInt("Ingrese el Año de publicacion");
- if (validarAño(año) == true) {
- unLibro.setAñoDePublicacion(año);
- break;
- }
- }
- while (true) {
- precio=Helper.getFloat("Ingrese el Precio del Libro");
- if(precio>0){
- unLibro.setPrecio(precio);
- break;
- }
- }
- do {
- System.out.println("Cual es el genero del libro??");
- int op2 = Helper.getPositiveInt("1-Narrativo\n2-Dramatico\n3-Lirico");
- switch(op2) {
- case 1:
- unLibro.setGenero("Narrativo");
- si = true;
- break;
- case 2:
- unLibro.setGenero("Dramatico");
- si = true;
- break;
- case 3:
- unLibro.setGenero("Lirico");
- si = true;
- break;
- default:System.out.println("Genero no valido");
- si = false;
- }
- }while(si!= true);
- return unLibro;
- }
- public static ArrayList<String> cargarAutores(){
- Scanner ingresoA = new Scanner(System.in);
- ArrayList<String> autores = new ArrayList<String>();
- int opcion;
- String autor;
- do {
- autor = ingresoA.nextLine();
- autores.add(autor);
- opcion = Helper.getPositiveInt("Desea ingresar otro autor??(1:Si//2:No)");
- }while(opcion != 2 );
- return(autores);
- }
- static Libros nuevoLibroAleatorio(){
- Libros unLibro = new Libros();
- int año;
- float precio;
- unLibro.setTitulo(generarTitulo());
- unLibro.setEditorial(generarEditorial());
- unLibro.setAutores(generarAutor());
- while (true) {
- año = aleatorio.nextInt(2022);
- if (validarAño(año) == true) {
- unLibro.setAñoDePublicacion(año);
- break;
- }
- }
- while (true) {
- precio=aleatorio.nextFloat()*1000;
- precio=(float)Math.round(precio * 100d)/100;
- if(precio>0){
- unLibro.setPrecio(precio);
- break;
- }
- }
- unLibro.setGenero(generarGenero());
- return unLibro;
- }
- public static String generarTitulo() {
- String[] titulos = {"Danza de Fuego y Hielo", "Tiny Tina: Camaras y Cabronazos", "Esclavo de la Libertad",
- "El Festiva de la Blasfemia", "El Sueño de un Millar de Gatos", "Una Falsa Verdad",
- "Como Matatar a un Ruiseñor", "Las 3 Lunas: Leyenda Khajiit", "El Principio del Fin",
- "Dovahkiin: El Sangre de Dragon", "La Odisea de los Giles", "El Campo de Juncos",
- "Bandersnatch", "La Leyenda del Hada y el Mago", "Feliz Dia de tu Muerte",
- "La Divina Comedia", "Los Ojos del Perro Siberiano", "El Deseo de un Raton",
- "La Niebla y la Fortuna", "Ciudades de Papel", "Guia del Mas Alla",
- "El Dia que Dios Parpadeo", "Los 7 Pecados Capitales", "Caronte: La historia del Barquero"};
- int i = aleatorio.nextInt(titulos.length-1);
- return titulos[i];
- }
- public static String generarEditorial() {
- String[] editoriales = {"Random House",
- "Simon & Schuster",
- "Sintesis",
- "Apolo S.A.",
- "Pearson",
- "Kadokawa"};
- int i = aleatorio.nextInt(editoriales.length-1);
- return editoriales[i];
- }
- public static ArrayList<String> generarAutor() {
- ArrayList<String> autores = new ArrayList<String>();
- String[] autor = {"Abello","Verano","Ana",
- "Abril" , "Juan","carlos","Abril" , "Juan Carlos" , "Cullel" , "Diana"
- ,"Abril", "Fernández Serrato" , "Abuín" , "Anxo" , "Pérez", "Rasilla" ,"Soria", "Tomás" , "G.Adonis",
- "Aganzo" , "Carls", "Aguilar","Marina" , "Jesús Aguiló" , "Josep Lluís","Agustini" , "Delmira",
- "Alarcos Llorach" , "Emilio","Alas","Leopoldo", "Albero"," Miguel", "Albert", "Mechthild",
- "Alberti","Rafael", "Alberti" , "Rafael", "León" , "María", "Teresa","Alburquerque", "García" , "Luis",
- "Alcantarilla" , "María","Aldecoa" , "Ignacio","Aldrete" , "Bernardo","Alegría" , "Claribel",
- "Aleixandre" , "Vicente","Alemany Bay" , "Carmen","Alemany ", "Carmen" ,"Valero" , "Eva" , "Sanchís" , "Victor",
- "Alexander" , "Francisco","Allan Poe" , "Edgar","Almuzara ", "Javier", "Alonso Ares" , "Adolfo"};
- int x = aleatorio.nextInt(5)+1;
- for(int i =0 ; i<x ;i++){
- int j = aleatorio.nextInt(autor.length-1);
- autores.add(autor[j]);
- }
- return (autores);
- }
- public static String generarGenero() {
- String[] genero = {"Narrativo","Dramatico","Lirico"};
- int z = aleatorio.nextInt(genero.length-1);
- return genero[z];
- }
- public static void buscarPorAutor(ArrayList<Libros> array, String buscar) {
- for (int i = 0; i < array.size(); i++) {
- if(array.get(i).getAutores().contains(buscar)) {
- System.out.println(array.get(i));
- }
- }
- }
- public static void buscarPorEditorial(ArrayList<Libros> array, String buscar) {
- for (int i = 0; i < array.size(); i++) {
- if(array.get(i).getEditorial().equalsIgnoreCase(buscar)) {
- System.out.println(array.get(i));
- }
- }
- }
- public static void buscarPrecio(ArrayList<Libros> array, float x) {
- for(Libros libro:array) {
- if(libro.getPrecio()<x) {
- System.out.println(libro.toString());
- }
- }
- }
- public static void mostrar(ArrayList<Libros> array, int X) {
- int i =0;
- while(i<array.size() && X>i) {
- System.out.println(array.get(i).toString());
- i++;
- }
- }
- public static Boolean validarAño(int año) {
- while(true) {
- if ((1900 <= año) && (año <= 2022)) {
- return true;
- }else {
- return false;
- }
- }
- }
- @SuppressWarnings("resource")
- public static void espera (){ //Este modulo detiene el proceso hasta que el ususario precione "enter"
- Scanner waitForKeypress = new Scanner(System.in);
- System.out.print("Presiona la tecla Enter para continuar....");
- waitForKeypress.nextLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement