Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.TreeSet;
- import java.util.stream.Collectors;
- class Book {
- private String title;
- private String category;
- private float price;
- public Book(String title, String category, float price) {
- this.title = title;
- this.category = category;
- this.price = price;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public float getPrice() {
- return price;
- }
- public void setPrice(float price) {
- this.price = price;
- }
- public String getCategory() {
- return category;
- }
- public void setCategory(String category) {
- this.category = category;
- }
- public String toString(){
- return String.format("%s (%s) %.2f", title, category, price);
- }
- }
- class BookCollection{
- TreeSet<Book> books;
- public BookCollection(){
- books = new TreeSet<>(Comparator.comparing(Book::getTitle).thenComparing(Book::getPrice));
- }
- public void addBook(Book book){
- books.add(book);
- }
- public void printByCategory(String category){
- books.stream()
- .filter(book -> book.getCategory().toLowerCase().equals(category.toLowerCase()))
- .forEach(System.out::println);
- }
- public List<Book> getCheapestN(int n){
- if(n < books.size()){
- return books.stream()
- .collect(Collectors.toList());
- }
- return books.stream()
- .limit(n)
- .sorted(Comparator.comparing(Book::getPrice))
- .collect(Collectors.toList());
- }
- }
- class BooksTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- scanner.nextLine();
- BookCollection booksCollection = new BookCollection();
- Set<String> categories = fillCollection(scanner, booksCollection);
- System.out.println("=== PRINT BY CATEGORY ===");
- for (String category : categories) {
- System.out.println("CATEGORY: " + category);
- booksCollection.printByCategory(category);
- }
- System.out.println("=== TOP N BY PRICE ===");
- print(booksCollection.getCheapestN(n));
- }
- static void print(List<Book> books) {
- for (Book book : books) {
- System.out.println(book);
- }
- }
- static TreeSet<String> fillCollection(Scanner scanner,
- BookCollection collection) {
- TreeSet<String> categories = new TreeSet<String>();
- while (scanner.hasNext()) {
- String line = scanner.nextLine();
- String[] parts = line.split(":");
- Book book = new Book(parts[0], parts[1], Float.parseFloat(parts[2]));
- collection.addBook(book);
- categories.add(parts[1]);
- }
- return categories;
- }
- }
- // Вашиот код овде
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement