Advertisement
SashkoKlincharov

[Java][НП] - Books collection

Aug 26th, 2021
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1.  
  2.  
  3. Да се напише класа за книга Book во која се чува:
  4.  
  5. наслов
  6. категорија
  7. цена.
  8.  
  9. Да се имплементира конструктор со следните аргументи Book(String title, String category, float price).
  10.  
  11. Потоа да се напише класа BookCollection во која се чува колекција од книги. Во оваа класа треба да се имплментираат следните методи:
  12.  
  13. public void addBook(Book book) - додавање книга во колекцијата
  14. public void printByCategory(String category) - ги печати сите книги од проследената категорија (се споредува стрингот без разлика на мали и големи букви), сортирани според насловот на книгата (ако насловот е ист, се сортираат според цената).
  15. public List<Book> getCheapestN(int n) - враќа листа на најевтините N книги (ако има помалку од N книги во колекцијата, ги враќа сите).
  16.  
  17.  
  18.  
  19. import java.util.*;
  20. import java.util.stream.Collectors;
  21.  
  22. class Book implements Comparable<Book> {
  23. private String title;
  24. private String category;
  25. private float price;
  26.  
  27. public Book(String title, String category, float price) {
  28. this.title = title;
  29. this.category = category;
  30. this.price = price;
  31. }
  32.  
  33. public float getPrice() {
  34. return price;
  35. }
  36.  
  37. public String getTitle() {
  38. return title;
  39. }
  40.  
  41. public String getCategory() {
  42. return category;
  43. }
  44.  
  45. @Override
  46. public String toString() {
  47. return String.format("%s (%s) %.2f",title,category,price);
  48. }
  49.  
  50. @Override
  51. public boolean equals(Object o) {
  52. if (this == o) return true;
  53. if (o == null || getClass() != o.getClass()) return false;
  54. Book book = (Book) o;
  55. return Objects.equals(title, book.title)&&Objects.equals(category, book.category);
  56. }
  57.  
  58. @Override
  59. public int hashCode() {
  60. return Objects.hash(title, category);
  61. }
  62.  
  63. @Override
  64. public int compareTo(Book o) {
  65. int res = this.title.toLowerCase().compareTo(o.title.toLowerCase());
  66. if(res == 0)
  67. return Float.compare(this.price,o.price);
  68. return res;
  69. }
  70. }
  71.  
  72. class BookCollection {
  73. private List<Book> books;
  74.  
  75. public BookCollection() {
  76. books = new ArrayList<>();
  77. }
  78. public void addBook(Book book){
  79. books.add(book);
  80. }
  81. public void printByCategory(String category){
  82. books.stream()
  83. .filter(each -> each
  84. .getCategory()
  85. .equalsIgnoreCase(category))
  86. .sorted(Book::compareTo)
  87. .forEach(System.out::println);
  88. }
  89. public List<Book> getCheapestN(int n){
  90. return books.stream().sorted(Comparator.comparing(Book::getPrice).thenComparing(Book::getTitle)).limit(n).collect(Collectors.toList());
  91.  
  92. }
  93. }
  94.  
  95. public class BooksTest {
  96. public static void main(String[] args) {
  97. Scanner scanner = new Scanner(System.in);
  98. int n = scanner.nextInt();
  99. scanner.nextLine();
  100. BookCollection booksCollection = new BookCollection();
  101. Set<String> categories = fillCollection(scanner, booksCollection);
  102. System.out.println("=== PRINT BY CATEGORY ===");
  103. for (String category : categories) {
  104. System.out.println("CATEGORY: " + category);
  105. booksCollection.printByCategory(category);
  106. }
  107. System.out.println("=== TOP N BY PRICE ===");
  108. print(booksCollection.getCheapestN(n));
  109. }
  110.  
  111. static void print(List<Book> books) {
  112. for (Book book : books) {
  113. System.out.println(book);
  114. }
  115. }
  116.  
  117. static TreeSet<String> fillCollection(Scanner scanner,
  118. BookCollection collection) {
  119. TreeSet<String> categories = new TreeSet<String>();
  120. while (scanner.hasNext()) {
  121. String line = scanner.nextLine();
  122. String[] parts = line.split(":");
  123. Book book = new Book(parts[0], parts[1], Float.parseFloat(parts[2]));
  124. collection.addBook(book);
  125. categories.add(parts[1]);
  126. }
  127. return categories;
  128. }
  129. }
  130.  
  131. // Вашиот код овде
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. Sample input
  140.  
  141. 11
  142. 1984:A:11.3
  143. A Brief History of Time:A:25.66
  144. A Heartbreaking Work of Staggering Genius:A:21.87
  145. A Long Way Gone Memoirs of a Boy Soldier:B:15.18
  146. The Bad Beginning Or, Orphans!:A:12.87
  147. A Wrinkle in Time:B:14.34
  148. Selected Stories, 1968-1994:B:2.54
  149. Alice's Adventures in Wonderland:C:12.34
  150. Angela's Ashes A Memoir:C:18.19
  151. Laugh-Out-Loud Jokes for Kids:C:9.99
  152. Yes Please:C:11.50
  153.  
  154. Sample output
  155.  
  156. === PRINT BY CATEGORY ===
  157. CATEGORY: A
  158. 1984 (A) 11.30
  159. A Brief History of Time (A) 25.66
  160. A Heartbreaking Work of Staggering Genius (A) 21.87
  161. The Bad Beginning Or, Orphans! (A) 12.87
  162. CATEGORY: B
  163. A Long Way Gone Memoirs of a Boy Soldier (B) 15.18
  164. A Wrinkle in Time (B) 14.34
  165. Selected Stories, 1968-1994 (B) 2.54
  166. CATEGORY: C
  167. Alice's Adventures in Wonderland (C) 12.34
  168. Angela's Ashes A Memoir (C) 18.19
  169. Laugh-Out-Loud Jokes for Kids (C) 9.99
  170. Yes Please (C) 11.50
  171. === TOP N BY PRICE ===
  172. Selected Stories, 1968-1994 (B) 2.54
  173. Laugh-Out-Loud Jokes for Kids (C) 9.99
  174. 1984 (A) 11.30
  175. Yes Please (C) 11.50
  176. Alice's Adventures in Wonderland (C) 12.34
  177. The Bad Beginning Or, Orphans! (A) 12.87
  178. A Wrinkle in Time (B) 14.34
  179. A Long Way Gone Memoirs of a Boy Soldier (B) 15.18
  180. Angela's Ashes A Memoir (C) 18.19
  181. A Heartbreaking Work of Staggering Genius (A) 21.87
  182. A Brief History of Time (A) 25.66
  183.  
  184.  
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement