Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- public class PuntoCinco {
- static Scanner scanner = new Scanner(System.in);
- static ArrayList<Libro> librosArray = new ArrayList<Libro>();
- void showRangeLibros(double a, double b)
- {
- int x = 0, y = librosArray.size()-1, m;
- while (x < y)
- {
- m = (x+y)>>1;
- if(a <=librosArray.get(m).getPrecio()) y = m;
- else x = m+1;
- }
- for(int i = x; i < librosArray.size() && librosArray.get(i).getPrecio() <= b; i++)
- {
- System.out.println(librosArray.get(i).toString());
- }
- }
- String readStringLine(String message)
- {
- System.out.println(message);
- String ret = scanner.nextLine();
- return ret;
- }
- double forceReadPositiveDouble(String message)
- {
- double n = 0;
- while(true)
- {
- try
- {
- System.out.println(message);
- n = Double.parseDouble(scanner.next());
- if(n > 0 ) return n;
- System.out.println("El número a cargar debe ser positivo.");
- continue;
- }
- catch(Exception ex)
- {
- System.out.println("Se introdujo un número inválido, reintente.");
- }
- }
- }
- int forceReadPositiveInt(String message)
- {
- int n = 0;
- while(true)
- {
- try
- {
- System.out.println(message);
- n = Integer.parseInt(scanner.next());
- if(n > 0 ) return n;
- System.out.println("El número a cargar debe ser positivo.");
- continue;
- }
- catch(Exception ex)
- {
- System.out.println("Se introdujo un número inválido, reintente.");
- }
- }
- }
- void addLibros()
- {
- while(true)
- {
- 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:")));
- System.out.println("¿Continuará con la carga? (S/N)");
- if(scanner.next().charAt(0) == 'N') break;
- scanner.nextLine();
- }
- Collections.sort(librosArray);
- }
- public static void main(String[] args) {
- PuntoCinco app = new PuntoCinco();
- app.run();
- }
- void run()
- {
- addLibros();
- for(Libro a : librosArray)
- {
- System.out.println(a);
- }
- double fromPrice = forceReadPositiveDouble("Introduzca el mínimo del rango:");
- while(true)
- {
- double endPrice = forceReadPositiveDouble("Introduzca el máximo del rango:");
- if(endPrice >= fromPrice)
- {
- showRangeLibros(fromPrice, endPrice);
- break;
- }
- System.out.println("El máximo del rango debe ser mayor o igual al mínimo del rango.");
- }
- }
- }
Add Comment
Please, Sign In to add comment