Skip to content

Commit 68e317d

Browse files
feat: adds chukwumaibezim folder with Patron, Library, and Book logic 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 <[email protected]> --------- Signed-off-by: Anthony D. Mays <[email protected]> Co-authored-by: Anthony D. Mays <[email protected]>
1 parent 22ce3de commit 68e317d

File tree

6 files changed

+498
-0
lines changed

6 files changed

+498
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.codedifferently.lesson9.chukwumaibezim;
2+
3+
import java.util.Objects;
4+
5+
/** Book class */
6+
public class Book {
7+
private String title;
8+
private String ISBN;
9+
private String author;
10+
private int numberOfPages;
11+
private boolean isCheckedOut;
12+
13+
/**
14+
* Constructor for Book
15+
*
16+
* @param title
17+
* @param ISBN
18+
* @param author
19+
* @param numberOfPages
20+
*/
21+
public Book(String title, String ISBN, String author, int numberOfPages) {
22+
this.title = title;
23+
this.ISBN = ISBN;
24+
this.author = author;
25+
this.numberOfPages = numberOfPages;
26+
this.isCheckedOut = false;
27+
}
28+
29+
/** getter for title */
30+
public String getTitle() {
31+
return title;
32+
}
33+
34+
/** getter for ISBN */
35+
public String getISBN() {
36+
return ISBN;
37+
}
38+
39+
/** getter for author */
40+
public String getAuthor() {
41+
return author;
42+
}
43+
44+
/** getter for number of pages */
45+
public int getNumberOfPages() {
46+
return numberOfPages;
47+
}
48+
49+
/**
50+
* Check if book is checked out
51+
*
52+
* @return
53+
*/
54+
public boolean isCheckedOut() {
55+
return isCheckedOut;
56+
}
57+
58+
/** Check out book */
59+
public void checkOut() {
60+
isCheckedOut = true;
61+
}
62+
63+
/** Check in book */
64+
public void checkIn() {
65+
isCheckedOut = false;
66+
}
67+
68+
/**
69+
* Returns a string representation of the book in the format: "title by author".
70+
*
71+
* @return A string representation of the book.
72+
*/
73+
@Override
74+
public String toString() {
75+
return title + " by " + author;
76+
}
77+
78+
/**
79+
* Checks if two libraries are equal by comparing their books and patrons collections.
80+
*
81+
* @param obj The object to compare with this library.
82+
* @return True if the libraries are equal, false otherwise.
83+
*/
84+
@Override
85+
public boolean equals(Object obj) {
86+
if (this == obj) return true;
87+
if (obj == null || getClass() != obj.getClass()) return false;
88+
Book book = (Book) obj;
89+
return Objects.equals(ISBN, book.ISBN);
90+
}
91+
92+
/**
93+
* Generates a hash code value for the library based on its books and patrons collections.
94+
*
95+
* @return The hash code value for the library.
96+
*/
97+
@Override
98+
public int hashCode() {
99+
return Objects.hash(ISBN);
100+
}
101+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.codedifferently.lesson9.chukwumaibezim;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.Objects;
6+
7+
/** Library class */
8+
public class Library {
9+
private HashSet<Book> books;
10+
private ArrayList<Patron> patrons;
11+
12+
/** Constructor for Library */
13+
public Library() {
14+
this.books = new HashSet<>();
15+
this.patrons = new ArrayList<>();
16+
}
17+
18+
/**
19+
* Getters and Setters
20+
*
21+
* @return
22+
*/
23+
public void addBook(Book book) {
24+
books.add(book);
25+
}
26+
27+
/**
28+
* Remove book from library
29+
*
30+
* @param book
31+
*/
32+
public void removeBook(Book book) {
33+
books.remove(book);
34+
}
35+
36+
/**
37+
* Add patron to library
38+
*
39+
* @param patron
40+
*/
41+
public void addPatron(Patron patron) {
42+
patrons.add(patron);
43+
}
44+
45+
/**
46+
* Checks to see if book is in library, if so, checks out book
47+
*
48+
* @param book
49+
* @param patron
50+
*/
51+
public void checkOutBook(Book book, Patron patron) {
52+
if (books.contains(book) && !book.isCheckedOut()) {
53+
patron.performCheckout(book);
54+
book.checkOut();
55+
}
56+
}
57+
58+
/**
59+
* Checks to see if book is in library, if so, checks in book
60+
*
61+
* @param book
62+
* @param patron
63+
*/
64+
public void returnBook(Book book, Patron patron) {
65+
if (patron.getCheckedOutBooks().contains(book)) {
66+
patron.performCheckIn(book);
67+
book.checkIn();
68+
}
69+
}
70+
71+
/**
72+
* Checks if two libraries are equal by comparing their books and patrons collections.
73+
*
74+
* @param obj The object to compare with this library.
75+
* @return True if the libraries are equal, false otherwise.
76+
*/
77+
@Override
78+
public boolean equals(Object obj) {
79+
if (this == obj) return true;
80+
if (obj == null || getClass() != obj.getClass()) return false;
81+
Library library = (Library) obj;
82+
return Objects.equals(books, library.books) && Objects.equals(patrons, library.patrons);
83+
}
84+
85+
/**
86+
* Generates a hash code value for the library based on its books and patrons collections.
87+
*
88+
* @return The hash code value for the library.
89+
*/
90+
@Override
91+
public int hashCode() {
92+
return Objects.hash(books, patrons);
93+
}
94+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.codedifferently.lesson9.chukwumaibezim;
2+
3+
import java.util.HashSet;
4+
import java.util.Objects;
5+
6+
/** Patron class */
7+
public class Patron {
8+
private String name;
9+
private HashSet<Book> checkedOutBooks;
10+
11+
/**
12+
* Constructor for Patron
13+
*
14+
* @param name
15+
*/
16+
public Patron(String name) {
17+
this.name = name;
18+
this.checkedOutBooks = new HashSet<>();
19+
}
20+
21+
/**
22+
* Getters and Setters
23+
*
24+
* @return
25+
*/
26+
public String getName() {
27+
return name;
28+
}
29+
30+
/**
31+
* Check out book private method for Patron only
32+
*
33+
* @param book
34+
*/
35+
private void checkOutBook(Book book) {
36+
checkedOutBooks.add(book);
37+
}
38+
39+
/**
40+
* Check in book private method for Patron only
41+
*
42+
* @param book
43+
*/
44+
private void checkInBook(Book book) {
45+
checkedOutBooks.remove(book);
46+
}
47+
48+
/**
49+
* Perform checkout public method for Library
50+
*
51+
* @param book
52+
*/
53+
public void performCheckout(Book book) {
54+
checkOutBook(book);
55+
}
56+
57+
/**
58+
* Perform check-in public method for Library
59+
*
60+
* @param book
61+
*/
62+
public void performCheckIn(Book book) {
63+
checkInBook(book);
64+
}
65+
66+
/**
67+
* Get checked out books
68+
*
69+
* @return
70+
*/
71+
public HashSet<Book> getCheckedOutBooks() {
72+
return checkedOutBooks;
73+
}
74+
75+
/**
76+
* Checks if two libraries are equal by comparing their books and patrons collections.
77+
*
78+
* @param obj The object to compare with this library.
79+
* @return True if the libraries are equal, false otherwise.
80+
*/
81+
@Override
82+
public boolean equals(Object o) {
83+
if (this == o) return true;
84+
if (o == null || getClass() != o.getClass()) return false;
85+
Patron patron = (Patron) o;
86+
return Objects.equals(name, patron.name)
87+
&& Objects.equals(checkedOutBooks, patron.checkedOutBooks);
88+
}
89+
90+
/**
91+
* Generates a hash code value for the library based on its books and patrons collections.
92+
*
93+
* @return The hash code value for the library.
94+
*/
95+
@Override
96+
public int hashCode() {
97+
return Objects.hash(name, checkedOutBooks);
98+
}
99+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.codedifferently.lesson9.chukwumibezim;
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.assertTrue;
6+
7+
import com.codedifferently.lesson9.chukwumaibezim.Book;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class BookTest {
11+
@Test
12+
public void testGetTitle() {
13+
Book book = new Book("To Kill a Mockingbird", "9780061120084", "Harper Lee", 336);
14+
assertEquals("To Kill a Mockingbird", book.getTitle());
15+
}
16+
17+
@Test
18+
public void testGetISBN() {
19+
Book book =
20+
new Book(
21+
"Monster: The Autobiography of an L.A. Gang Member",
22+
"9780802141446",
23+
"Sanyika Shakur",
24+
400);
25+
assertEquals("9780802141446", book.getISBN());
26+
}
27+
28+
@Test
29+
public void testGetAuthor() {
30+
Book book = new Book("The Art of War", "9780143037521", "Sun Tzu", 288);
31+
assertEquals("Sun Tzu", book.getAuthor());
32+
}
33+
34+
@Test
35+
public void testGetNumberOfPages() {
36+
Book book = new Book("Stormbreaker (Alex Rider)", "9780142406113", "Anthony Horowitz", 240);
37+
assertEquals(240, book.getNumberOfPages());
38+
}
39+
40+
@Test
41+
public void testIsCheckedOut() {
42+
Book book = new Book("Homeboyz", "9780439933497", "Alan Lawrence Sitomer", 256);
43+
assertFalse(book.isCheckedOut());
44+
}
45+
46+
@Test
47+
public void testCheckOut() {
48+
Book book = new Book("Tyrell", "9780439838808", "Coe Booth", 320);
49+
book.checkOut();
50+
assertTrue(book.isCheckedOut());
51+
}
52+
53+
@Test
54+
public void testCheckIn() {
55+
Book book = new Book("The Art of War", "9780143037521", "Sun Tzu", 288);
56+
book.checkOut();
57+
book.checkIn();
58+
assertFalse(book.isCheckedOut());
59+
}
60+
}

0 commit comments

Comments
 (0)