Advertisement
Martina312

[НП] - Books Collection

Aug 20th, 2020
1,751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 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 String getCategory() {
  26.         return category;
  27.     }
  28.  
  29.     public float getPrice() {
  30.         return price;
  31.     }
  32.  
  33.     @Override
  34.     public String toString() {
  35.         return String.format("%s (%s) %.2f", title, category, price);
  36.     }
  37. }
  38.  
  39. class BookCollection{
  40.     List<Book> books;
  41.  
  42.     public BookCollection() {
  43.         books = new ArrayList<>();
  44.     }
  45.  
  46.     public void addBook(Book book){
  47.         books.add(book);
  48.     }
  49.  
  50.     public void printByCategory(String category){
  51.         books.stream()
  52.                 .filter(book -> book.getCategory().equals(category))
  53.                 .sorted(Comparator.comparing(Book::getTitle).thenComparing(Book::getPrice))
  54.                 .forEach(System.out::println);
  55.     }
  56.  
  57.     public List<Book> getCheapestN(int n){
  58.         return books.stream()
  59.                 .sorted(Comparator.comparing(Book::getPrice).thenComparing(Book::getTitle))
  60.                 .limit(n)
  61.                 .collect(Collectors.toList());
  62.     }
  63. }
  64.  
  65.  
  66.  
  67. public class BooksTest {
  68.     public static void main(String[] args) {
  69.         Scanner scanner = new Scanner(System.in);
  70.         int n = scanner.nextInt();
  71.         scanner.nextLine();
  72.         BookCollection booksCollection = new BookCollection();
  73.         Set<String> categories = fillCollection(scanner, booksCollection);
  74.         System.out.println("=== PRINT BY CATEGORY ===");
  75.         for (String category : categories) {
  76.             System.out.println("CATEGORY: " + category);
  77.             booksCollection.printByCategory(category);
  78.         }
  79.         System.out.println("=== TOP N BY PRICE ===");
  80.         print(booksCollection.getCheapestN(n));
  81.     }
  82.  
  83.     static void print(List<Book> books) {
  84.         for (Book book : books) {
  85.             System.out.println(book);
  86.         }
  87.     }
  88.  
  89.     static TreeSet<String> fillCollection(Scanner scanner,
  90.                                           BookCollection collection) {
  91.         TreeSet<String> categories = new TreeSet<String>();
  92.         while (scanner.hasNext()) {
  93.             String line = scanner.nextLine();
  94.             String[] parts = line.split(":");
  95.             Book book = new Book(parts[0], parts[1], Float.parseFloat(parts[2]));
  96.             collection.addBook(book);
  97.             categories.add(parts[1]);
  98.         }
  99.         return categories;
  100.     }
  101. }
  102.  
  103. // Вашиот код овде
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement