Skip to content

Commit 65248ba

Browse files
feat: adds Library class and corresponding tests (#260)
* feat - finished test for lesson 10 added answers in terminal * feat: Add Book, Patron, & Library, class and corresponding tests * fix: added Library file * chore: removes misplaced quiz file. --------- Co-authored-by: Anthony D. Mays <[email protected]>
1 parent 4027724 commit 65248ba

File tree

6 files changed

+187
-0
lines changed

6 files changed

+187
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.codedifferently.lesson9.kevinmason;
2+
3+
public class Book {
4+
private String title;
5+
private String isbn;
6+
private String author;
7+
private int numberOfPages;
8+
private boolean checkedOut;
9+
10+
public Book(String title, String isbn, String author, int numberOfPages) {
11+
this.title = title;
12+
this.isbn = isbn;
13+
this.author = author;
14+
this.numberOfPages = numberOfPages;
15+
this.checkedOut = false;
16+
}
17+
18+
public String getTitle() {
19+
return title;
20+
}
21+
22+
public String getIsbn() {
23+
return isbn;
24+
}
25+
26+
public String getAuthor() {
27+
return author;
28+
}
29+
30+
public int getNumberOfPages() {
31+
return numberOfPages;
32+
}
33+
34+
public boolean isCheckedOut() {
35+
return checkedOut;
36+
}
37+
38+
public void setCheckedOut(boolean checkedOut) {
39+
this.checkedOut = checkedOut;
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.codedifferently.lesson9.kevinmason;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Library {
7+
private List<Book> books;
8+
private List<Patron> patrons;
9+
10+
public Library() {
11+
this.books = new ArrayList<>();
12+
this.patrons = new ArrayList<>();
13+
}
14+
15+
public void addBook(Book book) {
16+
books.add(book);
17+
}
18+
19+
public void removeBook(Book book) {
20+
books.remove(book);
21+
}
22+
23+
public void registerPatron(Patron patron) {
24+
patrons.add(patron);
25+
}
26+
27+
public void checkOutBook(Book book, Patron patron) {
28+
if (!book.isCheckedOut()) {
29+
book.setCheckedOut(true);
30+
patron.addCheckedOutBook(book);
31+
}
32+
}
33+
34+
public void returnBook(Book book, Patron patron) {
35+
book.setCheckedOut(false);
36+
patron.removeCheckedOutBook(book);
37+
}
38+
39+
public List<Book> getBooks() {
40+
return books;
41+
}
42+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.codedifferently.lesson9.kevinmason;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Patron {
7+
private String name;
8+
private List<Book> checkedOutBooks;
9+
10+
public Patron(String name) {
11+
this.name = name;
12+
this.checkedOutBooks = new ArrayList<>();
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public List<Book> getCheckedOutBooks() {
20+
return checkedOutBooks;
21+
}
22+
23+
public void addCheckedOutBook(Book book) {
24+
checkedOutBooks.add(book);
25+
}
26+
27+
public void removeCheckedOutBook(Book book) {
28+
checkedOutBooks.remove(book);
29+
}
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codedifferently.lesson9.kevinmason;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class BookTest {
8+
9+
@Test
10+
public void testCheckOut() {
11+
Book book = new Book("Title", "ISBN123", "Author", 200);
12+
assertFalse(book.isCheckedOut());
13+
14+
book.setCheckedOut(true);
15+
assertTrue(book.isCheckedOut());
16+
}
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.codedifferently.lesson9.kevinmason;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.List;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class LibraryTest {
9+
10+
@Test
11+
public void testAddBook() {
12+
// Create a library
13+
Library library = new Library();
14+
15+
// Create a book
16+
Book book = new Book("Test Book", "ISBN123", "Test Author", 200);
17+
18+
// Add the book to the library
19+
library.addBook(book);
20+
21+
// Retrieve the list of books from the library
22+
List<Book> books = library.getBooks();
23+
24+
// Verify that the book was added to the library
25+
assertTrue(books.contains(book));
26+
}
27+
28+
// You can add more test methods for other functionalities here...
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.codedifferently.lesson9.kevinmason;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import static org.junit.jupiter.api.Assertions.assertTrue; // Import statically assertTrue method
5+
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class PatronTest {
10+
11+
@Test
12+
public void testAddCheckedOutBook() {
13+
// Create a patron
14+
Patron patron = new Patron("John Doe");
15+
16+
// Create a book
17+
Book book = new Book("Test Book", "ISBN123", "Test Author", 200);
18+
19+
// Add the book to the patron's checked out books
20+
patron.addCheckedOutBook(book);
21+
22+
// Retrieve the list of checked out books
23+
List<Book> checkedOutBooks = patron.getCheckedOutBooks();
24+
25+
// Verify that the book was added to the list of checked out books
26+
assertTrue(checkedOutBooks.contains(book));
27+
}
28+
}

0 commit comments

Comments
 (0)