-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
e4eab0e
commit b8b615d
Showing
6 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...app/src/main/java/com/codedifferently/lesson9/richhawkins/AlreadyCheckedOutException.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,7 @@ | ||
package main.java.com.codedifferently.lesson9.richhawkins; | ||
|
||
public class AlreadyCheckedOutException extends RuntimeException { | ||
public AlreadyCheckedOutException(String message) { | ||
super(message); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main.java.com.codedifferently.lesson9.richhawkins; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Book { | ||
private String title; | ||
private int isbn; | ||
private List<String> authors = new ArrayList<>(); | ||
private int numberOfPages; | ||
private boolean checkedOut; | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public int getIsbn() { | ||
return isbn; | ||
} | ||
|
||
public void setIsbn(int isbn) { | ||
this.isbn = isbn; | ||
} | ||
|
||
public List<String> getAuthors() { | ||
return authors; | ||
} | ||
|
||
public void setAuthors(List<String> authors) { | ||
this.authors = authors; | ||
} | ||
|
||
public int getNumberOfPages() { | ||
return numberOfPages; | ||
} | ||
|
||
public void setNumberOfPages(int numberOfPages) { | ||
this.numberOfPages = numberOfPages; | ||
} | ||
|
||
public boolean getCheckedOut() { | ||
return checkedOut; | ||
} | ||
|
||
public void setCheckedOut(boolean checkedOut) { | ||
this.checkedOut = checkedOut; | ||
} | ||
|
||
public Book(String title, int isbn, List<String> authors, int numberOfPages, boolean checkedOut) { | ||
this.title = title; | ||
this.isbn = isbn; | ||
this.authors = authors; | ||
this.numberOfPages = numberOfPages; | ||
this.checkedOut = checkedOut; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...p_app/src/main/java/com/codedifferently/lesson9/richhawkins/DuplicatePatronException.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,7 @@ | ||
package main.java.com.codedifferently.lesson9.richhawkins; | ||
|
||
public class DuplicatePatronException extends RuntimeException { | ||
public DuplicatePatronException(String message) { | ||
super(message); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/Library.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,54 @@ | ||
package main.java.com.codedifferently.lesson9.richhawkins; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
|
||
public class Library { | ||
private HashSet<Book> bookCollection = new HashSet<>(); | ||
private List<Patron> patrons = new ArrayList<>(); | ||
|
||
public HashSet<Book> getBookCollection() { | ||
return bookCollection; | ||
} | ||
|
||
public List<Patron> getPatrons() { | ||
return patrons; | ||
} | ||
|
||
public void addBook(Book book) { | ||
bookCollection.add(book); | ||
} | ||
|
||
public void removeBook(Book book) { | ||
bookCollection.remove(book); | ||
} | ||
|
||
public void registerNewPatron(Patron patron) { | ||
if (patrons.contains(patron)) { | ||
throw new DuplicatePatronException("Error: This patron already exists."); | ||
} | ||
patrons.add(patron); | ||
} | ||
|
||
public void checkOutBook(Book book, Patron patron, Library library) { | ||
if (book.getCheckedOut()) { | ||
throw new AlreadyCheckedOutException("Error: The book is already checked out."); | ||
} | ||
|
||
if (patrons.contains(patron) && library.getBookCollection().contains(book)) { | ||
book.setCheckedOut(true); | ||
patron.getCheckedOutBooks().add(book.getTitle()); | ||
} else { | ||
throw new IllegalArgumentException( | ||
"Error: Either the patron is not registered or the book is not in the library's collection."); | ||
} | ||
} | ||
|
||
public void checkBookIn(Book book, Patron patron, Library library) { | ||
if (book.getCheckedOut() && patrons.contains(patron) && library.bookCollection.contains(book)) { | ||
book.setCheckedOut(false); | ||
patron.getCheckedOutBooks().remove(book.getTitle()); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/Patron.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,28 @@ | ||
package main.java.com.codedifferently.lesson9.richhawkins; | ||
|
||
import java.util.HashSet; | ||
|
||
public class Patron { | ||
private String name; | ||
private HashSet<String> checkedOutBooks = new HashSet<>(); | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public HashSet<String> getCheckedOutBooks() { | ||
return checkedOutBooks; | ||
} | ||
|
||
public void setCheckedOutBooks(HashSet<String> checkedOutBooks) { | ||
this.checkedOutBooks = checkedOutBooks; | ||
} | ||
|
||
public Patron(String name) { | ||
this.name = name; | ||
} | ||
} |
148 changes: 148 additions & 0 deletions
148
...oop/oop_app/src/test/java/com/codedifferently/lesson9/richhawkins/LibraryProjectTest.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,148 @@ | ||
package test.java.com.codedifferently.lesson9.richhawkins; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import main.java.com.codedifferently.lesson9.richhawkins.AlreadyCheckedOutException; | ||
import main.java.com.codedifferently.lesson9.richhawkins.Book; | ||
import main.java.com.codedifferently.lesson9.richhawkins.Library; | ||
import main.java.com.codedifferently.lesson9.richhawkins.Patron; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class LibraryProjectTest { | ||
@Test | ||
public void testBookAccessors() { | ||
List<String> authors = new ArrayList<>(); | ||
authors.add("John Doe"); | ||
authors.add("Jane Doe"); | ||
|
||
Book book = new Book("A Story About Something", 7654321, authors, 350, false); | ||
List<String> newAuthors = new ArrayList<>(); | ||
newAuthors.add("Abe Lincoln"); | ||
|
||
book.setCheckedOut(true); | ||
assertEquals(true, book.getCheckedOut()); | ||
book.setTitle("Fun"); | ||
assertEquals("Fun", book.getTitle()); | ||
book.setIsbn(1234567); | ||
assertEquals(1234567, book.getIsbn()); | ||
book.setAuthors(newAuthors); | ||
assertEquals(newAuthors, book.getAuthors()); | ||
book.setNumberOfPages(250); | ||
assertEquals(250, book.getNumberOfPages()); | ||
} | ||
|
||
@Test | ||
public void testPatronAccessors() { | ||
Patron patron = new Patron("Rich"); | ||
HashSet<String> books = new HashSet<>(); | ||
books.add("A New Tale"); | ||
books.add("A Newer Tale"); | ||
|
||
patron.setName("Nick"); | ||
assertEquals("Nick", patron.getName()); | ||
patron.setCheckedOutBooks(books); | ||
assertEquals(books, patron.getCheckedOutBooks()); | ||
} | ||
|
||
@Test | ||
public void testAddBook() { | ||
Library library = new Library(); | ||
List<String> authors = new ArrayList<>(); | ||
authors.add("John Doe"); | ||
Book book = new Book("Test Book", 1234, authors, 145, false); | ||
|
||
library.addBook(book); | ||
|
||
assertTrue(library.getBookCollection().contains(book)); | ||
} | ||
|
||
@Test | ||
public void testRemoveBook() { | ||
Library library = new Library(); | ||
List<String> authors = new ArrayList<>(); | ||
authors.add("John Doe"); | ||
Book book = new Book("Test Book", 1234, authors, 145, false); | ||
|
||
library.removeBook(book); | ||
|
||
assertTrue(!library.getBookCollection().contains(book)); | ||
} | ||
|
||
@Test | ||
public void testRegisterNewPatron() { | ||
Library library = new Library(); | ||
Patron patron = new Patron("Rich"); | ||
|
||
library.registerNewPatron(patron); | ||
|
||
assertTrue(library.getPatrons().contains(patron)); | ||
} | ||
|
||
@Test | ||
public void testCheckOutBook() { | ||
// Create a library, book, and patrons | ||
Library library = new Library(); | ||
Book book = new Book("Test Book", 1234, new ArrayList<>(), 145, false); | ||
Patron patron1 = new Patron("John Doe"); | ||
Patron patron2 = new Patron("Jane Doe"); | ||
|
||
// Add the book to the library | ||
library.addBook(book); | ||
|
||
// Register patron1 with the library | ||
library.registerNewPatron(patron1); | ||
|
||
// Check out the book to patron1 | ||
library.checkOutBook(book, patron1, library); | ||
|
||
// Verify that the book is checked out | ||
assertTrue(book.getCheckedOut()); | ||
|
||
// Verify that patron1 has the book checked out | ||
assertTrue(patron1.getCheckedOutBooks().contains(book.getTitle())); | ||
|
||
// Try to check out the book to patron2 (should fail) | ||
try { | ||
library.checkOutBook(book, patron2, library); | ||
fail("Expected AlreadyCheckedOutException was not thrown"); | ||
} catch (AlreadyCheckedOutException e) { | ||
// Expected exception, do nothing | ||
} | ||
// Verify that the book is still checked out by patron1 | ||
assertTrue(book.getCheckedOut()); | ||
assertTrue(patron1.getCheckedOutBooks().contains(book.getTitle())); | ||
} | ||
|
||
@Test | ||
public void testCheckBookIn() { | ||
// Create a library, book, and patron | ||
Library library = new Library(); | ||
Book book = new Book("Test Book", 1234, new ArrayList<>(), 145, false); | ||
Patron patron = new Patron("John Doe"); | ||
|
||
// Add the book to the library | ||
library.addBook(book); | ||
|
||
// Register the patron with the library | ||
library.registerNewPatron(patron); | ||
|
||
// Check out the book to the patron | ||
library.checkOutBook(book, patron, library); | ||
|
||
// Verify that the book is checked out | ||
assertTrue(book.getCheckedOut()); | ||
|
||
// Check the book back in | ||
library.checkBookIn(book, patron, library); | ||
|
||
// Verify that the book is no longer checked out | ||
assertFalse(book.getCheckedOut()); | ||
|
||
// Verify that the patron no longer has the book checked out | ||
assertFalse(patron.getCheckedOutBooks().contains(book.getTitle())); | ||
} | ||
} |