Guest User

Untitled

a guest
May 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. package Book;
  2. import static java.util.Collections.*;
  3.  
  4. import java.util.Arrays;
  5. import java.util.Comparator;
  6. import java.util.function.Predicate;
  7.  
  8. public class HW5 {
  9.  
  10. public static void main(String[] args) {
  11. String[] authorBook1 = { "Stephen Hawking" };
  12.  
  13. Book book1 = new Book(1, "The Grand Design", authorBook1, "Transworld Digital", 2015, 256, 406, "limp");
  14. String[] authorBook2 = { "Robert Winston" };
  15. Book book2 = new Book(2, "SuperHuman Encyclopedia", authorBook2, "Dorling Kindersley", 2014, 208, 432, "hard");
  16.  
  17. Book[] arr = { book1, book2 };
  18. Book[] sortedBooksByAuthors =
  19.  
  20. Book.sortBooks(arr, (book1, book2) -> {
  21.  
  22. return book1.getAuthors().compareToIgnoreCase(book2.getAuthors());
  23.  
  24. });
  25.  
  26.  
  27.  
  28. }
  29.  
  30. public static Book[] sortBooks(Book[] arr, Comparator<Book> comparator) {
  31.  
  32. Book[] resultArr = Arrays.copyOf(arr, arr.length);
  33.  
  34. Arrays.sort(resultArr, comparator);
  35.  
  36. return resultArr;
  37.  
  38.  
  39.  
  40. }
  41.  
  42. public static Book[] filterArr(Book[] arr, Predicate<Book> predicate) {
  43.  
  44. return Arrays.stream(arr)
  45.  
  46. .filter(predicate)
  47.  
  48. .toArray(Book[]::new);
  49.  
  50. }
  51.  
  52.  
  53. }
  54.  
  55. class Book {
  56. private int id;
  57. private String name;
  58. private String[] authors;
  59. private String publisher;
  60. private int publisherYear;
  61. private int numOfPages;
  62. private int price;
  63. private String typeOfBinding;
  64.  
  65.  
  66. public Book(int id, String name, String[] authors, String publisher, int publisherYear, int numOfPages, int price,
  67. String typeOfBinding) {
  68. this.id = id;
  69. this.name = name;
  70. this.authors = authors;
  71. this.publisher = publisher;
  72. this.publisherYear = publisherYear;
  73. this.numOfPages = numOfPages;
  74. this.price = price;
  75. this.typeOfBinding = typeOfBinding;
  76.  
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. public int getId() {
  86. return id;
  87. }
  88.  
  89. public String getName() {
  90. return name;
  91. }
  92.  
  93. public String[] getAuthors() {
  94. return authors;
  95.  
  96. }
  97.  
  98. public String getPublisher() {
  99. return publisher;
  100. }
  101.  
  102. public int getPublisherYear() {
  103. return publisherYear;
  104. }
  105.  
  106. public int getNumOfPages() {
  107. return numOfPages;
  108. }
  109.  
  110. public int getPrice() {
  111. return price;
  112. }
  113.  
  114. public String getTypeOfBinding() {
  115. return typeOfBinding;
  116. }
  117.  
  118. }
Add Comment
Please, Sign In to add comment