WallHero

TP01 Punto Cinco MainClass

Sep 17th, 2020 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Scanner;
  4.  
  5. public class PuntoCinco {
  6.    
  7.     static Scanner scanner = new Scanner(System.in);
  8.     static ArrayList<Libro> librosArray = new ArrayList<Libro>();
  9.    
  10.     void showRangeLibros(double a, double b)
  11.     {
  12.         int x = 0, y = librosArray.size()-1, m;
  13.         while (x < y)
  14.         {
  15.             m = (x+y)>>1;
  16.             if(a <=librosArray.get(m).getPrecio()) y = m;
  17.             else x = m+1;
  18.         }
  19.        
  20.         for(int i = x; i < librosArray.size() && librosArray.get(i).getPrecio() <= b; i++)
  21.         {
  22.             System.out.println(librosArray.get(i).toString());
  23.         }
  24.     }
  25.    
  26.     String readStringLine(String message)
  27.     {
  28.         System.out.println(message);
  29.         String ret = scanner.nextLine();
  30.         return ret;
  31.     }
  32.    
  33.     double forceReadPositiveDouble(String message)
  34.     {
  35.         double n = 0;
  36.         while(true)
  37.         {
  38.             try
  39.             {
  40.                 System.out.println(message);
  41.                 n = Double.parseDouble(scanner.next());
  42.                 if(n > 0 ) return n;
  43.                 System.out.println("El número a cargar debe ser positivo.");
  44.                 continue;
  45.             }
  46.             catch(Exception ex)
  47.             {
  48.                 System.out.println("Se introdujo un número inválido, reintente.");
  49.             }  
  50.         }
  51.     }  
  52.    
  53.    
  54.     int forceReadPositiveInt(String message)
  55.     {
  56.         int n = 0;
  57.         while(true)
  58.         {
  59.             try
  60.             {
  61.                 System.out.println(message);
  62.                 n = Integer.parseInt(scanner.next());
  63.                 if(n > 0 ) return n;
  64.                 System.out.println("El número a cargar debe ser positivo.");
  65.                 continue;
  66.             }
  67.             catch(Exception ex)
  68.             {
  69.                 System.out.println("Se introdujo un número inválido, reintente.");
  70.             }  
  71.         }
  72.     }  
  73.    
  74.     void addLibros()
  75.     {
  76.         while(true)
  77.         {
  78.             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:")));
  79.             System.out.println("¿Continuará con la carga? (S/N)");
  80.             if(scanner.next().charAt(0) == 'N') break;
  81.             scanner.nextLine();
  82.         }
  83.         Collections.sort(librosArray);
  84.     }
  85.    
  86.     public static void main(String[] args) {
  87.         PuntoCinco app = new PuntoCinco();
  88.         app.run();
  89.  
  90.        
  91.     }
  92.    
  93.     void run()
  94.     {
  95.         addLibros();
  96.         for(Libro a : librosArray)
  97.         {
  98.             System.out.println(a);
  99.         }
  100.         double fromPrice = forceReadPositiveDouble("Introduzca el mínimo del rango:");
  101.         while(true)
  102.         {
  103.             double endPrice = forceReadPositiveDouble("Introduzca el máximo del rango:");
  104.             if(endPrice >= fromPrice)
  105.             {
  106.                 showRangeLibros(fromPrice, endPrice);
  107.                 break;
  108.             }
  109.             System.out.println("El máximo del rango debe ser mayor o igual al mínimo del rango.");
  110.  
  111.         }
  112.     }
  113.    
  114. }
  115.  
Add Comment
Please, Sign In to add comment