Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactored book and movie list actions

  • Loading branch information...
commit 2a77ce7d7c23569d4b7edd632b0374606406f1a0 1 parent ee6948a
@tmattia authored
View
2  src/com/twu/biblioteca/Book.java
@@ -1,6 +1,6 @@
package com.twu.biblioteca;
-public class Book {
+public class Book implements Record {
private int number;
private String description;
View
15 src/com/twu/biblioteca/BookList.java
@@ -1,24 +1,15 @@
package com.twu.biblioteca;
-import java.util.ArrayList;
-public class BookList extends ArrayList<Book> {
+public class BookList extends RecordList<Book> {
public boolean contains(int bookNumber) {
- for (Book book : this) {
- if (book.getNumber() == bookNumber) {
+ for (Record book : this) {
+ if (((Book) book).getNumber() == bookNumber) {
return true;
}
}
return false;
}
-
- public String toString() {
- String ret = "";
- for (Book book : this) {
- ret += book.toString() + "\n";
- }
- return ret;
- }
}
View
21 src/com/twu/biblioteca/ListBooksAction.java
@@ -1,21 +0,0 @@
-package com.twu.biblioteca;
-
-public class ListBooksAction implements Action {
-
- final static public String LIST_OF_BOOKS_MSG = "List of Books in the library:";
-
- private Console console;
- private BookList bookList;
-
- public ListBooksAction(Console console, BookList bookList) {
- this.console = console;
- this.bookList = bookList;
- }
-
- @Override
- public void execute() {
- console.println(LIST_OF_BOOKS_MSG);
- console.println(bookList.toString());
- }
-
-}
View
21 src/com/twu/biblioteca/ListMoviesAction.java
@@ -1,21 +0,0 @@
-package com.twu.biblioteca;
-
-public class ListMoviesAction implements Action {
-
- final static public String LIST_OF_MOVIES_MSG = "List of Movies in the library:";
-
- private Console console;
- private MovieList movieList;
-
- public ListMoviesAction(Console console, MovieList movieList) {
- this.console = console;
- this.movieList = movieList;
- }
-
- @Override
- public void execute() {
- console.println(LIST_OF_MOVIES_MSG);
- console.println(movieList.toString());
- }
-
-}
View
18 src/com/twu/biblioteca/ListRecordsAction.java
@@ -0,0 +1,18 @@
+package com.twu.biblioteca;
+
+public class ListRecordsAction implements Action {
+
+ private Console console;
+ private RecordList<?> recordList;
+
+ public ListRecordsAction(Console console, RecordList<?> recordList) {
+ this.console = console;
+ this.recordList = recordList;
+ }
+
+ @Override
+ public void execute() {
+ console.println(recordList.toString());
+ }
+
+}
View
4 src/com/twu/biblioteca/Main.java
@@ -18,7 +18,7 @@ public static void main(String[] args) throws InvalidRatingScoreException {
bookList.add(book1);
bookList.add(book2);
- ListBooksAction listBooksAction = new ListBooksAction(console, bookList);
+ ListRecordsAction listBooksAction = new ListRecordsAction(console, bookList);
MenuOption listBooksOption = new MenuOption(1, "List books", listBooksAction);
Movie movie1 = new Movie("First Movie", "Director A", new Rating(10));
@@ -29,7 +29,7 @@ public static void main(String[] args) throws InvalidRatingScoreException {
movieList.add(movie2);
movieList.add(movie3);
- ListMoviesAction listMoviesAction = new ListMoviesAction(console, movieList);
+ ListRecordsAction listMoviesAction = new ListRecordsAction(console, movieList);
MenuOption listMoviesOption = new MenuOption(2, "List movies", listMoviesAction);
CheckLibraryNumberAction checkLibraryNumberAction = new CheckLibraryNumberAction(console, authenticator);
View
2  src/com/twu/biblioteca/Movie.java
@@ -1,6 +1,6 @@
package com.twu.biblioteca;
-public class Movie {
+public class Movie implements Record {
static final String RATING_NOT_AVAILABLE_MSG = "N/A";
View
11 src/com/twu/biblioteca/MovieList.java
@@ -1,15 +1,6 @@
package com.twu.biblioteca;
-import java.util.ArrayList;
-public class MovieList extends ArrayList<Movie> {
-
- public String toString() {
- String ret = "";
- for (Movie movie : this) {
- ret += movie.toString() + "\n";
- }
- return ret;
- }
+public class MovieList extends RecordList<Movie> {
}
View
5 src/com/twu/biblioteca/Record.java
@@ -0,0 +1,5 @@
+package com.twu.biblioteca;
+
+public interface Record {
+
+}
View
15 src/com/twu/biblioteca/RecordList.java
@@ -0,0 +1,15 @@
+package com.twu.biblioteca;
+
+import java.util.ArrayList;
+
+public abstract class RecordList<T> extends ArrayList<Record> {
+
+ public String toString() {
+ StringBuffer buffer = new StringBuffer();
+ for (Record record : this) {
+ buffer.append(record.toString() + "\n");
+ }
+ return buffer.toString();
+ }
+
+}
View
28 test/com/twu/biblioteca/ListBooksActionTest.java
@@ -1,28 +0,0 @@
-package com.twu.biblioteca;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-import static org.mockito.Mockito.inOrder;
-
-import org.junit.Test;
-import org.mockito.InOrder;
-
-public class ListBooksActionTest {
-
- @Test
- public void shouldListBooks() {
- final String FAKE_BOOK_LIST = "Fake book list";
-
- Console mockedConsole = mock(Console.class);
- BookList mockedBookList = mock(BookList.class);
- when(mockedBookList.toString()).thenReturn(FAKE_BOOK_LIST);
-
- ListBooksAction listBooksAction = new ListBooksAction(mockedConsole, mockedBookList);
- listBooksAction.execute();
-
- InOrder inOrder = inOrder(mockedConsole);
- inOrder.verify(mockedConsole).println(ListBooksAction.LIST_OF_BOOKS_MSG);
- inOrder.verify(mockedConsole).println(FAKE_BOOK_LIST);
- }
-
-}
View
29 test/com/twu/biblioteca/ListMoviesActionTest.java
@@ -1,29 +0,0 @@
-package com.twu.biblioteca;
-
-import static org.mockito.Mockito.inOrder;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import org.junit.Test;
-import org.mockito.InOrder;
-
-
-public class ListMoviesActionTest {
-
- @Test
- public void shouldListMovies() {
- final String FAKE_LIST_OF_MOVIES = "Fake list of movies";
-
- Console mockedConsole = mock(Console.class);
- MovieList mockedMovieList = mock(MovieList.class);
- when(mockedMovieList.toString()).thenReturn(FAKE_LIST_OF_MOVIES);
-
- ListMoviesAction listMoviesAction = new ListMoviesAction(mockedConsole, mockedMovieList);
- listMoviesAction.execute();
-
- InOrder inOrder = inOrder(mockedConsole);
- inOrder.verify(mockedConsole).println(ListMoviesAction.LIST_OF_MOVIES_MSG);
- inOrder.verify(mockedConsole).println(FAKE_LIST_OF_MOVIES);
- }
-
-}
View
25 test/com/twu/biblioteca/ListRecordsActionTest.java
@@ -0,0 +1,25 @@
+package com.twu.biblioteca;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.junit.Test;
+
+public class ListRecordsActionTest {
+
+ @Test
+ public void shouldListBooks() {
+ final String FAKE_RECORD_LIST = "Fake record list";
+
+ Console mockedConsole = mock(Console.class);
+ RecordList<?> mockedRecordList = mock(RecordList.class);
+ when(mockedRecordList.toString()).thenReturn(FAKE_RECORD_LIST);
+
+ ListRecordsAction listBooksAction = new ListRecordsAction(mockedConsole, mockedRecordList);
+ listBooksAction.execute();
+
+ verify(mockedConsole).println(FAKE_RECORD_LIST);
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.