Skip to content

Commit

Permalink
feat: adds Library class and corresponding tests (#260)
Browse files Browse the repository at this point in the history
* 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 <anthonydmays@users.noreply.github.com>
  • Loading branch information
kjknows and anthonydmays committed Mar 22, 2024
1 parent 4027724 commit 65248ba
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 0 deletions.
@@ -0,0 +1,41 @@
package com.codedifferently.lesson9.kevinmason;

public class Book {
private String title;
private String isbn;
private String author;
private int numberOfPages;
private boolean checkedOut;

public Book(String title, String isbn, String author, int numberOfPages) {
this.title = title;
this.isbn = isbn;
this.author = author;
this.numberOfPages = numberOfPages;
this.checkedOut = false;
}

public String getTitle() {
return title;
}

public String getIsbn() {
return isbn;
}

public String getAuthor() {
return author;
}

public int getNumberOfPages() {
return numberOfPages;
}

public boolean isCheckedOut() {
return checkedOut;
}

public void setCheckedOut(boolean checkedOut) {
this.checkedOut = checkedOut;
}
}
@@ -0,0 +1,42 @@
package com.codedifferently.lesson9.kevinmason;

import java.util.ArrayList;
import java.util.List;

public class Library {
private List<Book> books;
private List<Patron> patrons;

public Library() {
this.books = new ArrayList<>();
this.patrons = new ArrayList<>();
}

public void addBook(Book book) {
books.add(book);
}

public void removeBook(Book book) {
books.remove(book);
}

public void registerPatron(Patron patron) {
patrons.add(patron);
}

public void checkOutBook(Book book, Patron patron) {
if (!book.isCheckedOut()) {
book.setCheckedOut(true);
patron.addCheckedOutBook(book);
}
}

public void returnBook(Book book, Patron patron) {
book.setCheckedOut(false);
patron.removeCheckedOutBook(book);
}

public List<Book> getBooks() {
return books;
}
}
@@ -0,0 +1,30 @@
package com.codedifferently.lesson9.kevinmason;

import java.util.ArrayList;
import java.util.List;

public class Patron {
private String name;
private List<Book> checkedOutBooks;

public Patron(String name) {
this.name = name;
this.checkedOutBooks = new ArrayList<>();
}

public String getName() {
return name;
}

public List<Book> getCheckedOutBooks() {
return checkedOutBooks;
}

public void addCheckedOutBook(Book book) {
checkedOutBooks.add(book);
}

public void removeCheckedOutBook(Book book) {
checkedOutBooks.remove(book);
}
}
@@ -0,0 +1,17 @@
package com.codedifferently.lesson9.kevinmason;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class BookTest {

@Test
public void testCheckOut() {
Book book = new Book("Title", "ISBN123", "Author", 200);
assertFalse(book.isCheckedOut());

book.setCheckedOut(true);
assertTrue(book.isCheckedOut());
}
}
@@ -0,0 +1,29 @@
package com.codedifferently.lesson9.kevinmason;

import static org.junit.jupiter.api.Assertions.*;

import java.util.List;
import org.junit.jupiter.api.Test;

public class LibraryTest {

@Test
public void testAddBook() {
// Create a library
Library library = new Library();

// Create a book
Book book = new Book("Test Book", "ISBN123", "Test Author", 200);

// Add the book to the library
library.addBook(book);

// Retrieve the list of books from the library
List<Book> books = library.getBooks();

// Verify that the book was added to the library
assertTrue(books.contains(book));
}

// You can add more test methods for other functionalities here...
}
@@ -0,0 +1,28 @@
package com.codedifferently.lesson9.kevinmason;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue; // Import statically assertTrue method

import java.util.List;
import org.junit.jupiter.api.Test;

public class PatronTest {

@Test
public void testAddCheckedOutBook() {
// Create a patron
Patron patron = new Patron("John Doe");

// Create a book
Book book = new Book("Test Book", "ISBN123", "Test Author", 200);

// Add the book to the patron's checked out books
patron.addCheckedOutBook(book);

// Retrieve the list of checked out books
List<Book> checkedOutBooks = patron.getCheckedOutBooks();

// Verify that the book was added to the list of checked out books
assertTrue(checkedOutBooks.contains(book));
}
}

0 comments on commit 65248ba

Please sign in to comment.