Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. package library;
  2.  
  3. public class BookBuilder {
  4.  
  5. private int bookNumber;
  6. private String title;
  7. private String author;
  8. private String rating;
  9. private String numberOfRaters;
  10.  
  11. public BookBuilder addBookNumber(int bookNumber) {
  12. this.bookNumber = bookNumber;
  13. return this;
  14. }
  15.  
  16. public BookBuilder addTitle(String title) {
  17. this.title = title;
  18. return this;
  19. }
  20.  
  21. public BookBuilder addAuthor(String author) {
  22. this.author = author;
  23. return this;
  24. }
  25.  
  26. public BookBuilder addRating(String rating) {
  27. this.rating = rating;
  28. return this;
  29. }
  30.  
  31. public BookBuilder addRating(int rating) {
  32. this.rating = "" + rating;
  33. return this;
  34. }
  35.  
  36. public BookBuilder addNumberOfRaters(String numberOfRaters) {
  37. this.numberOfRaters = numberOfRaters;
  38. return this;
  39. }
  40.  
  41. public BookBuilder addNumberOfRaters(int numberOfRaters) {
  42. this.numberOfRaters = "" + numberOfRaters;
  43. return this;
  44. }
  45.  
  46. public Book build() {
  47. if (bookNumber == 0) {
  48. throw new RuntimeException("Book number can't be 0");
  49. }
  50.  
  51. if (title == null) {
  52. throw new RuntimeException("Book title can't be null");
  53. }
  54.  
  55. if (author == null) {
  56. throw new RuntimeException("Book author can't be null");
  57. }
  58.  
  59. if (rating == null) {
  60. throw new RuntimeException("Book rating can't be null");
  61. }
  62.  
  63. if (numberOfRaters == null) {
  64. throw new RuntimeException("Book numberOfRaters can't be null");
  65. }
  66.  
  67. return new Book(author, title, bookNumber, rating, numberOfRaters);
  68. }
  69.  
  70. public BookBuilder addBookNumber(String nextLine) {
  71. this.bookNumber = Integer.parseInt(nextLine);
  72. return this;
  73. }
  74.  
  75. public BookBuilder parseRatingString(String nextLine) {
  76. String[] ratingData = nextLine.split(" avg rating — ");
  77.  
  78. addRating(ratingData[0]);
  79. addNumberOfRaters(trimLastCharacters(ratingData[1], 8));
  80.  
  81. return this;
  82. }
  83.  
  84. private static String trimLastCharacters(String stringToBeTrimmed, int numberOfTrimmedCharacters) {
  85. return stringToBeTrimmed.substring(0, stringToBeTrimmed.length() - numberOfTrimmedCharacters);
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement