Skip to content

Commit

Permalink
Refactor in naming and responsibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
mapu77 committed Jul 13, 2018
1 parent 858a75e commit f4ad46c
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 33 deletions.
25 changes: 14 additions & 11 deletions src/main/java/com/twu/biblioteca/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,42 @@

import com.twu.biblioteca.books.application.BookShelvesInteractor;
import com.twu.biblioteca.books.infrastructure.AbstractBookPresenter;
import com.twu.biblioteca.books.infrastructure.BookInputController;
import com.twu.biblioteca.books.infrastructure.BookPresenter;

public class App {

public static void main(String[] args) {
OptionPresenter optionPresenter = new OptionPresenter(System.out);
optionPresenter.sayWelcome();
optionPresenter.showMenu();
MenuInputController user = new MenuInputController(System.in);
AppPresenter appPresenter = new AppPresenter(System.out);
appPresenter.sayWelcome();
appPresenter.showMenu();
AppInputController user = new AppInputController(System.in);

BookShelvesInteractor bookShelves = new BookShelvesInteractor();
bookShelves.preloadBooks();
BookInputController bookInputController = new BookInputController(System.in);
AbstractBookPresenter bookPresenter = new BookPresenter(bookShelves, System.out);
while (!user.wantsToExit()) {
switch (user.getSelectedOption()) {
case 1:
bookPresenter.listBooks();
break;
case 2:
optionPresenter.askForCheckOut();
String bookTitle = user.readBookTitle();
appPresenter.askForBookCheckOut();
String bookTitle = bookInputController.readBookTitle();
bookPresenter.checkOut(bookTitle);
break;
case 3:
optionPresenter.askForReturn();
bookTitle = user.readBookTitle();
appPresenter.askForBookReturn();
bookTitle = bookInputController.readBookTitle();
bookPresenter.returnBook(bookTitle);
break;
default:
optionPresenter.sayInvalidOption();
appPresenter.sayInvalidOption();
}
optionPresenter.showMenu();
appPresenter.showMenu();
}
System.out.println("Thanks for using our system");
appPresenter.sayFarewell();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import java.io.InputStream;
import java.util.Scanner;

class MenuInputController {
class AppInputController {
private final Scanner scanner;
private Integer selectedOption;

MenuInputController(InputStream inputScanner) {
AppInputController(InputStream inputScanner) {
this.scanner = new Scanner(inputScanner);
}

Expand All @@ -23,7 +23,4 @@ Integer getSelectedOption() {
return this.selectedOption;
}

String readBookTitle() {
return scanner.nextLine();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import java.io.PrintStream;

class OptionPresenter {
class AppPresenter {
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;

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

void sayFarewell() {
output.println("Thanks for using our system");
}

void sayWelcome() {
output.println(FANCY_LINE);
output.println(WELCOME_MESSAGE);
Expand All @@ -27,16 +30,16 @@ void showMenu() {
output.println("\t0. Exit");
}

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

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

void sayInvalidOption() {
output.println("Select a valid option!");
System.out.println();
output.println();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.twu.biblioteca.books.infrastructure;

import java.io.InputStream;
import java.util.Scanner;

public class BookInputController {
private final Scanner scanner;

public BookInputController(InputStream inputStream) {
this.scanner = new Scanner(inputStream);
}

public String readBookTitle() {
return scanner.nextLine();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class MenuInputControllerShould {
public class AppInputControllerShould {

private MenuInputController inputController;
private AppInputController inputController;

@Before
public void setUp() {
Expand All @@ -19,21 +19,21 @@ public void setUp() {
@Test
public void returnTrueWhenUserWantsToExit() {
System.setIn(new ByteArrayInputStream("0".getBytes()));
inputController = new MenuInputController(System.in);
inputController = new AppInputController(System.in);
assertThat(inputController.wantsToExit(), is(true));
}

@Test
public void returnFalseWhenUserDoesNotWantToExit() {
System.setIn(new ByteArrayInputStream("1".getBytes()));
inputController = new MenuInputController(System.in);
inputController = new AppInputController(System.in);
assertThat(inputController.wantsToExit(), is(false));
}

@Test
public void keepSelectedOptionWhenAskingIfUserWantsToExit() {
System.setIn(new ByteArrayInputStream("34".getBytes()));
inputController = new MenuInputController(System.in);
inputController = new AppInputController(System.in);
inputController.wantsToExit();
assertThat(inputController.getSelectedOption(), is(not(nullValue())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@

import static org.mockito.Mockito.*;

public class OptionPresenterShould {
public class AppPresenterShould {

private PrintStream outMock;
private OptionPresenter optionPresenter;
private AppPresenter appPresenter;

@Before
public void setUp() {
outMock = mock(PrintStream.class);
optionPresenter = new OptionPresenter(outMock);
appPresenter = new AppPresenter(outMock);
}

@Test
public void showAWelcomeMessage() {
optionPresenter.sayWelcome();
appPresenter.sayWelcome();
verify(outMock).println("Welcome to the Bangalore Public Library system");
}

@Test
public void showTwoFancyLinesWhenWelcoming() {
optionPresenter.sayWelcome();
appPresenter.sayWelcome();
verify(outMock, times(2)).println("----------------------------------------------");
}

@Test
public void showMenuOptions() {
optionPresenter.showMenu();
appPresenter.showMenu();
InOrder inOrder = inOrder(outMock);
inOrder.verify(outMock).println("Choose an option:");
inOrder.verify(outMock).println("\t1. List books");
Expand All @@ -42,4 +42,16 @@ public void showMenuOptions() {
inOrder.verify(outMock).println("\t0. Exit");
verifyNoMoreInteractions(outMock);
}

@Test
public void askForBookCheckOut() {
appPresenter.askForBookCheckOut();
verify(outMock).println("What book do you want to checkout?");
}

@Test
public void askForBookReturn() {
appPresenter.askForBookReturn();
verify(outMock).println("What book do you want to return?");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.twu.biblioteca.books.infrastructure;

import org.junit.Test;

import java.io.ByteArrayInputStream;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class BookInputControllerShould {
@Test
public void readUserBookTitleInput() {
System.setIn(new ByteArrayInputStream("some input".getBytes()));
BookInputController bookInputController = new BookInputController(System.in);
assertThat(bookInputController.readBookTitle(), is("some input"));
}
}

0 comments on commit f4ad46c

Please sign in to comment.