Advertisement
NenadKocev

[НП] Books collection

Jan 22nd, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.Set;
  7. import java.util.TreeSet;
  8. import java.util.stream.Collectors;
  9.  
  10. class Book {
  11.     private String title;
  12.     private String category;
  13.     private float price;
  14.  
  15.     public Book(String title, String category, float price) {
  16.         this.title = title;
  17.         this.category = category;
  18.         this.price = price;
  19.     }
  20.  
  21.     public String getTitle() {
  22.         return title;
  23.     }
  24.  
  25.     public void setTitle(String title) {
  26.         this.title = title;
  27.     }
  28.  
  29.     public float getPrice() {
  30.         return price;
  31.     }
  32.  
  33.     public void setPrice(float price) {
  34.         this.price = price;
  35.     }
  36.  
  37.     public String getCategory() {
  38.         return category;
  39.     }
  40.  
  41.     public void setCategory(String category) {
  42.         this.category = category;
  43.     }
  44.  
  45.     public String toString(){
  46.         return String.format("%s (%s) %.2f", title, category, price);
  47.     }
  48. }
  49.  
  50. class BookCollection{
  51.     TreeSet<Book> books;
  52.  
  53.     public BookCollection(){
  54.         books = new TreeSet<>(Comparator.comparing(Book::getTitle).thenComparing(Book::getPrice));
  55.     }
  56.  
  57.     public void addBook(Book book){
  58.         books.add(book);
  59.     }
  60.  
  61.     public void printByCategory(String category){
  62.         books.stream()
  63.                 .filter(book -> book.getCategory().toLowerCase().equals(category.toLowerCase()))
  64.                 .forEach(System.out::println);
  65.     }
  66.  
  67.     public List<Book> getCheapestN(int n){
  68.         if(n < books.size()){
  69.             return books.stream()
  70.                     .collect(Collectors.toList());
  71.         }
  72.         return books.stream()
  73.                 .limit(n)
  74.                 .sorted(Comparator.comparing(Book::getPrice))
  75.                 .collect(Collectors.toList());
  76.     }
  77. }
  78.  
  79. class BooksTest {
  80.     public static void main(String[] args) {
  81.         Scanner scanner = new Scanner(System.in);
  82.         int n = scanner.nextInt();
  83.         scanner.nextLine();
  84.         BookCollection booksCollection = new BookCollection();
  85.         Set<String> categories = fillCollection(scanner, booksCollection);
  86.         System.out.println("=== PRINT BY CATEGORY ===");
  87.         for (String category : categories) {
  88.             System.out.println("CATEGORY: " + category);
  89.             booksCollection.printByCategory(category);
  90.         }
  91.         System.out.println("=== TOP N BY PRICE ===");
  92.         print(booksCollection.getCheapestN(n));
  93.     }
  94.  
  95.     static void print(List<Book> books) {
  96.         for (Book book : books) {
  97.             System.out.println(book);
  98.         }
  99.     }
  100.  
  101.     static TreeSet<String> fillCollection(Scanner scanner,
  102.                                           BookCollection collection) {
  103.         TreeSet<String> categories = new TreeSet<String>();
  104.         while (scanner.hasNext()) {
  105.             String line = scanner.nextLine();
  106.             String[] parts = line.split(":");
  107.             Book book = new Book(parts[0], parts[1], Float.parseFloat(parts[2]));
  108.             collection.addBook(book);
  109.             categories.add(parts[1]);
  110.         }
  111.         return categories;
  112.     }
  113. }
  114.  
  115. // Вашиот код овде
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement