Skip to content

Commit a2a9ebd

Browse files
authored
feat: created 'Bookshelf' class (#516)
* feat: created 'Bookshelf' class for Chelsea * chore: './gradlew :objects_app:spotlessApply' script * chore: created new folder 'Bookshelf' for class * chore: fixed capitalization
1 parent dae0b70 commit a2a9ebd

File tree

3 files changed

+263
-0
lines changed

3 files changed

+263
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.codedifferently.lesson16.chelseaogbonnia.bookshelf;
2+
3+
enum Genre {
4+
FICTION,
5+
NON_FICTION,
6+
MYSTERY,
7+
SCIENCE_FICTION,
8+
FANTASY,
9+
HORROR,
10+
ROMANCE,
11+
ADVENTURE,
12+
HISTORY,
13+
CRIME
14+
}
15+
16+
enum BookType {
17+
HARDCOVER,
18+
PAPERBACK,
19+
MAGAZINE,
20+
NOVEL,
21+
POEM,
22+
COMICS
23+
}
24+
25+
public class Book {
26+
private String title;
27+
private String author;
28+
private Genre genre;
29+
private BookType bookType;
30+
31+
public Book(String title, String author, Genre genre, BookType bookType) {
32+
this.title = title;
33+
this.author = author;
34+
this.genre = genre;
35+
this.bookType = bookType;
36+
}
37+
38+
public String getTitle() {
39+
return title;
40+
}
41+
42+
public String getAuthor() {
43+
return author;
44+
}
45+
46+
public Genre getGenre() {
47+
return genre;
48+
}
49+
50+
public BookType getBookType() {
51+
return bookType;
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.codedifferently.lesson16.chelseaogbonnia.bookshelf;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
8+
class BookNotFoundException extends Exception {
9+
public BookNotFoundException(String message) {
10+
super(message);
11+
}
12+
}
13+
14+
public class Bookshelf {
15+
private String owner;
16+
private int capacity;
17+
private List<Book> books;
18+
private HashMap<String, Book> bookMap;
19+
private boolean isPublic;
20+
21+
public Bookshelf(String owner, int capacity, boolean isPublic) {
22+
this.owner = owner;
23+
this.capacity = capacity;
24+
this.isPublic = isPublic;
25+
this.books = new ArrayList<>();
26+
this.bookMap = new HashMap<>();
27+
}
28+
29+
public List<Book> listBooks() {
30+
return Collections.unmodifiableList(books);
31+
}
32+
33+
public void addBook(Book book) throws Exception {
34+
if (books.size() >= capacity) {
35+
throw new Exception("Bookshelf is full. Cannot add more books.");
36+
}
37+
books.add(book);
38+
bookMap.put(book.getTitle(), book);
39+
}
40+
41+
public Book findBook(String title) throws BookNotFoundException {
42+
if (bookMap.containsKey(title)) {
43+
return bookMap.get(title);
44+
}
45+
throw new BookNotFoundException("Book titled \"" + title + "\" not found.");
46+
}
47+
48+
public boolean hasBook(String title) {
49+
return bookMap.containsKey(title);
50+
}
51+
52+
public int countBooksByGenre(Genre genre) {
53+
int count = 0;
54+
for (Book book : books) {
55+
if (book.getGenre() == genre) {
56+
count++;
57+
}
58+
}
59+
return count;
60+
}
61+
62+
public String getOwner() {
63+
return owner;
64+
}
65+
66+
public boolean isPublic() {
67+
return isPublic;
68+
}
69+
70+
@Override
71+
public String toString() {
72+
return "Bookshelf owned by "
73+
+ owner
74+
+ " with capacity "
75+
+ capacity
76+
+ " (Public: "
77+
+ isPublic
78+
+ ")";
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.codedifferently.lesson16.chelseaogbonnia.bookshelf;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class BookshelfTest {
11+
private Bookshelf bookshelf;
12+
13+
@BeforeEach
14+
public void setUp() {
15+
bookshelf = new Bookshelf("Chelsea", 10, true);
16+
}
17+
18+
@Test
19+
public void testAddBook() throws Exception {
20+
// Arrange
21+
Book book = new Book("Frankenstein", "Mary Shelly", Genre.HORROR, BookType.NOVEL);
22+
23+
// Act
24+
bookshelf.addBook(book);
25+
26+
// Assert
27+
assertTrue(bookshelf.hasBook("Frankenstein"));
28+
}
29+
30+
@Test
31+
public void testFindBookExists() throws Exception {
32+
// Arrange
33+
Book book =
34+
new Book("Brave New World", "Aldous Huxley", Genre.SCIENCE_FICTION, BookType.PAPERBACK);
35+
bookshelf.addBook(book);
36+
37+
// Act
38+
Book foundBook = bookshelf.findBook("Brave New World");
39+
40+
// Assert
41+
assertEquals(book, foundBook);
42+
}
43+
44+
@Test
45+
public void testFindBookNotExists() {
46+
// Arrange
47+
String nonExistentBookTitle = "Non-Existent Book";
48+
49+
// Act & Assert
50+
Exception exception =
51+
assertThrows(
52+
BookNotFoundException.class,
53+
() -> {
54+
bookshelf.findBook(nonExistentBookTitle);
55+
});
56+
assertEquals("Book titled \"" + nonExistentBookTitle + "\" not found.", exception.getMessage());
57+
}
58+
59+
@Test
60+
public void testCountBooksByGenre() throws Exception {
61+
// Arrange
62+
bookshelf.addBook(new Book("The Hobbit", "J.R.R. Tolkien", Genre.FANTASY, BookType.HARDCOVER));
63+
bookshelf.addBook(new Book("Dune", "Frank Herbert", Genre.SCIENCE_FICTION, BookType.HARDCOVER));
64+
65+
// Act & Assert
66+
assertEquals(1, bookshelf.countBooksByGenre(Genre.SCIENCE_FICTION));
67+
assertEquals(1, bookshelf.countBooksByGenre(Genre.FANTASY));
68+
}
69+
70+
@Test
71+
public void testAddBookToFullShelf() throws Exception {
72+
// Arrange
73+
for (int i = 0; i < 10; i++) {
74+
bookshelf.addBook(new Book("Book " + i, "Author " + i, Genre.FICTION, BookType.PAPERBACK));
75+
}
76+
Book extraBook = new Book("Extra Book", "Author Extra", Genre.FICTION, BookType.HARDCOVER);
77+
78+
// Act & Assert
79+
Exception exception =
80+
assertThrows(
81+
Exception.class,
82+
() -> {
83+
bookshelf.addBook(extraBook);
84+
});
85+
assertEquals("Bookshelf is full. Cannot add more books.", exception.getMessage());
86+
}
87+
88+
@Test
89+
public void testCountBooksByGenreNoBooks() {
90+
// Arrange
91+
Genre genreToCheck = Genre.ADVENTURE;
92+
93+
// Act & Assert
94+
assertEquals(0, bookshelf.countBooksByGenre(genreToCheck));
95+
}
96+
97+
@Test
98+
public void testListBooks() throws Exception {
99+
// Arrange
100+
bookshelf.addBook(
101+
new Book("The Adventures of Tintin", "Hergé", Genre.ADVENTURE, BookType.COMICS));
102+
bookshelf.addBook(new Book("The Big Sleep", "Raymond Chandler", Genre.CRIME, BookType.NOVEL));
103+
bookshelf.addBook(
104+
new Book("Don Quixote", "Miguel de Cervantes", Genre.ADVENTURE, BookType.NOVEL));
105+
bookshelf.addBook(new Book("Sonnet 18", "William Shakespeare", Genre.ROMANCE, BookType.POEM));
106+
107+
// Act
108+
int bookCount = bookshelf.listBooks().size();
109+
110+
// Assert
111+
assertEquals(4, bookCount);
112+
}
113+
114+
@Test
115+
public void checksMagazineBooks() throws Exception {
116+
// Arrange
117+
Book magazine =
118+
new Book("National Geographic", "Various", Genre.NON_FICTION, BookType.MAGAZINE);
119+
bookshelf.addBook(magazine);
120+
121+
// Act
122+
boolean hasMagazine = bookshelf.hasBook("National Geographic");
123+
int magazineCount = bookshelf.countBooksByGenre(Genre.NON_FICTION);
124+
125+
// Assert
126+
assertTrue(hasMagazine, "Bookshelf should contain the magazine 'National Geographic'.");
127+
assertEquals(
128+
1, magazineCount, "There should be 1 magazine in the bookshelf under NON_FICTION.");
129+
}
130+
}

0 commit comments

Comments
 (0)