BAEL-305: Refactored Book and Bookshelf as interfaces and implemented them.
This commit is contained in:
parent
ffe5ce630c
commit
525c93059c
|
@ -17,7 +17,7 @@
|
|||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<version>4.0.0-b01</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.commands;
|
||||
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.Book;
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.Bookshelf;
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.BookshelfImpl;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
|
@ -9,13 +9,12 @@ import java.io.IOException;
|
|||
public class SearchCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
Book book = Bookshelf.getInstance()
|
||||
Book book = new BookshelfImpl().getInstance()
|
||||
.findByTitle(request.getParameter("title"));
|
||||
if (book != null) {
|
||||
request.setAttribute("book", book);
|
||||
forward("book-found");
|
||||
} else {
|
||||
request.setAttribute("books", Bookshelf.getInstance().getBooks());
|
||||
forward("book-notfound");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,40 +1,15 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
public class Book {
|
||||
private String author;
|
||||
private String title;
|
||||
private Double price;
|
||||
public interface Book {
|
||||
String getAuthor();
|
||||
|
||||
public Book() {
|
||||
}
|
||||
void setAuthor(String author);
|
||||
|
||||
public Book(String author, String title, Double price) {
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
this.price = price;
|
||||
}
|
||||
String getTitle();
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
void setTitle(String title);
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
Double getPrice();
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
void setPrice(Double price);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
public class BookImpl implements Book {
|
||||
private String author;
|
||||
private String title;
|
||||
private Double price;
|
||||
|
||||
public BookImpl() {
|
||||
}
|
||||
|
||||
public BookImpl(String author, String title, Double price) {
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
|
@ -1,32 +1,15 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
public interface Bookshelf {
|
||||
|
||||
public class Bookshelf {
|
||||
private static Bookshelf INSTANCE = new Bookshelf();
|
||||
private List<Book> books = new ArrayList<>();
|
||||
|
||||
public static Bookshelf getInstance() {
|
||||
if (INSTANCE.books.size() == 0) {
|
||||
INSTANCE.init();
|
||||
}
|
||||
return INSTANCE;
|
||||
default void init() {
|
||||
add(new BookImpl("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
|
||||
add(new BookImpl("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
|
||||
}
|
||||
|
||||
private void init() {
|
||||
books.add(new Book("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
|
||||
books.add(new Book("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
|
||||
}
|
||||
Bookshelf getInstance();
|
||||
|
||||
public Book findByTitle(String title) {
|
||||
return books.stream()
|
||||
.filter(book -> book.getTitle().toLowerCase().contains(title.toLowerCase()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
<E extends Book> boolean add(E book);
|
||||
|
||||
public List<Book> getBooks() {
|
||||
return books;
|
||||
}
|
||||
Book findByTitle(String title);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class BookshelfImpl extends ArrayList<Book> implements Bookshelf {
|
||||
private static Bookshelf INSTANCE;
|
||||
|
||||
@Override
|
||||
public Bookshelf getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new BookshelfImpl();
|
||||
INSTANCE.init();
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book findByTitle(String title) {
|
||||
return this.stream()
|
||||
.filter(book -> book.getTitle().toLowerCase().contains(title.toLowerCase()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue