Sort Books By Price - TCS Xplore Array Hands 2 Java Solution






import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class SortBooksByPrice {
    final static Scanner sc = new Scanner(System.in);
    public static void main(String args[] ) throws Exception {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        ArrayList<Book> book = new ArrayList<>();
        int id;
        String title; 
        String author;
        double price;
        
        for (int i = 0; i < 4; i++) {
            id = sc.nextInt();
            sc.nextLine();
            title = sc.nextLine();
            author = sc.nextLine();
            price = sc.nextDouble(); 
            book.add(new Book(id, title, author, price));
        }
        
        //Sorting Collection
        Collections.sort(book, new Comparator<Book>(){

          public int compare(Book o1, Book o2)
          {
             return Double.compare(o1.getPrice(), o2.getPrice());
          }
        });
        
        //Output
        for(int i = 0; i < book.size(); i++) {
        System.out.println(book.get(i).getId() + " " + book.get(i).getTitle() + " " + book.get(i).getAuthor() + " " + book.get(i).getPrice());
        }
    }
}

class Book
{
    private int id;
    private String title;
    private String author;
    private double price;
    
    public Book(int id, String title, String author, double price){
         this.id = id;
         this.title = title;
         this.author = author;
         this.price = price;
    }
    public void setId(int id){
        this.id = id;
    }
    
    public int getId(){
        return id;
    }
    
    public void setTitle(String title){
        this.title = title;
    }
    
    public String getTitle(){
        return title;
    }
    
    public void setAuthor(String author){
        this.author = author;
    }
    
    public String getAuthor(){
        return author;
    }
    
    public void setPrice(double price){
        this.price = price;
    }
    
    public double getPrice(){
        return price;
    }
}

Comments