Advertisement
WallHero

Punto cinco

Sep 7th, 2020 (edited)
1,238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.Scanner;
  5.  
  6. public class PuntoCinco {
  7.    
  8.     static Scanner scanner = new Scanner(System.in);
  9.     static ArrayList<Libro> librosArray = new ArrayList<Libro>();
  10.    
  11.    
  12.    
  13.    
  14.    
  15.     static void showRangeLibros(double a, double b)
  16.     {
  17.         int x = 0, y = librosArray.size()-1, m;
  18.         while (x < y)
  19.         {
  20.             m = (x+y)>>1;
  21.             if(a <=librosArray.get(m).getPrecio()) y = m;
  22.             else x = m+1;
  23.         }
  24.        
  25.         for(int i = x; i < librosArray.size() && librosArray.get(i).getPrecio() <= b; i++)
  26.         {
  27.             System.out.println(librosArray.get(i).toString());
  28.         }
  29.     }
  30.    
  31.     static String readStringLine(String message)
  32.     {
  33.         System.out.println(message);
  34.         String ret = scanner.nextLine();
  35.         return ret;
  36.     }
  37.    
  38.     static double forceReadPositiveDouble(String message)
  39.     {
  40.         double n = 0;
  41.         while(true)
  42.         {
  43.             try
  44.             {
  45.                 System.out.println(message);
  46.                 n = Double.parseDouble(scanner.next());
  47.                 if(n > 0 ) return n;
  48.                 System.out.println("El número a cargar debe ser positivo.");
  49.                 continue;
  50.             }
  51.             catch(Exception ex)
  52.             {
  53.                 System.out.println("Se introdujo un número inválido, reintente.");
  54.             }  
  55.         }
  56.     }  
  57.    
  58.    
  59.     static int forceReadPositiveInt(String message)
  60.     {
  61.         int n = 0;
  62.         while(true)
  63.         {
  64.             try
  65.             {
  66.                 System.out.println(message);
  67.                 n = Integer.parseInt(scanner.next());
  68.                 if(n > 0 ) return n;
  69.                 System.out.println("El número a cargar debe ser positivo.");
  70.                 continue;
  71.             }
  72.             catch(Exception ex)
  73.             {
  74.                 System.out.println("Se introdujo un número inválido, reintente.");
  75.             }  
  76.         }
  77.     }  
  78.    
  79.     public static void addLibros()
  80.     {
  81.         while(true)
  82.         {
  83.             librosArray.add(new Libro(readStringLine("Introduzca el título:"),readStringLine("Introduzca el nombre del autor:"), readStringLine("Introduzca la editorial:"), forceReadPositiveInt("Año de publicación:"), forceReadPositiveDouble("Precio:")));
  84.             System.out.println("¿Continuará con la carga? (S/N)");
  85.             if(scanner.next().charAt(0) == 'N') break;
  86.             scanner.nextLine();
  87.         }
  88.     }
  89.    
  90.     public static void main(String[] args) {
  91.         addLibros();
  92.         Collections.sort(librosArray);
  93.         for(Libro a : librosArray)
  94.         {
  95.             System.out.println(a);
  96.         }
  97.         double fromPrice = forceReadPositiveDouble("Introduzca el mínimo del rango:");
  98.         while(true)
  99.         {
  100.             double endPrice = forceReadPositiveDouble("Introduzca el máximo del rango:");
  101.             if(endPrice >= fromPrice)
  102.             {
  103.                 showRangeLibros(fromPrice, endPrice);
  104.                 break;
  105.             }
  106.             System.out.println("El máximo del rango debe ser mayor o igual al mínimo del rango.");
  107.  
  108.         }
  109.        
  110.     }
  111.  
  112.  
  113. }
  114.  
  115. class Libro implements Comparable<Libro>
  116. {
  117.     private String titulo;
  118.     private String autor;
  119.     private String editorial;
  120.     private int anoPublicacion;
  121.     private double precio;
  122.    
  123.     public Libro() {
  124.     }  
  125.    
  126.    
  127.     public Libro(String titulo, String autor, String editorial, int anoPublicacion, double precio)
  128.     {
  129.         this.titulo = titulo;
  130.         this.autor = autor;
  131.         this.editorial = editorial;
  132.         this.anoPublicacion = anoPublicacion;
  133.         this.precio = precio;
  134.     }
  135.    
  136.     @Override
  137.     public String toString() {
  138.         return "Título: "+ this.getTitulo() +"\nAutor:"+ this.getAutor()+ "\nEditorial:" + getEditorial() + "\nAño de publicación: " + anoPublicacion + "\nPrecio:" + precio;
  139.     }
  140.  
  141.     @Override
  142.     public int compareTo(Libro libro)
  143.     {
  144.         if(this.getPrecio() > libro.getPrecio()) return 1;
  145.         if(this.getPrecio() < libro.getPrecio()) return -1;
  146.         return 0;
  147.     }
  148.     public String getTitulo() {
  149.         return titulo;
  150.     }
  151.  
  152.  
  153.     public void setTitulo(String titulo) {
  154.         this.titulo = titulo;
  155.     }
  156.  
  157.  
  158.     public String getAutor() {
  159.         return autor;
  160.     }
  161.  
  162.  
  163.     public void setAutor(String autor) {
  164.         this.autor = autor;
  165.     }
  166.  
  167.  
  168.     public String getEditorial() {
  169.         return editorial;
  170.     }
  171.  
  172.  
  173.     public void setEditorial(String editorial) {
  174.         this.editorial = editorial;
  175.     }
  176.    
  177.     public int getAnoPublicacion() {
  178.         return anoPublicacion;
  179.     }
  180.    
  181.     public void setAnoPublicacion(int anoPublicacion) {
  182.         this.anoPublicacion = anoPublicacion;
  183.     }
  184.    
  185.     public double getPrecio() {
  186.         return precio;
  187.     }
  188.    
  189.     public void setPrecio(double precio) {
  190.         this.precio = precio;
  191.     }
  192.    
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement