Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <anthonydmays@users.noreply.github.com>
- Loading branch information
1 parent
4027724
commit 65248ba
Showing
6 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/kevinmason/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/kevinmason/Library.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/kevinmason/Patron.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/kevinmason/BookTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/kevinmason/LibraryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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... | ||
} |
28 changes: 28 additions & 0 deletions
28
lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/kevinmason/PatronTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |