You are on page 1of 2

import java.io.

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

class Book
{
int id;
String title;
String author;
double price;

Book(int id, String title, String author, double price)


{
this.id = id;
this.title = title;
this.author = author;
this.price = price;
}

public int getId()


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

class Solution
{
public static void main(String[] ar)
{
Book[] book = new Book[4];
Scanner sc = new Scanner(System.in);

for(int i = 0; i<book.length; i++)


{
int id = sc.nextInt(); sc.nextLine();
String title = sc.nextLine();
String author = sc.nextLine();
Double price = sc.nextDouble();

book[i] = new Book(id, title, author, price);


}
sortBookbyPrice(book);

for(Book b:book)
{
System.out.println(b.getId() + " " + b.getTitle() + " " + b.getAuthor() + " " +
b.getPrice());
}
}

public static void sortBookbyPrice(Book[] book)


{
for(int i =0; i<book.length;i++)
{
for(int j = 0;j<i;j++)
{
if(book[i].getPrice() < book[j].getPrice())
{
Book temp = book[i];
book[i] = book[j];
book[j] = temp;
}
}
}
}
}

You might also like