Skip to content

Commit c08cf3b

Browse files
authored
feat: adds new LibraryBook object for lesson 16 (David Smith) (#633)
* feat: Added LibraryBook.java & LibraryBookTest.java * Merge branch 'code-differently:main' into DavidSmith-Lesson16 * Merge branch 'code-differently:main' into DavidSmith-Lesson16 * Update LibraryBookTest.java
1 parent b68534e commit c08cf3b

File tree

2 files changed

+308
-0
lines changed

2 files changed

+308
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.codedifferently.lesson16.librarybook;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class LibraryBook {
7+
8+
public enum Genre {
9+
FICTION,
10+
NONFICTION,
11+
MYSTERY,
12+
SCIENCE,
13+
FANTASY
14+
}
15+
16+
private String title;
17+
private String author;
18+
private Genre genre;
19+
private int publicationYear;
20+
private List<String> borrowersHistory;
21+
private boolean isCheckedOut;
22+
private List<String> newBorrowers = new ArrayList<>();
23+
24+
public LibraryBook(String title, String author, Genre genre, int publicationYear) {
25+
this.title = title;
26+
this.author = author;
27+
this.genre = genre;
28+
this.publicationYear = publicationYear;
29+
this.borrowersHistory = new ArrayList<>();
30+
this.isCheckedOut = false;
31+
this.newBorrowers = newBorrowers;
32+
}
33+
34+
public void checkOut(String borrower) throws OverdueException {
35+
if (isCheckedOut) {
36+
throw new OverdueException("Book is already checked out!");
37+
}
38+
this.isCheckedOut = true;
39+
borrowersHistory.add(borrower);
40+
}
41+
42+
public void returnBook(boolean isOverdue) throws OverdueException {
43+
if (!isCheckedOut) {
44+
throw new OverdueException("Book was not checked out!");
45+
}
46+
if (isOverdue) {
47+
throw new OverdueException("Book is overdue!");
48+
}
49+
this.isCheckedOut = false;
50+
}
51+
52+
public String displayBorrowerHistory() {
53+
StringBuilder history = new StringBuilder("Borrower History:\n");
54+
for (String borrower : borrowersHistory) {
55+
history.append("- ").append(borrower).append("\n");
56+
}
57+
return history.toString();
58+
}
59+
60+
public String getTitle() {
61+
return title;
62+
}
63+
64+
public void setTitle(String title) {
65+
this.title = title;
66+
}
67+
68+
public String getAuthor() {
69+
return author;
70+
}
71+
72+
public void setAuthor(String author) {
73+
this.author = author;
74+
}
75+
76+
public Genre getGenre() {
77+
return genre;
78+
}
79+
80+
public void setGenre(Genre genre) {
81+
this.genre = genre;
82+
}
83+
84+
public int getPublicationYear() {
85+
return publicationYear;
86+
}
87+
88+
public void setPublicationYear(int publicationYear) {
89+
this.publicationYear = publicationYear;
90+
}
91+
92+
public List<String> getBorrowersHistory() {
93+
return borrowersHistory;
94+
}
95+
96+
public void setBorrowersHistory(List<String> borrowersHistory) {
97+
this.borrowersHistory = borrowersHistory;
98+
}
99+
100+
public boolean isCheckedOut() {
101+
return isCheckedOut;
102+
}
103+
104+
public void setCheckedOut(boolean isCheckedOut) {
105+
this.isCheckedOut = isCheckedOut;
106+
}
107+
108+
public List<String> getNewBorrowers() {
109+
return newBorrowers;
110+
}
111+
112+
public void setNewBorrowers(List<String> newBorrowers) {
113+
this.newBorrowers = newBorrowers;
114+
}
115+
116+
public static class OverdueException extends Exception {
117+
public OverdueException(String message) {
118+
super(message);
119+
}
120+
}
121+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package com.codedifferently.lesson16.librarybook;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
import static org.junit.jupiter.api.Assertions.fail;
8+
9+
import java.util.List;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class LibraryBookTest {
14+
15+
private LibraryBook book;
16+
17+
@BeforeEach
18+
public void setUp() {
19+
book = new LibraryBook("1984", "George Orwell", LibraryBook.Genre.FICTION, 1949);
20+
}
21+
22+
@Test
23+
public void testConstructor() {
24+
assertEquals("1984", book.getTitle());
25+
assertEquals("George Orwell", book.getAuthor());
26+
assertEquals(LibraryBook.Genre.FICTION, book.getGenre());
27+
assertEquals(1949, book.getPublicationYear());
28+
}
29+
30+
@Test
31+
public void testCheckOut() throws LibraryBook.OverdueException {
32+
book.checkOut("Alice");
33+
assertTrue(book.isCheckedOut());
34+
}
35+
36+
@Test
37+
public void testReturnBook() throws LibraryBook.OverdueException {
38+
book.checkOut("Alice");
39+
book.returnBook(false);
40+
assertFalse(book.isCheckedOut());
41+
}
42+
43+
@Test
44+
public void testDisplayBorrowerHistory() throws LibraryBook.OverdueException {
45+
book.checkOut("Alice");
46+
book.returnBook(false);
47+
book.checkOut("Bob");
48+
book.returnBook(false);
49+
50+
String expectedHistory = "Borrower History:\n- Alice\n- Bob\n";
51+
52+
assertEquals(expectedHistory, book.displayBorrowerHistory());
53+
}
54+
55+
@Test
56+
public void testOverdueExceptionOnReturn() throws LibraryBook.OverdueException {
57+
book.checkOut("Alice");
58+
Exception exception =
59+
assertThrows(
60+
LibraryBook.OverdueException.class,
61+
() -> {
62+
book.returnBook(true);
63+
});
64+
assertEquals("Book is overdue!", exception.getMessage());
65+
}
66+
67+
@Test
68+
public void testCheckOutAlreadyCheckedOut() throws LibraryBook.OverdueException {
69+
book.checkOut("Alice");
70+
Exception exception =
71+
assertThrows(
72+
LibraryBook.OverdueException.class,
73+
() -> {
74+
book.checkOut("Bob");
75+
});
76+
assertEquals("Book is already checked out!", exception.getMessage());
77+
}
78+
79+
@Test
80+
public void testReturnBookNotCheckedOut() {
81+
Exception exception =
82+
assertThrows(
83+
LibraryBook.OverdueException.class,
84+
() -> {
85+
book.returnBook(false);
86+
});
87+
assertEquals("Book was not checked out!", exception.getMessage());
88+
}
89+
90+
@Test
91+
public void testCheckOutAddsToHistory() throws LibraryBook.OverdueException {
92+
book.checkOut("Charlie");
93+
assertEquals(1, book.getBorrowersHistory().size());
94+
assertEquals("Charlie", book.getBorrowersHistory().get(0));
95+
}
96+
97+
@Test
98+
public void testMultipleCheckOutsAndReturns() throws LibraryBook.OverdueException {
99+
book.checkOut("Alice");
100+
book.returnBook(false);
101+
book.checkOut("Bob");
102+
book.returnBook(false);
103+
book.checkOut("Charlie");
104+
105+
assertEquals(3, book.getBorrowersHistory().size());
106+
assertEquals("Alice", book.getBorrowersHistory().get(0));
107+
assertEquals("Bob", book.getBorrowersHistory().get(1));
108+
assertEquals("Charlie", book.getBorrowersHistory().get(2));
109+
}
110+
111+
@Test
112+
public void testBorrowerHistoryIsEmptyInitially() {
113+
assertEquals(0, book.getBorrowersHistory().size());
114+
}
115+
116+
@Test
117+
public void testGenre() {
118+
assertEquals(LibraryBook.Genre.FICTION, book.getGenre());
119+
}
120+
121+
@Test
122+
public void testSetTitle() {
123+
book.setTitle("Animal Farm");
124+
assertEquals("Animal Farm", book.getTitle(), "Title should be updated to 'Animal Farm'");
125+
}
126+
127+
@Test
128+
public void testSetAuthor() {
129+
book.setAuthor("Aldous Huxley");
130+
assertEquals("Aldous Huxley", book.getAuthor(), "Author should be updated to 'Aldous Huxley'");
131+
}
132+
133+
@Test
134+
public void testSetGenre() {
135+
book.setGenre(LibraryBook.Genre.NONFICTION);
136+
assertEquals(
137+
LibraryBook.Genre.NONFICTION, book.getGenre(), "Genre should be updated to NON_FICTION");
138+
}
139+
140+
@Test
141+
public void testSetPublicationYear() {
142+
book.setPublicationYear(1932);
143+
assertEquals(1932, book.getPublicationYear(), "Publication year should be updated to 1932");
144+
}
145+
146+
@Test
147+
public void testSetCheckedOutStatus() {
148+
book.setCheckedOut(true);
149+
assertTrue(book.isCheckedOut(), "Checked out status should be updated to true");
150+
}
151+
152+
@Test
153+
public void testSetBorrowersHistory() {
154+
book.setBorrowersHistory(List.of("Charlie", "Dana"));
155+
156+
assertEquals(
157+
2, book.getBorrowersHistory().size(), "Borrowers history should contain 2 entries");
158+
assertEquals(
159+
"Charlie", book.getBorrowersHistory().get(0), "First borrower should be 'Charlie'");
160+
assertEquals("Dana", book.getBorrowersHistory().get(1), "Second borrower should be 'Dana'");
161+
162+
try {
163+
book.getBorrowersHistory().add("Eve");
164+
fail("borrowersHistory should not be modifiable externally");
165+
} catch (UnsupportedOperationException e) {
166+
}
167+
}
168+
169+
@Test
170+
public void testSetNewBorrowers() {
171+
book.setNewBorrowers(List.of("Alice", "Bob"));
172+
173+
assertEquals(2, book.getNewBorrowers().size(), "New borrowers should contain 2 entries");
174+
assertEquals("Alice", book.getNewBorrowers().get(0), "First new borrower should be 'Alice'");
175+
assertEquals("Bob", book.getNewBorrowers().get(1), "Second new borrower should be 'Bob'");
176+
}
177+
178+
@Test
179+
public void testGetNewBorrowers() {
180+
book.setNewBorrowers(List.of("Charlie", "Dana"));
181+
182+
List<String> borrowers = book.getNewBorrowers();
183+
assertEquals(2, borrowers.size(), "Should return 2 new borrowers");
184+
assertEquals("Charlie", borrowers.get(0), "First new borrower should be 'Charlie'");
185+
assertEquals("Dana", borrowers.get(1), "Second new borrower should be 'Dana'");
186+
}
187+
}

0 commit comments

Comments
 (0)