-
Notifications
You must be signed in to change notification settings - Fork 25
feat: created 'Bookshelf' class #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ffed015
feat: created 'Bookshelf' class for Chelsea
Cogbonnia 9fe1711
chore: './gradlew :objects_app:spotlessApply' script
Cogbonnia 7bcc1bf
Merge branch 'code-differently:main' into lesson_16
Cogbonnia 5faea8c
Merge branch 'code-differently:main' into lesson_16
Cogbonnia e84b7fc
Merge branch 'code-differently:main' into lesson_16
Cogbonnia b3f08c1
chore: created new folder 'Bookshelf' for class
Cogbonnia 15805f0
Merge branch 'code-differently:main' into lesson_16
Cogbonnia beb19f4
Merge branch 'code-differently:main' into lesson_16
Cogbonnia f02b552
chore: fixed capitalization
Cogbonnia eb48303
Merge branch 'code-differently:main' into lesson_16
Cogbonnia 0bf4a48
Merge branch 'code-differently:main' into lesson_16
Cogbonnia 5d565ce
Merge branch 'code-differently:main' into lesson_16
Cogbonnia 5202fed
Merge branch 'code-differently:main' into lesson_16
Cogbonnia 58c1c5d
Merge branch 'code-differently:main' into lesson_16
Cogbonnia 1a5dc2e
Merge branch 'code-differently:main' into lesson_16
Cogbonnia 702a5b8
Merge branch 'code-differently:main' into lesson_16
Cogbonnia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...bjects_app/src/main/java/com/codedifferently/lesson16/chelseaogbonnia/Bookshelf/Book.java
This file contains hidden or 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,53 @@ | ||
package com.codedifferently.lesson16.chelseaogbonnia.bookshelf; | ||
|
||
enum Genre { | ||
FICTION, | ||
NON_FICTION, | ||
MYSTERY, | ||
SCIENCE_FICTION, | ||
FANTASY, | ||
HORROR, | ||
ROMANCE, | ||
ADVENTURE, | ||
HISTORY, | ||
CRIME | ||
} | ||
|
||
enum BookType { | ||
HARDCOVER, | ||
PAPERBACK, | ||
MAGAZINE, | ||
NOVEL, | ||
POEM, | ||
COMICS | ||
} | ||
|
||
public class Book { | ||
private String title; | ||
private String author; | ||
private Genre genre; | ||
private BookType bookType; | ||
|
||
public Book(String title, String author, Genre genre, BookType bookType) { | ||
this.title = title; | ||
this.author = author; | ||
this.genre = genre; | ||
this.bookType = bookType; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public String getAuthor() { | ||
return author; | ||
} | ||
|
||
public Genre getGenre() { | ||
return genre; | ||
} | ||
|
||
public BookType getBookType() { | ||
return bookType; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...s_app/src/main/java/com/codedifferently/lesson16/chelseaogbonnia/Bookshelf/Bookshelf.java
This file contains hidden or 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,80 @@ | ||
package com.codedifferently.lesson16.chelseaogbonnia.bookshelf; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
class BookNotFoundException extends Exception { | ||
public BookNotFoundException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
||
public class Bookshelf { | ||
private String owner; | ||
private int capacity; | ||
private List<Book> books; | ||
private HashMap<String, Book> bookMap; | ||
private boolean isPublic; | ||
|
||
public Bookshelf(String owner, int capacity, boolean isPublic) { | ||
this.owner = owner; | ||
this.capacity = capacity; | ||
this.isPublic = isPublic; | ||
this.books = new ArrayList<>(); | ||
this.bookMap = new HashMap<>(); | ||
} | ||
|
||
public List<Book> listBooks() { | ||
return Collections.unmodifiableList(books); | ||
} | ||
|
||
public void addBook(Book book) throws Exception { | ||
if (books.size() >= capacity) { | ||
throw new Exception("Bookshelf is full. Cannot add more books."); | ||
} | ||
books.add(book); | ||
bookMap.put(book.getTitle(), book); | ||
} | ||
|
||
public Book findBook(String title) throws BookNotFoundException { | ||
if (bookMap.containsKey(title)) { | ||
return bookMap.get(title); | ||
} | ||
throw new BookNotFoundException("Book titled \"" + title + "\" not found."); | ||
} | ||
|
||
public boolean hasBook(String title) { | ||
return bookMap.containsKey(title); | ||
} | ||
|
||
public int countBooksByGenre(Genre genre) { | ||
int count = 0; | ||
for (Book book : books) { | ||
if (book.getGenre() == genre) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
public String getOwner() { | ||
return owner; | ||
} | ||
|
||
public boolean isPublic() { | ||
return isPublic; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Bookshelf owned by " | ||
+ owner | ||
+ " with capacity " | ||
+ capacity | ||
+ " (Public: " | ||
+ isPublic | ||
+ ")"; | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
...p/src/test/java/com/codedifferently/lesson16/chelseaogbonnia/Bookshelf/BookshelfTest.java
This file contains hidden or 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,130 @@ | ||
package com.codedifferently.lesson16.chelseaogbonnia.bookshelf; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class BookshelfTest { | ||
private Bookshelf bookshelf; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
bookshelf = new Bookshelf("Chelsea", 10, true); | ||
} | ||
|
||
@Test | ||
public void testAddBook() throws Exception { | ||
// Arrange | ||
Book book = new Book("Frankenstein", "Mary Shelly", Genre.HORROR, BookType.NOVEL); | ||
|
||
// Act | ||
bookshelf.addBook(book); | ||
|
||
// Assert | ||
assertTrue(bookshelf.hasBook("Frankenstein")); | ||
} | ||
|
||
@Test | ||
public void testFindBookExists() throws Exception { | ||
// Arrange | ||
Book book = | ||
new Book("Brave New World", "Aldous Huxley", Genre.SCIENCE_FICTION, BookType.PAPERBACK); | ||
bookshelf.addBook(book); | ||
|
||
// Act | ||
Book foundBook = bookshelf.findBook("Brave New World"); | ||
|
||
// Assert | ||
assertEquals(book, foundBook); | ||
} | ||
|
||
@Test | ||
public void testFindBookNotExists() { | ||
// Arrange | ||
String nonExistentBookTitle = "Non-Existent Book"; | ||
|
||
// Act & Assert | ||
Exception exception = | ||
assertThrows( | ||
BookNotFoundException.class, | ||
() -> { | ||
bookshelf.findBook(nonExistentBookTitle); | ||
}); | ||
assertEquals("Book titled \"" + nonExistentBookTitle + "\" not found.", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testCountBooksByGenre() throws Exception { | ||
// Arrange | ||
bookshelf.addBook(new Book("The Hobbit", "J.R.R. Tolkien", Genre.FANTASY, BookType.HARDCOVER)); | ||
bookshelf.addBook(new Book("Dune", "Frank Herbert", Genre.SCIENCE_FICTION, BookType.HARDCOVER)); | ||
|
||
// Act & Assert | ||
assertEquals(1, bookshelf.countBooksByGenre(Genre.SCIENCE_FICTION)); | ||
assertEquals(1, bookshelf.countBooksByGenre(Genre.FANTASY)); | ||
} | ||
|
||
@Test | ||
public void testAddBookToFullShelf() throws Exception { | ||
// Arrange | ||
for (int i = 0; i < 10; i++) { | ||
bookshelf.addBook(new Book("Book " + i, "Author " + i, Genre.FICTION, BookType.PAPERBACK)); | ||
} | ||
Book extraBook = new Book("Extra Book", "Author Extra", Genre.FICTION, BookType.HARDCOVER); | ||
|
||
// Act & Assert | ||
Exception exception = | ||
assertThrows( | ||
Exception.class, | ||
() -> { | ||
bookshelf.addBook(extraBook); | ||
}); | ||
assertEquals("Bookshelf is full. Cannot add more books.", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testCountBooksByGenreNoBooks() { | ||
// Arrange | ||
Genre genreToCheck = Genre.ADVENTURE; | ||
|
||
// Act & Assert | ||
assertEquals(0, bookshelf.countBooksByGenre(genreToCheck)); | ||
} | ||
|
||
@Test | ||
public void testListBooks() throws Exception { | ||
// Arrange | ||
bookshelf.addBook( | ||
new Book("The Adventures of Tintin", "Hergé", Genre.ADVENTURE, BookType.COMICS)); | ||
bookshelf.addBook(new Book("The Big Sleep", "Raymond Chandler", Genre.CRIME, BookType.NOVEL)); | ||
bookshelf.addBook( | ||
new Book("Don Quixote", "Miguel de Cervantes", Genre.ADVENTURE, BookType.NOVEL)); | ||
bookshelf.addBook(new Book("Sonnet 18", "William Shakespeare", Genre.ROMANCE, BookType.POEM)); | ||
|
||
// Act | ||
int bookCount = bookshelf.listBooks().size(); | ||
|
||
// Assert | ||
assertEquals(4, bookCount); | ||
} | ||
|
||
@Test | ||
public void checksMagazineBooks() throws Exception { | ||
// Arrange | ||
Book magazine = | ||
new Book("National Geographic", "Various", Genre.NON_FICTION, BookType.MAGAZINE); | ||
bookshelf.addBook(magazine); | ||
|
||
// Act | ||
boolean hasMagazine = bookshelf.hasBook("National Geographic"); | ||
int magazineCount = bookshelf.countBooksByGenre(Genre.NON_FICTION); | ||
|
||
// Assert | ||
assertTrue(hasMagazine, "Bookshelf should contain the magazine 'National Geographic'."); | ||
assertEquals( | ||
1, magazineCount, "There should be 1 magazine in the bookshelf under NON_FICTION."); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.