Facebook
From Lukasz, 6 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 277
  1.  
  2. package lukasz;
  3.  
  4.  
  5. import java.util.Objects;
  6.  
  7.  
  8. public class Book implements Comparable<Book> {
  9.    
  10.     private String title;
  11.     String author;
  12.     int pages;
  13.  
  14.     public Book(String title, String author, int pages){
  15.         this.title = title;
  16.         this.author = author;
  17.         this.pages = pages;
  18.    
  19.     }
  20.  
  21.     @Override
  22.     public int hashCode() {
  23.         int hash = 7;
  24.         hash = 31 * hash + Objects.hashCode(this.title);
  25.         hash = 31 * hash + Objects.hashCode(this.author);
  26.         hash = 31 * hash + this.pages;
  27.         return hash;
  28.     }
  29.  
  30.     @Override
  31.     public boolean equals(Object obj) {
  32.         if (this == obj) {
  33.             return true;
  34.         }
  35.         if (obj == null) {
  36.             return false;
  37.         }
  38.         if (getClass() != obj.getClass()) {
  39.             return false;
  40.         }
  41.         final Book other = (Book) obj;
  42.         if (this.pages != other.pages) {
  43.             return false;
  44.         }
  45.         if (!Objects.equals(this.title, other.title)) {
  46.             return false;
  47.         }
  48.         if (!Objects.equals(this.author, other.author)) {
  49.             return false;
  50.         }
  51.         return true;
  52.     }
  53.  
  54.     @Override
  55.     public String toString() {
  56.         return "Book{" + "title=" + title + ", author=" + author + ", pages=" + pages + '}';
  57.     }
  58.     @Override
  59.     public int compareTo(Book o){
  60.         int compareTitle = title.compareTo(o.title);
  61.        
  62.         if (compareTitle == 0){
  63.             return author.compareTo(o.author);
  64.         }
  65.         else{
  66.             return compareTitle;
  67.         }
  68.     }
  69.  
  70.    
  71.    
  72. }
  73. -------------------------------------------------------------------------------------------------------
  74.  
  75.  
  76. package lukasz;
  77.  
  78.  
  79. import java.util.ArrayList;
  80. import java.util.Collections;
  81. import java.util.Iterator;
  82. import java.util.List;
  83.  
  84.  
  85. public class Start {
  86.     public static void main(String[] args) {
  87.         List<Book> books = new ArrayList<>();
  88.         books.add(new Book("Podstawy języka Java","A",100));
  89.         books.add(new Book("Java dla opornych","B",100));
  90.         books.add(new Book("Podstawy języka Java","C",150));
  91.         books.add(new Book("C# dla początkujących","B",200));
  92.         books.add(new Book("W pustyni i w puszczy","Henryk Sienkiewicz",350));
  93.        
  94.         for(Book book : books){
  95.             System.out.println(book);
  96.         }
  97.        
  98.         Collections.sort(books);
  99.        
  100.         System.out.println("\nPosortowane: ");
  101.         for (Book book : books){
  102.             System.out.println(books.toString());
  103.          
  104.         }
  105.         books.sort(new CompareBook());
  106.         System.out.println("Posortowane wg. stron: ");
  107.         System.out.println(books.toString());
  108.        
  109.         books.sort(new CompareBook2());
  110.         System.out.println("Posorotwane wg. autora malejąco: ");
  111.         System.out.println(books.toString());
  112.        
  113.         Iterator<Book> b = books.iterator();
  114.        
  115.         while (b.hasNext()){
  116.             Book bo = b.next();
  117.             if (bo.pages <= 150){
  118.                 b.remove();
  119.             }
  120.         }
  121.         System.out.println("Usunięcie z listy wartości mniejszych niż 150: ");
  122.         System.out.println(books);
  123.    
  124.     }
  125.        
  126.     }
  127.  
  128. ---------------------------------------------------------------------------------------------------
  129.  
  130. package lukasz;
  131.  
  132. import java.util.Comparator;
  133.  
  134.  
  135. public class CompareBook implements Comparator<Book> {
  136.  
  137.    
  138.  
  139.     @Override
  140.     public int compare(Book o1, Book o2) {
  141.         int a = o2.pages - o1.pages;
  142.         if (a == 0){
  143.             a = o1.author.compareTo(o2.author);
  144.         }
  145.        
  146.             return a;
  147.        
  148.     }
  149.  
  150.    
  151. }
  152. ------------------------------------------------------------------------------------
  153.  
  154. package lukasz;
  155.  
  156. import java.util.Comparator;
  157.  
  158.  
  159. public class CompareBook2 implements Comparator<Book> {
  160.  
  161.     @Override
  162.     public int compare(Book o1, Book o2) {
  163.         int compareAuthor = o1.author.compareTo(o2.author);
  164.         return compareAuthor;
  165.     }
  166.  
  167.    
  168.    
  169. }
  170.