Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ooks collection Problem 5 (2 / 5)
- Да се напише класа за книга Book во која се чува:
- наслов
- категорија
- цена.
- Да се имплементира конструктор со следните аргументи Book(String title, String category, float price).
- Потоа да се напише класа BookCollection во која се чува колекција од книги. Во оваа класа треба да се имплментираат следните методи:
- public void addBook(Book book) - додавање книга во колекцијата
- public void printByCategory(String category) - ги печати сите книги од проследената категорија (се споредува стрингот без разлика на мали и големи букви), сортирани според насловот на книгата (ако насловот е ист, се сортираат според цената).
- public List<Book> getCheapestN(int n) - враќа листа на најевтините N книги (ако има помалку од N книги во колекцијата, ги враќа сите).
- ==============================================================================================================
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.TreeSet;
- import java.util.function.Predicate;
- import java.util.stream.Collectors;
- class Book implements Comparable<Book> {
- private final String title, category;
- private final Float price;
- public Book(String title, String category, float price) {
- this.title = title;
- this.category = category;
- this.price = price;
- }
- public String getTitle() {
- return this.title;
- }
- public String getCategory() {
- return this.category;
- }
- public Float getPrice() {
- return this.price;
- }
- @Override
- public int compareTo(Book arg0) {
- if (this.title.compareToIgnoreCase(arg0.title) == 0) {
- return this.price.compareTo(arg0.price);
- }
- else {
- return this.title.compareToIgnoreCase(arg0.title);
- }
- }
- @Override
- public String toString() {
- return String.format("%s (%s) %.2f", title, category, price);
- }
- }
- class BookCollection {
- private ArrayList<Book> books;
- public BookCollection() {
- books = new ArrayList<>();
- }
- public void addBook(Book book) {
- this.books.add(book);
- }
- public void printByCategory(String category) {
- Predicate<Book> predicate = book -> book.getCategory().equalsIgnoreCase(category);
- this.books.stream()
- .filter(predicate)
- .sorted()
- .forEach(System.out :: println);
- }
- public List<Book> getCheapestN(int n) {
- return this.books.stream()
- .sorted((b1, b2) -> {
- if (b1.getPrice().compareTo(b2.getPrice()) == 0)
- return b1.getTitle().compareTo(b2.getTitle());
- else
- return b1.getPrice().compareTo(b2.getPrice());
- })
- .limit(n)
- .collect(Collectors.toList());
- }
- }
- public 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;
- }
- }
- // Вашиот код овде
- ==================================================================================================================
- Sample input
- 11
- 1984:A:11.3
- A Brief History of Time:A:25.66
- A Heartbreaking Work of Staggering Genius:A:21.87
- A Long Way Gone Memoirs of a Boy Soldier:B:15.18
- The Bad Beginning Or, Orphans!:A:12.87
- A Wrinkle in Time:B:14.34
- Selected Stories, 1968-1994:B:2.54
- Alice's Adventures in Wonderland:C:12.34
- Angela's Ashes A Memoir:C:18.19
- Laugh-Out-Loud Jokes for Kids:C:9.99
- Yes Please:C:11.50
- ==================================================================================================================
- Sample output
- === PRINT BY CATEGORY ===
- CATEGORY: A
- 1984 (A) 11.30
- A Brief History of Time (A) 25.66
- A Heartbreaking Work of Staggering Genius (A) 21.87
- The Bad Beginning Or, Orphans! (A) 12.87
- CATEGORY: B
- A Long Way Gone Memoirs of a Boy Soldier (B) 15.18
- A Wrinkle in Time (B) 14.34
- Selected Stories, 1968-1994 (B) 2.54
- CATEGORY: C
- Alice's Adventures in Wonderland (C) 12.34
- Angela's Ashes A Memoir (C) 18.19
- Laugh-Out-Loud Jokes for Kids (C) 9.99
- Yes Please (C) 11.50
- === TOP N BY PRICE ===
- Selected Stories, 1968-1994 (B) 2.54
- Laugh-Out-Loud Jokes for Kids (C) 9.99
- 1984 (A) 11.30
- Yes Please (C) 11.50
- Alice's Adventures in Wonderland (C) 12.34
- The Bad Beginning Or, Orphans! (A) 12.87
- A Wrinkle in Time (B) 14.34
- A Long Way Gone Memoirs of a Boy Soldier (B) 15.18
- Angela's Ashes A Memoir (C) 18.19
- A Heartbreaking Work of Staggering Genius (A) 21.87
- A Brief History of Time (A) 25.66
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement