-
Notifications
You must be signed in to change notification settings - Fork 54.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-305: Refactored Book and Bookshelf as interfaces and implemented…
… them.
- Loading branch information
1 parent
ffe5ce6
commit 525c930
Showing
6 changed files
with
87 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
...c/main/java/com/baeldung/enterprise/patterns/front/controller/commands/SearchCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 7 additions & 32 deletions
39
...er-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
46 changes: 46 additions & 0 deletions
46
...attern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/BookImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
31 changes: 7 additions & 24 deletions
31
...ttern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Bookshelf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
24 changes: 24 additions & 0 deletions
24
...n/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/BookshelfImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |