Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package library;
- public class BookBuilder {
- private int bookNumber;
- private String title;
- private String author;
- private String rating;
- private String numberOfRaters;
- public BookBuilder addBookNumber(int bookNumber) {
- this.bookNumber = bookNumber;
- return this;
- }
- public BookBuilder addTitle(String title) {
- this.title = title;
- return this;
- }
- public BookBuilder addAuthor(String author) {
- this.author = author;
- return this;
- }
- public BookBuilder addRating(String rating) {
- this.rating = rating;
- return this;
- }
- public BookBuilder addRating(int rating) {
- this.rating = "" + rating;
- return this;
- }
- public BookBuilder addNumberOfRaters(String numberOfRaters) {
- this.numberOfRaters = numberOfRaters;
- return this;
- }
- public BookBuilder addNumberOfRaters(int numberOfRaters) {
- this.numberOfRaters = "" + numberOfRaters;
- return this;
- }
- public Book build() {
- if (bookNumber == 0) {
- throw new RuntimeException("Book number can't be 0");
- }
- if (title == null) {
- throw new RuntimeException("Book title can't be null");
- }
- if (author == null) {
- throw new RuntimeException("Book author can't be null");
- }
- if (rating == null) {
- throw new RuntimeException("Book rating can't be null");
- }
- if (numberOfRaters == null) {
- throw new RuntimeException("Book numberOfRaters can't be null");
- }
- return new Book(author, title, bookNumber, rating, numberOfRaters);
- }
- public BookBuilder addBookNumber(String nextLine) {
- this.bookNumber = Integer.parseInt(nextLine);
- return this;
- }
- public BookBuilder parseRatingString(String nextLine) {
- String[] ratingData = nextLine.split(" avg rating — ");
- addRating(ratingData[0]);
- addNumberOfRaters(trimLastCharacters(ratingData[1], 8));
- return this;
- }
- private static String trimLastCharacters(String stringToBeTrimmed, int numberOfTrimmedCharacters) {
- return stringToBeTrimmed.substring(0, stringToBeTrimmed.length() - numberOfTrimmedCharacters);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement