Advertisement
popovstefan

[NP 2 kol.] Books collection

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