fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Author{
  9. int author;
  10. String authorName;
  11. Author(int author, String authorName){
  12. this.authorName = authorName;
  13. this.author = author;
  14. }
  15. }
  16.  
  17. class Book{
  18. int bookId;
  19. String title;
  20. String genre;
  21. double price;
  22. Author author;
  23. Book(int bookId, String title, String genre, double price, Author author){
  24. this.bookId = bookId;
  25. this.title = title;
  26. this.genre = genre;
  27. this.price = price;
  28. this.author = author;
  29. }
  30. }
  31.  
  32. class Ideone
  33. {
  34. public static ArrayList<Book> getBooksByGenre(Book[] books, String genre){
  35. ArrayList<Book> ls = new ArrayList<>();
  36. for(Book b : books){
  37. if(b.genre.equalsIgnoreCase(genre)){
  38. ls.add(b);
  39. }
  40. }
  41. if(ls.size() == 0){
  42. return null;
  43. }
  44. return ls;
  45. }
  46. public static Book getHighestPriceBook(Book[] books){
  47. if(books.length == 0) return null;
  48.  
  49. Book max = books[0];
  50.  
  51. for(Book b : books){
  52. if(b.price > max.price){
  53. max = b;
  54. }
  55. }
  56. return max;
  57. }
  58.  
  59. public static void main (String[] args) throws java.lang.Exception
  60. {
  61. // your code goes here
  62. Scanner sc = new Scanner(System.in);
  63. Integer n = sc.nextInt(); sc.nextLine();
  64.  
  65. Book[] books = new Book[n];
  66.  
  67. for(int i = 0; i < n; i++){
  68. Integer a = sc.nextInt(); sc.nextLine();
  69. String b = sc.nextLine();
  70.  
  71. Integer c = sc.nextInt(); sc.nextLine();
  72. String d = sc.nextLine();
  73. String e = sc.nextLine();
  74. double f = sc.nextDouble(); sc.nextLine();
  75.  
  76. books[i] = new Book(c,d, e, f ,new Author(a, b));
  77. }
  78.  
  79. String genre1 = sc.nextLine();
  80.  
  81.  
  82. ArrayList<Book> list1 = getBooksByGenre(books, genre1);
  83.  
  84. if(list1 == null){
  85. System.out.println("Genre not found");
  86. }else{
  87. for(Book b : list1){
  88. System.out.println("AuthorName: " + b.author.authorName + ", Title: " + b.title);
  89. }
  90. }
  91.  
  92. Book b = getHighestPriceBook(books);
  93. if(b == null){
  94. System.out.print("No books available");
  95. }else{
  96. System.out.println("Highest Priced Book: ");
  97. System.out.println("AuthorName: " + b.author.authorName + ", Title: " + b.title + ", Price: " + b.price);
  98. }
  99. }
  100. }
Success #stdin #stdout 0.27s 61388KB
stdin
3
101
R.K Narayan
201
Malgudi Days
Fiction
500
102
APJ Abdul Kalam
202
Wings of Fire
Autobiography
650
103
Chetan Bhagat
203
2 States
Fiction
400
Fiction
stdout
AuthorName: R.K Narayan, Title: Malgudi Days
AuthorName: Chetan Bhagat, Title: 2 States
Highest Priced Book: 
AuthorName: APJ Abdul Kalam, Title: Wings of Fire, Price: 650.0