Advertisement
Dimitrija

Books collection

Feb 13th, 2022
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. class Book implements Comparable<Book>{
  5.     private String title;
  6.     private String category;
  7.     private float price;
  8.  
  9.     public float getPrice() {
  10.         return price;
  11.     }
  12.  
  13.     public String getBookCategory() {
  14.         return category;
  15.     }
  16.  
  17.     Book(String title, String category, float price){
  18.         this.title = title;
  19.         this.category = category;
  20.         this.price = price;
  21.     }
  22.  
  23.     @Override
  24.     public int compareTo( Book o) {
  25.             if (this.title.toLowerCase().equals(o.title.toLowerCase() )){
  26.                 return Float.compare(this.price,o.price);
  27.             }
  28.             else{
  29.                 return this.title.toLowerCase().compareTo(o.title.toLowerCase());
  30.             }
  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.  
  41.     List<Book> bookCollection;
  42.  
  43.     public BookCollection() {
  44.         bookCollection = new LinkedList<>();
  45.     }
  46.  
  47.     public void addBook(Book book){
  48.         bookCollection.add(book);
  49.     }
  50.     public void printByCategory(String category){
  51.         List<Book> copyCollection = bookCollection.stream()
  52.                 .filter(x -> x.getBookCategory().equals(category))
  53.                 .sorted().collect(Collectors.toList());
  54.  
  55.         copyCollection.forEach(System.out::println);
  56.  
  57.     }
  58.     public List<Book> getCheapestN(int n){
  59.  
  60.         Comparator<Book> comparator = Comparator.comparing(Book::getPrice);
  61.  
  62.         return bookCollection.stream()
  63.                 .sorted()
  64.                 .sorted(comparator)
  65.                 .limit(n)
  66.                 .collect(Collectors.toList());
  67.  
  68.     }
  69. }
  70.  
  71.  
  72.  
  73. public class BooksTest {
  74.     public static void main(String[] args) {
  75.         Scanner scanner = new Scanner(System.in);
  76.         int n = scanner.nextInt();
  77.         scanner.nextLine();
  78.         BookCollection booksCollection = new BookCollection();
  79.         Set<String> categories = fillCollection(scanner, booksCollection);
  80.         System.out.println("=== PRINT BY CATEGORY ===");
  81.         for (String category : categories) {
  82.             System.out.println("CATEGORY: " + category);
  83.             booksCollection.printByCategory(category);
  84.         }
  85.         System.out.println("=== TOP N BY PRICE ===");
  86.         print(booksCollection.getCheapestN(n));
  87.     }
  88.  
  89.     static void print(List<Book> books) {
  90.         for (Book book : books) {
  91.             System.out.println(book);
  92.         }
  93.     }
  94.  
  95.     static TreeSet<String> fillCollection(Scanner scanner,
  96.                                           BookCollection collection) {
  97.         TreeSet<String> categories = new TreeSet<String>();
  98.         while (scanner.hasNext()) {
  99.             String line = scanner.nextLine();
  100.             String[] parts = line.split(":");
  101.             Book book = new Book(parts[0], parts[1], Float.parseFloat(parts[2]));
  102.             collection.addBook(book);
  103.             categories.add(parts[1]);
  104.         }
  105.         return categories;
  106.     }
  107. }
  108.  
  109. // Вашиот код овде
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement