Skip to content

Instantly share code, notes, and snippets.

@Furkan-Gulsen
Created May 21, 2022 07:26
Show Gist options
  • Save Furkan-Gulsen/90c37c7393f783e3c14888de86148767 to your computer and use it in GitHub Desktop.
Save Furkan-Gulsen/90c37c7393f783e3c14888de86148767 to your computer and use it in GitHub Desktop.
Patika Java 102 - Ödev - Kitap Sıralayıcı
import java.util.Comparator;
import java.util.TreeSet;
class Book implements Comparable<Book>{
private final String name;
private final int pageNumber;
private final String author;
private final int publishDate;
public Book(String name, int pageNumber, String author, int publishDate) {
this.name = name;
this.pageNumber = pageNumber;
this.author = author;
this.publishDate = publishDate;
}
public String getName() {
return name;
}
public int getPageNumber() {
return pageNumber;
}
public String getAuthor() {
return author;
}
public int getPublishDate() {
return publishDate;
}
@Override
public int compareTo(Book b) {
return (this.getName()).compareTo(b.getName());
}
}
public class Main {
public static void main(String[] args){
Book book1 = new Book("Tehlikeli Oyunlar", 572, "Oğuz Atay", 1997);
Book book2 = new Book("İçimizdeki Şeytan", 257, "Sabahattin Ali", 1999);
Book book3 = new Book("Bu Ülke", 355, "Cemil Meriç", 1991);
Book book4 = new Book("Romeo ve Juliet", 408, "William Shakespeare",1591);
Book book5 = new Book("Küçük Prens", 140, "Antoine de Saint-Exupéry",1943);
TreeSet<Book> bookNameTreeSet = new TreeSet<Book>();
bookNameTreeSet.add(book1);
bookNameTreeSet.add(book2);
bookNameTreeSet.add(book3);
bookNameTreeSet.add(book4);
bookNameTreeSet.add(book5);
for (Book book:bookNameTreeSet) {
System.out.printf("Book Name: %-25s Page: %5d\n", book.getName(), book.getPageNumber());
}
System.out.println("======================================");
TreeSet<Book> bookSetPageNum = new TreeSet<>(new Comparator<Book>() {
@Override
public int compare(Book b1, Book b2) {
return b1.getPageNumber() - b2.getPageNumber();
}
}.reversed());
bookSetPageNum.add(book1);
bookSetPageNum.add(book2);
bookSetPageNum.add(book3);
bookSetPageNum.add(book4);
bookSetPageNum.add(book5);
for (Book book:bookSetPageNum) {
System.out.printf("Book Name: %-25s Page: %5d\n", book.getName(), book.getPageNumber());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment