Skip to content

Commit

Permalink
feat: adds chukwumaibezim folder with Patron, Library, and Book logic…
Browse files Browse the repository at this point in the history
… as well as chukwumaibezim folder for test for PatronTest, BookTest, and LibraryTest (#242)

* feat: adds logic for Library, Patron, and Book classes

* feat: adds checkOutBook and checkInBook methods to Patron class, and adds tests for BookTest, LibraryTest, and PatronTest

* feat: adds javadoc for Library, Patron, and Book classes

* fix: adds checkInBook and checkOutBook to make private in Patron to stop it being accessed aside from Library, creates performCheckIn and getCheckedOutBooks, applies Objects java util to compare objects so not repeated

* fix: readds javadoc to public methods

* tests: adds missing deps

Signed-off-by: Anthony D. Mays <anthony@morganlatimer.com>

---------

Signed-off-by: Anthony D. Mays <anthony@morganlatimer.com>
Co-authored-by: Anthony D. Mays <anthony@morganlatimer.com>
  • Loading branch information
cibezim and anthonydmays committed Mar 21, 2024
1 parent 22ce3de commit 68e317d
Show file tree
Hide file tree
Showing 6 changed files with 498 additions and 0 deletions.
@@ -0,0 +1,101 @@
package com.codedifferently.lesson9.chukwumaibezim;

import java.util.Objects;

/** Book class */
public class Book {
private String title;
private String ISBN;
private String author;
private int numberOfPages;
private boolean isCheckedOut;

/**
* Constructor for Book
*
* @param title
* @param ISBN
* @param author
* @param numberOfPages
*/
public Book(String title, String ISBN, String author, int numberOfPages) {
this.title = title;
this.ISBN = ISBN;
this.author = author;
this.numberOfPages = numberOfPages;
this.isCheckedOut = false;
}

/** getter for title */
public String getTitle() {
return title;
}

/** getter for ISBN */
public String getISBN() {
return ISBN;
}

/** getter for author */
public String getAuthor() {
return author;
}

/** getter for number of pages */
public int getNumberOfPages() {
return numberOfPages;
}

/**
* Check if book is checked out
*
* @return
*/
public boolean isCheckedOut() {
return isCheckedOut;
}

/** Check out book */
public void checkOut() {
isCheckedOut = true;
}

/** Check in book */
public void checkIn() {
isCheckedOut = false;
}

/**
* Returns a string representation of the book in the format: "title by author".
*
* @return A string representation of the book.
*/
@Override
public String toString() {
return title + " by " + author;
}

/**
* Checks if two libraries are equal by comparing their books and patrons collections.
*
* @param obj The object to compare with this library.
* @return True if the libraries are equal, false otherwise.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Book book = (Book) obj;
return Objects.equals(ISBN, book.ISBN);
}

/**
* Generates a hash code value for the library based on its books and patrons collections.
*
* @return The hash code value for the library.
*/
@Override
public int hashCode() {
return Objects.hash(ISBN);
}
}
@@ -0,0 +1,94 @@
package com.codedifferently.lesson9.chukwumaibezim;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Objects;

/** Library class */
public class Library {
private HashSet<Book> books;
private ArrayList<Patron> patrons;

/** Constructor for Library */
public Library() {
this.books = new HashSet<>();
this.patrons = new ArrayList<>();
}

/**
* Getters and Setters
*
* @return
*/
public void addBook(Book book) {
books.add(book);
}

/**
* Remove book from library
*
* @param book
*/
public void removeBook(Book book) {
books.remove(book);
}

/**
* Add patron to library
*
* @param patron
*/
public void addPatron(Patron patron) {
patrons.add(patron);
}

/**
* Checks to see if book is in library, if so, checks out book
*
* @param book
* @param patron
*/
public void checkOutBook(Book book, Patron patron) {
if (books.contains(book) && !book.isCheckedOut()) {
patron.performCheckout(book);
book.checkOut();
}
}

/**
* Checks to see if book is in library, if so, checks in book
*
* @param book
* @param patron
*/
public void returnBook(Book book, Patron patron) {
if (patron.getCheckedOutBooks().contains(book)) {
patron.performCheckIn(book);
book.checkIn();
}
}

/**
* Checks if two libraries are equal by comparing their books and patrons collections.
*
* @param obj The object to compare with this library.
* @return True if the libraries are equal, false otherwise.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Library library = (Library) obj;
return Objects.equals(books, library.books) && Objects.equals(patrons, library.patrons);
}

/**
* Generates a hash code value for the library based on its books and patrons collections.
*
* @return The hash code value for the library.
*/
@Override
public int hashCode() {
return Objects.hash(books, patrons);
}
}
@@ -0,0 +1,99 @@
package com.codedifferently.lesson9.chukwumaibezim;

import java.util.HashSet;
import java.util.Objects;

/** Patron class */
public class Patron {
private String name;
private HashSet<Book> checkedOutBooks;

/**
* Constructor for Patron
*
* @param name
*/
public Patron(String name) {
this.name = name;
this.checkedOutBooks = new HashSet<>();
}

/**
* Getters and Setters
*
* @return
*/
public String getName() {
return name;
}

/**
* Check out book private method for Patron only
*
* @param book
*/
private void checkOutBook(Book book) {
checkedOutBooks.add(book);
}

/**
* Check in book private method for Patron only
*
* @param book
*/
private void checkInBook(Book book) {
checkedOutBooks.remove(book);
}

/**
* Perform checkout public method for Library
*
* @param book
*/
public void performCheckout(Book book) {
checkOutBook(book);
}

/**
* Perform check-in public method for Library
*
* @param book
*/
public void performCheckIn(Book book) {
checkInBook(book);
}

/**
* Get checked out books
*
* @return
*/
public HashSet<Book> getCheckedOutBooks() {
return checkedOutBooks;
}

/**
* Checks if two libraries are equal by comparing their books and patrons collections.
*
* @param obj The object to compare with this library.
* @return True if the libraries are equal, false otherwise.
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Patron patron = (Patron) o;
return Objects.equals(name, patron.name)
&& Objects.equals(checkedOutBooks, patron.checkedOutBooks);
}

/**
* Generates a hash code value for the library based on its books and patrons collections.
*
* @return The hash code value for the library.
*/
@Override
public int hashCode() {
return Objects.hash(name, checkedOutBooks);
}
}
@@ -0,0 +1,60 @@
package com.codedifferently.lesson9.chukwumibezim;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.codedifferently.lesson9.chukwumaibezim.Book;
import org.junit.jupiter.api.Test;

public class BookTest {
@Test
public void testGetTitle() {
Book book = new Book("To Kill a Mockingbird", "9780061120084", "Harper Lee", 336);
assertEquals("To Kill a Mockingbird", book.getTitle());
}

@Test
public void testGetISBN() {
Book book =
new Book(
"Monster: The Autobiography of an L.A. Gang Member",
"9780802141446",
"Sanyika Shakur",
400);
assertEquals("9780802141446", book.getISBN());
}

@Test
public void testGetAuthor() {
Book book = new Book("The Art of War", "9780143037521", "Sun Tzu", 288);
assertEquals("Sun Tzu", book.getAuthor());
}

@Test
public void testGetNumberOfPages() {
Book book = new Book("Stormbreaker (Alex Rider)", "9780142406113", "Anthony Horowitz", 240);
assertEquals(240, book.getNumberOfPages());
}

@Test
public void testIsCheckedOut() {
Book book = new Book("Homeboyz", "9780439933497", "Alan Lawrence Sitomer", 256);
assertFalse(book.isCheckedOut());
}

@Test
public void testCheckOut() {
Book book = new Book("Tyrell", "9780439838808", "Coe Booth", 320);
book.checkOut();
assertTrue(book.isCheckedOut());
}

@Test
public void testCheckIn() {
Book book = new Book("The Art of War", "9780143037521", "Sun Tzu", 288);
book.checkOut();
book.checkIn();
assertFalse(book.isCheckedOut());
}
}

0 comments on commit 68e317d

Please sign in to comment.