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 authorId;
  10. String authorName;
  11.  
  12. public String getAuthorName(){
  13. return authorName;
  14. }
  15.  
  16. public int getAuthorId(){
  17. return authorId;
  18. }
  19. Author(int authorId, String authorName){
  20. this.authorId = authorId;
  21. this.authorName = authorName;
  22. }
  23. }
  24.  
  25. class Book{
  26. int bookId;
  27. String title;
  28. String genre;
  29. double price;
  30. Author author;
  31.  
  32. public int getBookId(){
  33. return bookId;
  34. }
  35.  
  36. public String getTitle(){
  37. return title;
  38. }
  39.  
  40. public String getGenre(){
  41. return genre;
  42. }
  43.  
  44. public double getPrice(){
  45. return price;
  46. }
  47.  
  48. public Author getAuthor(){
  49. return author;
  50. }
  51.  
  52. Book(Author author, int bookId, String title, String genre, double price){
  53. this.bookId = bookId;
  54. this.title = title;
  55. this.genre = genre;
  56. this.price = price;
  57. this.author = author;
  58. }
  59. }
  60.  
  61. class Ideone
  62. {
  63. public static ArrayList<Book> getBooksBelongingToGenre(Book[] books, String genre){
  64. ArrayList<Book> ls = new ArrayList<>();
  65. for(int i = 0; i < books.length; i++){
  66. if(books[i].getGenre().equalsIgnoreCase(genre)){
  67. ls.add(books[i]);
  68. }
  69. }
  70. if(ls.size() == 0){
  71. return null;
  72. }
  73. return ls;
  74. }
  75.  
  76. public static ArrayList<Book> calculateDiscountedPrice(Book[] books, String genre, int discount){
  77. ArrayList<Book> ls = new ArrayList<>();
  78. for(int i = 0; i < books.length; i++){
  79. if(books[i].getGenre().equalsIgnoreCase(genre)){
  80. ls.add(books[i]);
  81. }
  82. }
  83. if(ls.size() == 0){
  84. return null;
  85. }
  86. return ls;
  87. }
  88.  
  89. public static void main (String[] args) throws java.lang.Exception
  90. {
  91. // your code goes here
  92. Scanner sc = new Scanner(System.in);
  93. int n = sc.nextInt();
  94. Book[] books = new Book[n];
  95. sc.nextLine();
  96. for(int i = 0; i < n; i++){
  97. int a = sc.nextInt();
  98. sc.nextLine();
  99. String b = sc.nextLine();
  100. int c = sc.nextInt();
  101. sc.nextLine();
  102. String d = sc.nextLine();
  103. String e = sc.nextLine();
  104. double f = sc.nextDouble();
  105. sc.nextLine();
  106. books[i] = new Book(new Author(a, b), c, d, e, f);
  107. }
  108.  
  109. String genre1 = sc.nextLine();
  110. String genre2 = sc.nextLine();
  111. int discount = sc.nextInt();
  112.  
  113.  
  114. ArrayList<Book> list1 = getBooksBelongingToGenre(books, genre1);
  115.  
  116. if(list1 == null){
  117. System.out.println("Genre is not available");
  118. }else{
  119. for(Book b : list1){
  120. Author a = b.getAuthor();
  121. System.out.print("AuthorName: " + a.getAuthorName() + ", Title: " + b.getTitle());
  122. System.out.println();
  123. }
  124. }
  125.  
  126. System.out.println();
  127.  
  128.  
  129. ArrayList<Book> list2 = calculateDiscountedPrice(books, genre2, discount);
  130.  
  131. if(list2 == null){
  132. System.out.println("Discounted books are unavailable in the given genre.");
  133. }else{
  134. for(Book b : list2){
  135. Author a = b.getAuthor();
  136. double price = b.getPrice();
  137. double newPrice = (double) (price - (price * discount / 100));
  138. System.out.print("AuthorName: " + a.getAuthorName() + ", Title:" + b.getTitle() + ", Price:" + newPrice);
  139. System.out.println();
  140. }
  141. }
  142.  
  143. }
  144. }
Success #stdin #stdout 0.25s 61552KB
stdin
4
101
J.K. Rowling
201
Harry Potter
Fantasy
500
102
Agatha Christie
202
Murder on the Orient Express
Crime
400
103
Dan Brown
203
The Da Vinci Code
Thriller
600
104
George Orwell
204
1984
Dystopian
300
Crime
Fantasy
10
stdout
AuthorName: Agatha Christie, Title: Murder on the Orient Express

AuthorName: J.K. Rowling, Title:Harry Potter, Price:450.0