Advertisement
Roke98

Ejercicio6-Tp1-UNJu

Sep 17th, 2022 (edited)
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.82 KB | None | 0 0
  1. package Practico1;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5. import java.util.Scanner;
  6. import javax.swing.Spring;
  7. import java.io.IOException;
  8.  
  9. public class Ejercicio6 {
  10. static Random aleatorio = new Random();
  11.    
  12.     public static void main(String[] args) {
  13.         // TODO Auto-generated method stub
  14.         menu();
  15.     }
  16.    
  17.     public static void menu() {
  18.         ArrayList<Libros> biblioteca = new ArrayList<Libros>();
  19.         Libros temporal  = new Libros();
  20.         int opcion;
  21.          do {
  22.             System.out.println("..........................................");
  23.             System.out.println("1 - Cargar Libros de Forma Manual");
  24.             System.out.println("2 - Cargar Libros Aleatorios");
  25.             System.out.println("3 - Buscar libros por...");
  26.             System.out.println("4 - Buscar Libros con precio menor a X");
  27.             System.out.println("5 - Mostrar una cantidad");
  28.             System.out.println("6 - Finalizar");
  29.             System.out.println("..........................................");
  30.            
  31.             opcion=Helper.getPositiveInt("Ingrese una opcion ");
  32.             switch(opcion){
  33.                 case 1:
  34.                     do {  
  35.                         temporal = cargarLibro();
  36.                         biblioteca.add(temporal);
  37.                         System.out.println("Libro Cargado Correctamente!!");
  38.                         opcion = Helper.getPositiveInt("Cargar otro libro???(1=Si/2=No)");
  39.                     } while (opcion!=2);
  40.                     break;                  
  41.                 case 2:
  42.                     do {  
  43.                         temporal = nuevoLibroAleatorio();
  44.                         biblioteca.add(temporal);
  45.                         System.out.println("Libro Cargado Correctamente!!");
  46.                         opcion = Helper.getPositiveInt("Cargar otro libro???(1=Si/2=No)");
  47.                     } while (opcion!=2);
  48.                    
  49.                     break;
  50.                 case 3:
  51.                     Scanner ingresar = new Scanner(System.in);
  52.                     String buscar;
  53.                     do {
  54.                         System.out.println("-----------------------------------------");
  55.                         System.out.println("1 - Buscar por nombre de Autor");
  56.                         System.out.println("2 - Buscar por nombre de Editorial");
  57.                         System.out.println("3 - Retornar Menu Principal");
  58.                         System.out.println("-----------------------------------------");
  59.                         opcion =Helper.getPositiveInt("Ingrese una opcion de busqueda ");
  60.                         switch(opcion) {
  61.                         case 1:
  62.                             System.out.println("Busqueda por Autor");
  63.                             System.out.println("Ingrese autor que esta buscando: ");
  64.                             buscar = ingresar.nextLine();
  65.                             System.out.println("Titulos del autor " + buscar);
  66.                             buscarPorAutor(biblioteca,buscar);
  67.                             espera();
  68.                            
  69.                             break;
  70.                         case 2:
  71.                             System.out.println("Busqueda por Editorial");
  72.                             System.out.println("Ingrese editorial que esta buscando: ");
  73.                             buscar = ingresar.nextLine();
  74.                             System.out.println("Titulos publicados por la Editorial " + buscar);
  75.                             buscarPorEditorial(biblioteca, buscar);
  76.                             espera();
  77.                            
  78.                             break;
  79.                         default:
  80.                             System.out.println("Metodo de busqueda desconocido....");
  81.                         }
  82.                     }while(opcion != 3);
  83.                    
  84.                     break;
  85.                 case 4:
  86.                     float x = Helper.getFloat("Ingrese un precio maximo");
  87.                     buscarPrecio(biblioteca, x);
  88.                     espera();
  89.                    
  90.                     break;
  91.                 case 5:
  92.                     int X = Helper.getPositiveInt("Ingrese la cantidad de libros que desea ver");
  93.                     mostrar(biblioteca, X);
  94.                     espera();
  95.                    
  96.                     break;
  97.                 case 6:
  98.                     System.out.println("FIN DEL PROGRAMA");
  99.                     break;
  100.                 default:
  101.                     System.out.println("ERROR-LA OPCION INGRESADA ES INCORRECTA");;
  102.             }
  103.         } while (opcion!=6);
  104.          System.out.println("Gracias :D");
  105.     }
  106.    
  107.     public static Libros cargarLibro() {
  108.         Scanner ingreso = new Scanner(System.in);
  109.         Libros unLibro = new Libros();
  110.         int año;
  111.         Spring titulo, autor, editorail;
  112.         String genero =null;
  113.         float precio;
  114.         boolean si = false;
  115.         System.out.println("Datos del libro");
  116.         System.out.println("Ingrese el Titulo del Libro");
  117.         unLibro.setTitulo(ingreso.nextLine());
  118.         System.out.println("Ingrese el Autor del Libro");
  119.         unLibro.setAutores(cargarAutores());
  120.         System.out.println("Ingrese la Editorial del Libro");
  121.         unLibro.setEditorial(ingreso.nextLine());
  122.         while (true) {            
  123.             año = Helper.getPositiveInt("Ingrese el Año de publicacion");
  124.             if (validarAño(año) == true) {
  125.                 unLibro.setAñoDePublicacion(año);
  126.             break;
  127.             }
  128.          }
  129.         while (true) {        
  130.             precio=Helper.getFloat("Ingrese el Precio del Libro");
  131.             if(precio>0){
  132.                 unLibro.setPrecio(precio);
  133.             break;
  134.             }
  135.          }
  136.          do {
  137.              System.out.println("Cual es el genero del libro??");
  138.              int op2 = Helper.getPositiveInt("1-Narrativo\n2-Dramatico\n3-Lirico");
  139.              switch(op2) {
  140.              case 1:
  141.                  unLibro.setGenero("Narrativo");
  142.                  si = true;
  143.                  break;
  144.              case 2:
  145.                  unLibro.setGenero("Dramatico");
  146.                  si = true;
  147.                  break;
  148.              case 3:
  149.                  unLibro.setGenero("Lirico");
  150.                  si = true;
  151.                  break;
  152.              default:System.out.println("Genero no valido");
  153.              si = false;
  154.              }
  155.          }while(si!= true);
  156.  
  157.          return unLibro;
  158.      }
  159.      
  160.      public static ArrayList<String> cargarAutores(){
  161.          Scanner ingresoA = new Scanner(System.in);
  162.          ArrayList<String> autores = new ArrayList<String>();
  163.          int opcion;
  164.          String autor;
  165.          
  166.          do {
  167.              autor = ingresoA.nextLine();
  168.              autores.add(autor);
  169.              opcion = Helper.getPositiveInt("Desea ingresar otro autor??(1:Si//2:No)");
  170.          }while(opcion != 2 );
  171.          return(autores);
  172.      }
  173.      
  174.      static Libros nuevoLibroAleatorio(){
  175.          Libros unLibro = new Libros();
  176.          int año;
  177.          float precio;
  178.          unLibro.setTitulo(generarTitulo());
  179.          unLibro.setEditorial(generarEditorial());
  180.          unLibro.setAutores(generarAutor());
  181.  
  182.          while (true) {            
  183.             año = aleatorio.nextInt(2022);
  184.             if (validarAño(año) == true) {
  185.                 unLibro.setAñoDePublicacion(año);
  186.                 break;
  187.             }
  188.          }
  189.          while (true) {        
  190.              precio=aleatorio.nextFloat()*1000;
  191.              precio=(float)Math.round(precio * 100d)/100;
  192.              if(precio>0){
  193.                  unLibro.setPrecio(precio);
  194.                  break;
  195.              }
  196.          }
  197.          unLibro.setGenero(generarGenero());
  198.          return unLibro;
  199.      }
  200.      
  201.      public static String generarTitulo() {
  202.          String[] titulos = {"Danza de Fuego y Hielo", "Tiny Tina: Camaras y Cabronazos", "Esclavo de la Libertad",
  203.                             "El Festiva de la Blasfemia", "El Sueño de un Millar de Gatos", "Una Falsa Verdad",
  204.                             "Como Matatar a un Ruiseñor", "Las 3 Lunas: Leyenda Khajiit", "El Principio del Fin",
  205.                             "Dovahkiin: El Sangre de Dragon", "La Odisea de los Giles", "El Campo de Juncos",
  206.                             "Bandersnatch", "La Leyenda del Hada y el Mago", "Feliz Dia de tu Muerte",
  207.                             "La Divina Comedia", "Los Ojos del Perro Siberiano", "El Deseo de un Raton",
  208.                             "La Niebla y la Fortuna", "Ciudades de Papel", "Guia del Mas Alla",
  209.                             "El Dia que Dios Parpadeo", "Los 7 Pecados Capitales", "Caronte: La historia del Barquero"};
  210.          int i = aleatorio.nextInt(titulos.length-1);
  211.          return titulos[i];
  212.      }
  213.      
  214.      public static String generarEditorial() {
  215.          String[] editoriales = {"Random House",
  216.                                 "Simon & Schuster",
  217.                                 "Sintesis",
  218.                                 "Apolo S.A.",
  219.                                 "Pearson",
  220.                                 "Kadokawa"};
  221.          int i = aleatorio.nextInt(editoriales.length-1);
  222.          return editoriales[i];
  223.      }
  224.      
  225.      public static ArrayList<String> generarAutor() {
  226.          ArrayList<String> autores = new ArrayList<String>();
  227.          String[] autor = {"Abello","Verano","Ana",
  228.                     "Abril" , "Juan","carlos","Abril" , "Juan Carlos" , "Cullel" , "Diana"
  229.                     ,"Abril", "Fernández Serrato" ,    "Abuín" , "Anxo" , "Pérez", "Rasilla" ,"Soria", "Tomás" , "G.Adonis",
  230.                     "Aganzo" , "Carls", "Aguilar","Marina" , "Jesús Aguiló" , "Josep Lluís","Agustini" , "Delmira",
  231.                     "Alarcos Llorach" , "Emilio","Alas","Leopoldo", "Albero"," Miguel", "Albert", "Mechthild",
  232.                     "Alberti","Rafael", "Alberti" , "Rafael", "León" , "María", "Teresa","Alburquerque", "García" , "Luis",
  233.                     "Alcantarilla" , "María","Aldecoa" , "Ignacio","Aldrete" , "Bernardo","Alegría" , "Claribel",
  234.                     "Aleixandre" , "Vicente","Alemany Bay" , "Carmen","Alemany ", "Carmen" ,"Valero" , "Eva" , "Sanchís" , "Victor",
  235.                     "Alexander" , "Francisco","Allan Poe" , "Edgar","Almuzara ", "Javier",  "Alonso Ares" , "Adolfo"};
  236.          int x = aleatorio.nextInt(5)+1;
  237.          for(int i =0 ; i<x ;i++){
  238.              int j = aleatorio.nextInt(autor.length-1);
  239.              autores.add(autor[j]);
  240.          }
  241.          return (autores);
  242.      }
  243.      
  244.      public static String generarGenero() {
  245.          String[] genero = {"Narrativo","Dramatico","Lirico"};
  246.          int z = aleatorio.nextInt(genero.length-1);
  247.          return genero[z];
  248.      }
  249.      
  250.      public static void buscarPorAutor(ArrayList<Libros> array, String buscar) {
  251.          for (int i = 0; i < array.size(); i++) {
  252.             if(array.get(i).getAutores().contains(buscar)) {
  253.                 System.out.println(array.get(i));
  254.             }
  255.         }
  256.      }
  257.      
  258.      public static void buscarPorEditorial(ArrayList<Libros> array, String buscar) {
  259.          for (int i = 0; i < array.size(); i++) {
  260.             if(array.get(i).getEditorial().equalsIgnoreCase(buscar)) {
  261.                 System.out.println(array.get(i));
  262.             }
  263.         }
  264.      }
  265.      
  266.      public static void buscarPrecio(ArrayList<Libros> array, float x) {
  267.          for(Libros libro:array) {
  268.              if(libro.getPrecio()<x) {
  269.                  System.out.println(libro.toString());
  270.              }
  271.          }
  272.      }
  273.      
  274.      public static void mostrar(ArrayList<Libros> array, int X) {
  275.          int i =0;
  276.          while(i<array.size() && X>i) {
  277.              System.out.println(array.get(i).toString());
  278.              i++;
  279.         }
  280.      }
  281.      
  282.      public static Boolean validarAño(int año) {
  283.          while(true) {
  284.             if ((1900 <= año) && (año <= 2022)) {
  285.                 return true;
  286.             }else {
  287.                 return false;
  288.             }
  289.         }
  290.     }
  291.        
  292.      
  293.      @SuppressWarnings("resource")
  294.      public static void espera (){ //Este modulo detiene el proceso hasta que el ususario precione "enter"
  295.          Scanner waitForKeypress = new Scanner(System.in);
  296.          System.out.print("Presiona la tecla Enter para continuar....");
  297.          waitForKeypress.nextLine();
  298.      }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement