RAUL-SUAREZ

tp-punto5

Sep 13th, 2022
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.15 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Ejercicio_05 {
  6.  
  7.     public static void main(String[] args) {
  8.         // TODO Auto-generated method stub
  9.         Random aleatorio = new Random();
  10.  
  11.         ArrayList<Libro> listaLibro = new ArrayList<>();
  12.    
  13.         boolean continuar;
  14.         int opc, opc1;
  15.         float num;
  16.         String titulos[] = { "Ficciones", "Decamerón", "Poemas", "Nostromo", "Medea", "Fausto", "Odisea", "Ulises",
  17.                 "Shakuntala", "Ensayos" };
  18.         String autores[] = { "Goku", "Batman", "Joker", "Superman", "Arrow", "Kemal", "Rocky", "Coyote", "Tigre",
  19.                 "Puma" };
  20.         String editoriales[] = { "Warner", "DC", "Marvel", "Universal", "Fox", "Univision", "Nike", "Adidas",
  21.                 "Columbo", "CineMax" };
  22.         String años[] = { "2016", "2015", "1889", "1256", "2012", "1999", "1978", "1950", "1947","1556" };
  23.    
  24.         opc1 = Helper.menuManualRandom();
  25.         switch (opc1) {
  26.         case 1:
  27.             do {
  28.                 Libro libro = new Libro();
  29.                 libro.setTitulo(Helper.getValidsString("Ingrese Titulo"));
  30.                 libro.setAutor(Helper.getValidsString("Ingrese Autor"));
  31.                 libro.setEditorial(Helper.getValidsString("Ingrese Editorial"));
  32.                 libro.setAñoPublicacion(Helper.getValidsString("Ingrese Año de Publicacion"));
  33.                 libro.setPrecio(Helper.getFloat("Ingrese Precio "));
  34.                 listaLibro.add(libro);
  35.             } while (continuar = Helper.continueCargaLibro());
  36.             mostrarMP(listaLibro);
  37.             break;
  38.         case 2:
  39.             do {
  40.                 Libro libro = new Libro();
  41.                 float precio = aleatorio.nextInt(1000 - 100) ;
  42.                 String tituloAleatorio = titulos[aleatorio.nextInt(10)];
  43.                 libro.setTitulo(tituloAleatorio);
  44.                 String autorAleatorio = autores[aleatorio.nextInt(10)];
  45.                 libro.setAutor(autorAleatorio);
  46.                 String editorialAleatorio = editoriales[aleatorio.nextInt(10)];
  47.                 libro.setEditorial(editorialAleatorio);
  48.                 String añoAleatorio = años[aleatorio.nextInt(10)];
  49.                 libro.setAñoPublicacion(añoAleatorio);
  50.                 libro.setPrecio(precio);
  51.                 listaLibro.add(libro);
  52.                 mostrarLibro(listaLibro);
  53.                 System.out.println("");
  54.             } while (continuar = Helper.continueCargaLibro());
  55.             mostrarMP(listaLibro);
  56.             break;
  57.         }
  58.  
  59.     }
  60.     public static void mostrarMP(ArrayList<Libro> listaLibro) {
  61.         int opc = Helper.menuAutorEditorial();
  62.         mostrarSwitch(listaLibro,opc);
  63.         int num = Helper.forceInteger("Ingrese Precio");
  64.         mostrarMenorPrecio(listaLibro, num);
  65.     }
  66.    
  67.     public static void mostrarSwitch(ArrayList<Libro> listaLibro,int opc) {
  68.         switch (opc) {
  69.         case 1:
  70.             String autor = Helper.getValidsString("Ingrese Autor a buscar: ");
  71.             mostrarAutor(listaLibro, autor);
  72.             break;
  73.         case 2:
  74.             String editorial = Helper.getValidsString("Ingrese Editorial a buscar: ");
  75.             mostrarEditorial(listaLibro, editorial);
  76.             break;
  77.         }
  78.     }
  79.    
  80.     public static void mostrarLibro(ArrayList<Libro> lista) {
  81.         System.out.println("*********Libros Cargados*********");
  82.         for (Libro libro : lista) {
  83.             System.out.print(libro + " ");
  84.             System.out.println();
  85.         }  
  86.     }
  87.     public static void mostrarMenorPrecio(ArrayList<Libro> lista, float precio) {
  88.         boolean encontrado = false;
  89.         for (Libro libro : lista) {
  90.             if (libro.getPrecio() < precio) {
  91.                 encontrado = true;
  92.                 System.out.println("Libros Encontrados Menores a $" + precio + " son: " + libro.toString());
  93.  
  94.             }
  95.         }
  96.         if (!encontrado) {
  97.             System.out.println("Los Libros Menores a $" + precio + " No Existen ");
  98.         }
  99.     }
  100.  
  101.     public static void mostrarAutor(ArrayList<Libro> lista, String autor) {
  102.         boolean encontrado = false;
  103.         for (Libro libro : lista) {
  104.             if (libro.getAutor().equalsIgnoreCase(autor)) {//compara la cadena aunque halla sido en mayuscula
  105.                 encontrado = true;
  106.                 System.out.println("Libro encontrado: " + libro.toString());
  107.             }
  108.         }
  109.         if (!encontrado) {
  110.             System.out.println("El Libro No Existe");
  111.         }
  112.  
  113.     }
  114.  
  115.     public static void mostrarEditorial(ArrayList<Libro> lista, String editorial) {
  116.         boolean encontrado = false;
  117.         for (Libro libro : lista) {
  118.             if (libro.getEditorial().equalsIgnoreCase(editorial)) {
  119.                 encontrado = true;
  120.                 System.out.println("Libro encontrado: " + libro.toString());
  121.                 break;
  122.             }
  123.         }
  124.         if (!encontrado) {
  125.             System.out.println("El Libro No Existe");
  126.         }
  127.     }
  128.  
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment