Skip to content

Commit b8b615d

Browse files
authored
feat: creates a library management system with required functionality for Lesson9 (#258)
* feat: adds book and patron class and test file * feat: adds add/removeBook methods to Library and corresponding tests * feat: adds method to register patrons and corresponding test * feat: adds exception for duplicate patrons during registration. * feat: adds checkoutBook method and corresponding test * feat: creates new custom exception and tweaks tests to account for it * fix: tweaks checkedoutbook function * feat: adds checkBookIn function and corresponding tests. fix: tweaks tests and checkBookOut function.
1 parent e4eab0e commit b8b615d

File tree

6 files changed

+304
-0
lines changed

6 files changed

+304
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main.java.com.codedifferently.lesson9.richhawkins;
2+
3+
public class AlreadyCheckedOutException extends RuntimeException {
4+
public AlreadyCheckedOutException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main.java.com.codedifferently.lesson9.richhawkins;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Book {
7+
private String title;
8+
private int isbn;
9+
private List<String> authors = new ArrayList<>();
10+
private int numberOfPages;
11+
private boolean checkedOut;
12+
13+
public String getTitle() {
14+
return title;
15+
}
16+
17+
public void setTitle(String title) {
18+
this.title = title;
19+
}
20+
21+
public int getIsbn() {
22+
return isbn;
23+
}
24+
25+
public void setIsbn(int isbn) {
26+
this.isbn = isbn;
27+
}
28+
29+
public List<String> getAuthors() {
30+
return authors;
31+
}
32+
33+
public void setAuthors(List<String> authors) {
34+
this.authors = authors;
35+
}
36+
37+
public int getNumberOfPages() {
38+
return numberOfPages;
39+
}
40+
41+
public void setNumberOfPages(int numberOfPages) {
42+
this.numberOfPages = numberOfPages;
43+
}
44+
45+
public boolean getCheckedOut() {
46+
return checkedOut;
47+
}
48+
49+
public void setCheckedOut(boolean checkedOut) {
50+
this.checkedOut = checkedOut;
51+
}
52+
53+
public Book(String title, int isbn, List<String> authors, int numberOfPages, boolean checkedOut) {
54+
this.title = title;
55+
this.isbn = isbn;
56+
this.authors = authors;
57+
this.numberOfPages = numberOfPages;
58+
this.checkedOut = checkedOut;
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main.java.com.codedifferently.lesson9.richhawkins;
2+
3+
public class DuplicatePatronException extends RuntimeException {
4+
public DuplicatePatronException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main.java.com.codedifferently.lesson9.richhawkins;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
7+
public class Library {
8+
private HashSet<Book> bookCollection = new HashSet<>();
9+
private List<Patron> patrons = new ArrayList<>();
10+
11+
public HashSet<Book> getBookCollection() {
12+
return bookCollection;
13+
}
14+
15+
public List<Patron> getPatrons() {
16+
return patrons;
17+
}
18+
19+
public void addBook(Book book) {
20+
bookCollection.add(book);
21+
}
22+
23+
public void removeBook(Book book) {
24+
bookCollection.remove(book);
25+
}
26+
27+
public void registerNewPatron(Patron patron) {
28+
if (patrons.contains(patron)) {
29+
throw new DuplicatePatronException("Error: This patron already exists.");
30+
}
31+
patrons.add(patron);
32+
}
33+
34+
public void checkOutBook(Book book, Patron patron, Library library) {
35+
if (book.getCheckedOut()) {
36+
throw new AlreadyCheckedOutException("Error: The book is already checked out.");
37+
}
38+
39+
if (patrons.contains(patron) && library.getBookCollection().contains(book)) {
40+
book.setCheckedOut(true);
41+
patron.getCheckedOutBooks().add(book.getTitle());
42+
} else {
43+
throw new IllegalArgumentException(
44+
"Error: Either the patron is not registered or the book is not in the library's collection.");
45+
}
46+
}
47+
48+
public void checkBookIn(Book book, Patron patron, Library library) {
49+
if (book.getCheckedOut() && patrons.contains(patron) && library.bookCollection.contains(book)) {
50+
book.setCheckedOut(false);
51+
patron.getCheckedOutBooks().remove(book.getTitle());
52+
}
53+
}
54+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main.java.com.codedifferently.lesson9.richhawkins;
2+
3+
import java.util.HashSet;
4+
5+
public class Patron {
6+
private String name;
7+
private HashSet<String> checkedOutBooks = new HashSet<>();
8+
9+
public String getName() {
10+
return name;
11+
}
12+
13+
public void setName(String name) {
14+
this.name = name;
15+
}
16+
17+
public HashSet<String> getCheckedOutBooks() {
18+
return checkedOutBooks;
19+
}
20+
21+
public void setCheckedOutBooks(HashSet<String> checkedOutBooks) {
22+
this.checkedOutBooks = checkedOutBooks;
23+
}
24+
25+
public Patron(String name) {
26+
this.name = name;
27+
}
28+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package test.java.com.codedifferently.lesson9.richhawkins;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import java.util.ArrayList;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import main.java.com.codedifferently.lesson9.richhawkins.AlreadyCheckedOutException;
10+
import main.java.com.codedifferently.lesson9.richhawkins.Book;
11+
import main.java.com.codedifferently.lesson9.richhawkins.Library;
12+
import main.java.com.codedifferently.lesson9.richhawkins.Patron;
13+
import org.junit.jupiter.api.Test;
14+
15+
public class LibraryProjectTest {
16+
@Test
17+
public void testBookAccessors() {
18+
List<String> authors = new ArrayList<>();
19+
authors.add("John Doe");
20+
authors.add("Jane Doe");
21+
22+
Book book = new Book("A Story About Something", 7654321, authors, 350, false);
23+
List<String> newAuthors = new ArrayList<>();
24+
newAuthors.add("Abe Lincoln");
25+
26+
book.setCheckedOut(true);
27+
assertEquals(true, book.getCheckedOut());
28+
book.setTitle("Fun");
29+
assertEquals("Fun", book.getTitle());
30+
book.setIsbn(1234567);
31+
assertEquals(1234567, book.getIsbn());
32+
book.setAuthors(newAuthors);
33+
assertEquals(newAuthors, book.getAuthors());
34+
book.setNumberOfPages(250);
35+
assertEquals(250, book.getNumberOfPages());
36+
}
37+
38+
@Test
39+
public void testPatronAccessors() {
40+
Patron patron = new Patron("Rich");
41+
HashSet<String> books = new HashSet<>();
42+
books.add("A New Tale");
43+
books.add("A Newer Tale");
44+
45+
patron.setName("Nick");
46+
assertEquals("Nick", patron.getName());
47+
patron.setCheckedOutBooks(books);
48+
assertEquals(books, patron.getCheckedOutBooks());
49+
}
50+
51+
@Test
52+
public void testAddBook() {
53+
Library library = new Library();
54+
List<String> authors = new ArrayList<>();
55+
authors.add("John Doe");
56+
Book book = new Book("Test Book", 1234, authors, 145, false);
57+
58+
library.addBook(book);
59+
60+
assertTrue(library.getBookCollection().contains(book));
61+
}
62+
63+
@Test
64+
public void testRemoveBook() {
65+
Library library = new Library();
66+
List<String> authors = new ArrayList<>();
67+
authors.add("John Doe");
68+
Book book = new Book("Test Book", 1234, authors, 145, false);
69+
70+
library.removeBook(book);
71+
72+
assertTrue(!library.getBookCollection().contains(book));
73+
}
74+
75+
@Test
76+
public void testRegisterNewPatron() {
77+
Library library = new Library();
78+
Patron patron = new Patron("Rich");
79+
80+
library.registerNewPatron(patron);
81+
82+
assertTrue(library.getPatrons().contains(patron));
83+
}
84+
85+
@Test
86+
public void testCheckOutBook() {
87+
// Create a library, book, and patrons
88+
Library library = new Library();
89+
Book book = new Book("Test Book", 1234, new ArrayList<>(), 145, false);
90+
Patron patron1 = new Patron("John Doe");
91+
Patron patron2 = new Patron("Jane Doe");
92+
93+
// Add the book to the library
94+
library.addBook(book);
95+
96+
// Register patron1 with the library
97+
library.registerNewPatron(patron1);
98+
99+
// Check out the book to patron1
100+
library.checkOutBook(book, patron1, library);
101+
102+
// Verify that the book is checked out
103+
assertTrue(book.getCheckedOut());
104+
105+
// Verify that patron1 has the book checked out
106+
assertTrue(patron1.getCheckedOutBooks().contains(book.getTitle()));
107+
108+
// Try to check out the book to patron2 (should fail)
109+
try {
110+
library.checkOutBook(book, patron2, library);
111+
fail("Expected AlreadyCheckedOutException was not thrown");
112+
} catch (AlreadyCheckedOutException e) {
113+
// Expected exception, do nothing
114+
}
115+
// Verify that the book is still checked out by patron1
116+
assertTrue(book.getCheckedOut());
117+
assertTrue(patron1.getCheckedOutBooks().contains(book.getTitle()));
118+
}
119+
120+
@Test
121+
public void testCheckBookIn() {
122+
// Create a library, book, and patron
123+
Library library = new Library();
124+
Book book = new Book("Test Book", 1234, new ArrayList<>(), 145, false);
125+
Patron patron = new Patron("John Doe");
126+
127+
// Add the book to the library
128+
library.addBook(book);
129+
130+
// Register the patron with the library
131+
library.registerNewPatron(patron);
132+
133+
// Check out the book to the patron
134+
library.checkOutBook(book, patron, library);
135+
136+
// Verify that the book is checked out
137+
assertTrue(book.getCheckedOut());
138+
139+
// Check the book back in
140+
library.checkBookIn(book, patron, library);
141+
142+
// Verify that the book is no longer checked out
143+
assertFalse(book.getCheckedOut());
144+
145+
// Verify that the patron no longer has the book checked out
146+
assertFalse(patron.getCheckedOutBooks().contains(book.getTitle()));
147+
}
148+
}

0 commit comments

Comments
 (0)