Hello.java
import java.util.*;
public class Hello {
private static Map<Integer, List<Book>> getYearwiseBooks(List<Book> books) {
Map<Integer,List<Book>> map=new HashMap<>();
for(int i=0;i<books.size();++i){
if(!map.containsKey(books.get(i).pub_yr))
map.put(books.get(i).getYear(),new ArrayList<>());
map.get(books.get(i).getYear()).add(books.get(i));
}
for(int yr: map.keySet()){
List<Book> list= map.get(yr);
list.sort((Book a,Book b)->{
int aPrice=a.getPrice(),bPrice=b.getPrice(),aRating=a.getRating(),bRating=b.getRating(),aYr=a.getYear(),bYr=b.getYear();
if(aPrice==bPrice){
if(aRating == bRating){
if(aYr == bYr){
return a.getTitle().compareTo(b.getTitle());
}
else{
return aYr - bYr;
}
}
else{
return bRating-aRating;
}
}
else{
return aPrice - bPrice;
}
});
}
return map;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
sc.nextLine();
List<Book> books = new ArrayList<>();
for (int ctr = 1; ctr <= N; ctr++) {
Book book = new Book();
book.setTitle(sc.next());
book.setISBN(sc.next());
book.setPrice(sc.nextInt());
book.setRating(sc.nextInt());
book.setYear(sc.nextInt());
books.add(book);
sc.nextLine();
}
Map<Integer, List<Book>> yearWiseBooks = getYearwiseBooks(books);
int year = sc.nextInt();
for (Book book : yearWiseBooks.get(year)) {
StringBuilder sb = new StringBuilder();
sb.append(book.getTitle()).append(" ");
sb.append(book.getISBN()).append(" ");
sb.append(book.getPrice()).append(" ");
sb.append(book.getYear()).append(" ");
sb.append(book.getRating());
System.out.println(sb.toString());
}
}//end of main
}//end of class Hello
-----------------------------------------------------
0 Comments