Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Scanner;
- public class PuntoCinco {
- static Scanner scanner = new Scanner(System.in);
- static ArrayList<Libro> librosArray = new ArrayList<Libro>();
- static 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());
- }
- }
- static String readStringLine(String message)
- {
- System.out.println(message);
- String ret = scanner.nextLine();
- return ret;
- }
- static 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.");
- }
- }
- }
- static 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.");
- }
- }
- }
- public static 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();
- }
- }
- public static void main(String[] args) {
- addLibros();
- Collections.sort(librosArray);
- 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.");
- }
- }
- }
- class Libro implements Comparable<Libro>
- {
- private String titulo;
- private String autor;
- private String editorial;
- private int anoPublicacion;
- private double precio;
- public Libro() {
- }
- public Libro(String titulo, String autor, String editorial, int anoPublicacion, double precio)
- {
- this.titulo = titulo;
- this.autor = autor;
- this.editorial = editorial;
- this.anoPublicacion = anoPublicacion;
- this.precio = precio;
- }
- @Override
- public String toString() {
- return "Título: "+ this.getTitulo() +"\nAutor:"+ this.getAutor()+ "\nEditorial:" + getEditorial() + "\nAño de publicación: " + anoPublicacion + "\nPrecio:" + precio;
- }
- @Override
- public int compareTo(Libro libro)
- {
- if(this.getPrecio() > libro.getPrecio()) return 1;
- if(this.getPrecio() < libro.getPrecio()) return -1;
- return 0;
- }
- public String getTitulo() {
- return titulo;
- }
- public void setTitulo(String titulo) {
- this.titulo = titulo;
- }
- public String getAutor() {
- return autor;
- }
- public void setAutor(String autor) {
- this.autor = autor;
- }
- public String getEditorial() {
- return editorial;
- }
- public void setEditorial(String editorial) {
- this.editorial = editorial;
- }
- public int getAnoPublicacion() {
- return anoPublicacion;
- }
- public void setAnoPublicacion(int anoPublicacion) {
- this.anoPublicacion = anoPublicacion;
- }
- public double getPrecio() {
- return precio;
- }
- public void setPrecio(double precio) {
- this.precio = precio;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement