Skip to content

Commit

Permalink
Add return feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mapu77 committed Jul 12, 2018
1 parent 54e63d0 commit 858a75e
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 25 deletions.
11 changes: 7 additions & 4 deletions src/main/java/com/twu/biblioteca/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ public static void main(String[] args) {
bookPresenter.listBooks();
break;
case 2:
System.out.println("What book do you want to checkout?");
optionPresenter.askForCheckOut();
String bookTitle = user.readBookTitle();
bookPresenter.checkOut(bookTitle);
break;
case 3:
optionPresenter.askForReturn();
bookTitle = user.readBookTitle();
bookPresenter.returnBook(bookTitle);
break;
default:
System.out.println("Select a valid option!");
System.out.println();
optionPresenter.showMenu();
optionPresenter.sayInvalidOption();
}
optionPresenter.showMenu();
}
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/com/twu/biblioteca/OptionPresenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,41 @@

import java.io.PrintStream;

public class OptionPresenter {
class OptionPresenter {
private static final String WELCOME_MESSAGE = "Welcome to the Bangalore Public Library system";
private static final String FANCY_LINE = "----------------------------------------------";
private static final String END_OF_LINE = "\n";

private final PrintStream output;

public OptionPresenter(PrintStream output) {
OptionPresenter(PrintStream output) {
this.output = output;
}

public void sayWelcome() {
void sayWelcome() {
output.println(FANCY_LINE);
output.println(WELCOME_MESSAGE);
output.println(FANCY_LINE);
output.println(END_OF_LINE);
}

public void showMenu() {
void showMenu() {
output.println("Choose an option:");
output.println("\t1. List books");
output.println("\t2. Checkout book");
output.println("\t2. Checkout a book");
output.println("\t3. Return a book");
output.println("\t0. Exit");
}

void askForCheckOut() {
output.println("What book do you want to checkout?");
}

void askForReturn() {
output.println("What book do you want to return?");
}

void sayInvalidOption() {
output.println("Select a valid option!");
System.out.println();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public interface BookShelves {
Collection<BookInfo> listBooks();

void checkOut(String bookTitle) throws BookNotFound;

void returnBook(String bookTitle) throws BookNotFound;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public void checkOut(String bookTitle) {
else throw new BookNotFound();
}

@Override
public void returnBook(String bookTitle) {
if (this.books.containsKey(bookTitle))
this.books.get(bookTitle).returnCopy();
else throw new BookNotFound();
}

public void preloadBooks() {
BookInfo book1 = new BookInfoBuilder().withTitle("Harry Potter and the Philosopher's Stone").fromAuthor("J.K Rowling").publishedInYear(1997).build();
BookInfo book2 = new BookInfoBuilder().withTitle("Game of Thrones - A Game of Thrones").fromAuthor("George R. Martin").publishedInYear(1994).build();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/twu/biblioteca/books/core/BookInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public void checkOut() {
if (this.isAvailable()) checkedOut = true;
else throw new BookNotAvailable();
}

public void returnCopy() {
if (this.isAvailable()) throw new NotAbleToReturnBook();
else checkedOut = false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.twu.biblioteca.books.core;

public class NotAbleToReturnBook extends RuntimeException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ public interface AbstractBookPresenter {
void listBooks();

void checkOut(String bookTitle);

void returnBook(String bookTitle);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.twu.biblioteca.books.application.BookShelves;
import com.twu.biblioteca.books.core.BookInfo;
import com.twu.biblioteca.books.core.BookNotAvailable;
import com.twu.biblioteca.books.core.NotAbleToReturnBook;

import java.io.PrintStream;
import java.util.Comparator;
Expand All @@ -15,6 +16,9 @@ public class BookPresenter implements AbstractBookPresenter {
private static final String FANCY_LINE = "----------------------------------------";
private static final String HEADERS = "Title" + SPACE_BETWEEN_COLUMNS + "Author" + SPACE_BETWEEN_COLUMNS + "Publication year\n" + FANCY_LINE;
private static final String THANK_YOU_FOR_CHECKING_OUT = "Thank you! Enjoy the book";
private static final String BOOK_NOT_AVAILABLE = "That book is not available";
private static final String THANK_YOU_FOR_RETURNING = "Thank you for returning the book";
private static final String NOT_VALID_BOOK = "That is not a valid book to return";

private final BookShelves bookShelves;
private final PrintStream output;
Expand Down Expand Up @@ -43,7 +47,17 @@ public void checkOut(String bookTitle) {
bookShelves.checkOut(bookTitle);
output.println(THANK_YOU_FOR_CHECKING_OUT);
} catch (BookNotFound | BookNotAvailable e) {
output.println("That book is not available");
output.println(BOOK_NOT_AVAILABLE);
}
}

@Override
public void returnBook(String bookTitle) {
try {
bookShelves.returnBook(bookTitle);
output.println(THANK_YOU_FOR_RETURNING);
} catch (BookNotFound | NotAbleToReturnBook e) {
output.println(NOT_VALID_BOOK);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.twu.biblioteca.books;
package com.twu.biblioteca;

import com.twu.biblioteca.OptionPresenter;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
Expand Down Expand Up @@ -32,19 +31,14 @@ public void showTwoFancyLinesWhenWelcoming() {
verify(outMock, times(2)).println("----------------------------------------------");
}

@Test
public void leaveALineAfterTheWelcome() {
optionPresenter.sayWelcome();
verify(outMock).println("\n");
}

@Test
public void showMenuOptions() {
optionPresenter.showMenu();
InOrder inOrder = inOrder(outMock);
inOrder.verify(outMock).println("Choose an option:");
inOrder.verify(outMock).println("\t1. List books");
inOrder.verify(outMock).println("\t2. Checkout book");
inOrder.verify(outMock).println("\t2. Checkout a book");
inOrder.verify(outMock).println("\t3. Return a book");
inOrder.verify(outMock).println("\t0. Exit");
verifyNoMoreInteractions(outMock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ public void haveSomeBooksWhenPreloaded() {
}

@Test(expected = BookNotFound.class)
public void shouldThrowBookNotFoundWhenTryingToCheckOutABookThatIsNotAvailable() {
public void throwBookNotFoundWhenTryingToCheckOutABookThatIsNotAvailable() {
BookShelvesInteractor bookShelves = new BookShelvesInteractor();
bookShelves.preloadBooks();
bookShelves.checkOut("not existing book");
}

@Test(expected = BookNotFound.class)
public void throwBookNotFoundWhenTryingToReturnABookThatIsNotAvailable() {
BookShelvesInteractor bookShelves = new BookShelvesInteractor();
bookShelves.returnBook("not existing book");
}
}
14 changes: 14 additions & 0 deletions src/test/java/com/twu/biblioteca/books/core/BookInfoShould.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ public void throwBookNotAvailableWhenTryingToCheckOutBeingNotAvailable() {
book.checkOut();
book.checkOut();
}

@Test(expected = NotAbleToReturnBook.class)
public void throwNotAbleToReturnWhenTryingToReturnBeingAvailable() {
BookInfo book = new BookInfo("some title");
book.returnCopy();
}

@Test
public void beAvailableAfterReturned() {
BookInfo book = new BookInfo("some title");
book.checkOut();
book.returnCopy();
assertThat(book.isAvailable(), is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.twu.biblioteca.books.application.BookShelvesInteractor;
import com.twu.biblioteca.books.core.BookInfoBuilder;
import com.twu.biblioteca.books.core.BookNotAvailable;
import com.twu.biblioteca.books.core.NotAbleToReturnBook;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
Expand Down Expand Up @@ -37,7 +38,7 @@ public void sayThereAreNoBooksInTheShelves() {

@Test
public void showTheHeadersBeforeAnyBookInfo() {
when(bookShelvesMock.listBooks()).thenReturn(Arrays.asList(
when(bookShelvesMock.listBooks()).thenReturn(Collections.singletonList(
new BookInfoBuilder()
.withTitle("Harry Potter and the Philosopher's Stone")
.fromAuthor("J.K. Rowling")
Expand Down Expand Up @@ -89,7 +90,7 @@ public void showBooksAlphabeticallySorted() {

@Test
public void notShowCheckOutBooks() {
when(bookShelvesMock.listBooks()).thenReturn(Arrays.asList(
when(bookShelvesMock.listBooks()).thenReturn(Collections.singletonList(
new BookInfoBuilder()
.withTitle("Harry Potter and the Philosopher's Stone")
.fromAuthor("J.K. Rowling")
Expand All @@ -103,7 +104,7 @@ public void notShowCheckOutBooks() {

@Test
public void sayThankYouWhenCheckingOutABook() {
when(bookShelvesMock.listBooks()).thenReturn(Arrays.asList(
when(bookShelvesMock.listBooks()).thenReturn(Collections.singletonList(
new BookInfoBuilder()
.withTitle("Harry Potter and the Philosopher's Stone")
.fromAuthor("J.K. Rowling")
Expand All @@ -126,4 +127,31 @@ public void sayTheBookIsNotAvailableWhenTheBookIsNotInTheShelves() {
presenter.checkOut("Harry Potter and the Philosopher's Stone");
verify(outMock).println("That book is not available");
}

@Test
public void sayThanksForReturningABook() {
when(bookShelvesMock.listBooks()).thenReturn(Collections.singletonList(
new BookInfoBuilder()
.withTitle("Harry Potter and the Philosopher's Stone")
.fromAuthor("J.K. Rowling")
.publishedInYear(1997)
.checkedOut()
.build()));
presenter.returnBook("Harry Potter and the Philosopher's Stone");
verify(outMock).println("Thank you for returning the book");
}

@Test
public void sayTheBookIsNotValidToReturnWhenBookIsNotFound() {
doThrow(BookNotFound.class).when(bookShelvesMock).returnBook(anyString());
presenter.returnBook("Harry Potter and the Philosopher's Stone");
verify(outMock).println("That is not a valid book to return");
}

@Test
public void sayTheBookIsNotValidToReturnWhenBookIsYetAvailable() {
doThrow(NotAbleToReturnBook.class).when(bookShelvesMock).returnBook(anyString());
presenter.returnBook("Harry Potter and the Philosopher's Stone");
verify(outMock).println("That is not a valid book to return");
}
}

0 comments on commit 858a75e

Please sign in to comment.