diff --git a/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/AlreadyCheckedOutException.java b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/AlreadyCheckedOutException.java new file mode 100644 index 00000000..3bfec5a4 --- /dev/null +++ b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/AlreadyCheckedOutException.java @@ -0,0 +1,7 @@ +package main.java.com.codedifferently.lesson9.richhawkins; + +public class AlreadyCheckedOutException extends RuntimeException { + public AlreadyCheckedOutException(String message) { + super(message); + } +} diff --git a/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/Book.java b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/Book.java new file mode 100644 index 00000000..debf63f2 --- /dev/null +++ b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/Book.java @@ -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 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 getAuthors() { + return authors; + } + + public void setAuthors(List 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 authors, int numberOfPages, boolean checkedOut) { + this.title = title; + this.isbn = isbn; + this.authors = authors; + this.numberOfPages = numberOfPages; + this.checkedOut = checkedOut; + } +} diff --git a/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/DuplicatePatronException.java b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/DuplicatePatronException.java new file mode 100644 index 00000000..17b179a1 --- /dev/null +++ b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/DuplicatePatronException.java @@ -0,0 +1,7 @@ +package main.java.com.codedifferently.lesson9.richhawkins; + +public class DuplicatePatronException extends RuntimeException { + public DuplicatePatronException(String message) { + super(message); + } +} diff --git a/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/Library.java b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/Library.java new file mode 100644 index 00000000..221b13f1 --- /dev/null +++ b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/Library.java @@ -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 bookCollection = new HashSet<>(); + private List patrons = new ArrayList<>(); + + public HashSet getBookCollection() { + return bookCollection; + } + + public List 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()); + } + } +} diff --git a/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/Patron.java b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/Patron.java new file mode 100644 index 00000000..f8b23202 --- /dev/null +++ b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/richhawkins/Patron.java @@ -0,0 +1,28 @@ +package main.java.com.codedifferently.lesson9.richhawkins; + +import java.util.HashSet; + +public class Patron { + private String name; + private HashSet checkedOutBooks = new HashSet<>(); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public HashSet getCheckedOutBooks() { + return checkedOutBooks; + } + + public void setCheckedOutBooks(HashSet checkedOutBooks) { + this.checkedOutBooks = checkedOutBooks; + } + + public Patron(String name) { + this.name = name; + } +} diff --git a/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/richhawkins/LibraryProjectTest.java b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/richhawkins/LibraryProjectTest.java new file mode 100644 index 00000000..73cda863 --- /dev/null +++ b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/richhawkins/LibraryProjectTest.java @@ -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 authors = new ArrayList<>(); + authors.add("John Doe"); + authors.add("Jane Doe"); + + Book book = new Book("A Story About Something", 7654321, authors, 350, false); + List 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 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 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 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())); + } +}