-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds Nataya's test files for book, patron, library and an excep…
…tion (#264) * feat: Created test files for book, patron, library, and an exception * added constructors to both book files * feat: Created book, library, and patron files
- Loading branch information
Showing
6 changed files
with
317 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/natayaprice/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,72 @@ | ||
package com.codedifferently.lesson9.natayaprice; | ||
|
||
public class Book { | ||
|
||
private String Titles; | ||
private int ISBN; | ||
private String Authors; | ||
private int TotalPages; | ||
private boolean CheckedOut; | ||
|
||
/** | ||
* Constructs a new Book object. | ||
* | ||
* @param Titles the title of the book | ||
* @param ISBN the ISBN number of the book | ||
* @param Authors the author(s) of the book | ||
* @param TotalPages the total number of pages in the book | ||
* @param CheckedOut indicates whether the book is currently checked out | ||
*/ | ||
public Book(String Titles, int ISBN, String Authors, int TotalPages, boolean CheckedOut) { | ||
this.Titles = Titles; | ||
this.ISBN = ISBN; | ||
this.Authors = Authors; | ||
this.TotalPages = TotalPages; | ||
this.CheckedOut = CheckedOut; | ||
} | ||
|
||
/** | ||
* Gets the title of the book. | ||
* | ||
* @return the title of the book | ||
*/ | ||
public String getTitles() { | ||
return Titles; | ||
} | ||
|
||
/** | ||
* Gets the ISBN number of the book. | ||
* | ||
* @return the ISBN number of the book | ||
*/ | ||
public int getISBN() { | ||
return ISBN; | ||
} | ||
|
||
/** | ||
* Gets the author(s) of the book. | ||
* | ||
* @return the author(s) of the book | ||
*/ | ||
public String getAuthors() { | ||
return Authors; | ||
} | ||
|
||
/** | ||
* Gets the total number of pages in the book. | ||
* | ||
* @return the total number of pages in the book | ||
*/ | ||
public int getTotalPages() { | ||
return TotalPages; | ||
} | ||
|
||
/** | ||
* Checks if the book is currently checked out. | ||
* | ||
* @return true if the book is checked out, otherwise false | ||
*/ | ||
public boolean getCheckedOut() { | ||
return CheckedOut; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/natayaprice/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,31 @@ | ||
package com.codedifferently.lesson9.natayaprice; | ||
|
||
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 registerPatron(Patron patron) { | ||
patrons.add(patron); | ||
} | ||
|
||
public List<Patron> getPatrons() { | ||
return patrons; | ||
} | ||
|
||
public void addBook(Book book) { | ||
books.add(book); | ||
} | ||
|
||
public List<Book> getBooks() { | ||
return books; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/natayaprice/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,75 @@ | ||
package com.codedifferently.lesson9.natayaprice; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Patron { | ||
|
||
/** | ||
* Constructs a new Patron with the specified name and email. | ||
* | ||
* @param name the name of the patron | ||
* @param email the email of the patron | ||
*/ | ||
public Patron(String string, String string2) { | ||
// TODO Auto-generated constructor stub | ||
} | ||
|
||
/** | ||
* The main method to execute tests for Patron functionalities. | ||
* | ||
* @param args the command line arguments | ||
*/ | ||
public static void main(String[] args) { | ||
testRegisterPatron(); | ||
testPatronBooksCheckedOutMap(); | ||
} | ||
|
||
/** Tests the registration of a patron with the library. */ | ||
public static void testRegisterPatron() { | ||
Patron patron = new Patron("John Doe", "[email protected]"); | ||
Library library = new Library(); | ||
library.registerPatron(patron); | ||
System.out.println( | ||
library.getPatrons().contains(patron) | ||
? "Patron should be registered with the library" | ||
: "Patron is not registered with the library"); | ||
} | ||
|
||
/** Tests the patron-books checked out map. */ | ||
public static void testPatronBooksCheckedOutMap() { | ||
Map<String, Integer> patronBooksCheckedOutMap = new HashMap<>(); | ||
patronBooksCheckedOutMap.put("John Doe", 5); | ||
patronBooksCheckedOutMap.put("Jane Smith", 3); | ||
patronBooksCheckedOutMap.put("Alice Johnson", 0); | ||
|
||
System.out.println( | ||
patronBooksCheckedOutMap.get("John Doe") == 5 | ||
? "John Doe should have 5 books checked out" | ||
: "Incorrect number of books checked out for John Doe"); | ||
System.out.println( | ||
patronBooksCheckedOutMap.get("Jane Smith") == 3 | ||
? "Jane Smith should have 3 books checked out" | ||
: "Incorrect number of books checked out for Jane Smith"); | ||
System.out.println( | ||
patronBooksCheckedOutMap.get("Alice Johnson") == 0 | ||
? "Alice Johnson should have 0 books checked out" | ||
: "Incorrect number of books checked out for Alice Johnson"); | ||
|
||
System.out.println( | ||
patronBooksCheckedOutMap.size() == 3 | ||
? "Size of the map is correct" | ||
: "Incorrect size of the map"); | ||
|
||
patronBooksCheckedOutMap.put("John Doe", 8); | ||
System.out.println( | ||
patronBooksCheckedOutMap.get("John Doe") == 8 | ||
? "John Doe now has 8 books checked out" | ||
: "Incorrect number of books checked out for John Doe"); | ||
|
||
System.out.println( | ||
patronBooksCheckedOutMap.get("Nonexistent Patron") == null | ||
? "Nonexistent Patron is not found in the map" | ||
: "Nonexistent Patron is found in the map"); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/natayaprice/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,72 @@ | ||
package com.codedifferently.lesson9.natayaprice; | ||
|
||
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 org.junit.jupiter.api.Test; | ||
|
||
public class BookTest { | ||
|
||
/** Tests the constructor of the {@link Book} class. */ | ||
@Test | ||
public void testConstructor() { | ||
Book book = new Book("Title", 1234567890, "Author", 200, false); | ||
assertEquals("Title", book.getTitles()); | ||
assertEquals(1234567890, book.getISBN()); | ||
assertEquals("Author", book.getAuthors()); | ||
assertEquals(200, book.getTotalPages()); | ||
assertFalse(book.getCheckedOut()); | ||
} | ||
|
||
/** Tests the {@code getTitles()} method of the {@link Book} class. */ | ||
@Test | ||
public void testGetTitle() { | ||
Book book = new Book("Children of Blood and Bone", 972, "Tomi Adeyemi", 544, false); | ||
assertEquals("Children of Blood and Bone", book.getTitles()); | ||
} | ||
|
||
/** Tests the {@code getISBN()} method of the {@link Book} class. */ | ||
@Test | ||
public void testGetISBN() { | ||
Book book = | ||
new Book("The Sisters Grimm:The Fairy-Tale Detectives", 571, "Michael Buckley", 176, true); | ||
assertEquals(571, book.getISBN()); | ||
} | ||
|
||
/** Tests the {@code getAuthors()} method of the {@link Book} class. */ | ||
@Test | ||
public void testGetAuthors() { | ||
Book book = new Book("A Light in the Attic", 739, "Shel Silverstein", 176, true); | ||
assertEquals("Shel Silverstein", book.getAuthors()); | ||
} | ||
|
||
/** Tests the {@code getTotalPages()} method of the {@link Book} class. */ | ||
@Test | ||
public void testGetTotalPages() { | ||
Book book = | ||
new Book("Junie B. Jones and the Mushy Gushy Valentine", 408, "Barbara Park", 80, true); | ||
assertEquals(80, book.getTotalPages()); | ||
} | ||
|
||
/** Tests the {@code getCheckedOut()} method of the {@link Book} class. */ | ||
@Test | ||
public void testGetCheckedOut() { | ||
Book book = | ||
new Book("Thirst: Human Urges, Fatal Consequences", 060, "Michael Farquhar", 384, false); | ||
assertFalse(book.getCheckedOut()); | ||
} | ||
|
||
@Test | ||
public void testIsCheckedOut() { | ||
Book book = new Book("Women Who Run with the Wolves", 874, "Clarissa Pinkola Estés", 560, true); | ||
assertTrue(book.getCheckedOut()); | ||
} | ||
|
||
/** Tests the {@code getCheckedIn()} method of the {@link Book} class. */ | ||
@Test | ||
public void testGetCheckedIn() { | ||
Book book = new Book("Beloved", 416, "Toni Morrison", 324, false); | ||
assertFalse(book.getCheckedOut()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/natayaprice/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,24 @@ | ||
package com.codedifferently.lesson9.natayaprice; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class LibraryTest { | ||
|
||
@Test | ||
public void testAddBook() { | ||
Library library = new Library(); | ||
Book book = | ||
new Book( | ||
"The Sisters Grimm: The Fairy-Tale Detectives", 571, "Michael Buckley", 176, false); | ||
library.addBook(book); | ||
List<Book> booksInLibrary = library.getBooks(); | ||
// Assert that one book should be added to the library | ||
assertEquals(1, booksInLibrary.size(), "One book should be added to the library"); | ||
// Assert that the added book is the same as the book in the library | ||
assertEquals( | ||
book, booksInLibrary.get(0), "Added book should be the same as the book in the library"); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/natayaprice/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,43 @@ | ||
package com.codedifferently.lesson9.natayaprice; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class PatronTest { | ||
|
||
/** Tests the registration of a patron with the library. */ | ||
@Test | ||
public void testRegisterPatron() { | ||
Patron patron = new Patron("John Doe", "[email protected]"); | ||
Library library = new Library(); | ||
library.registerPatron(patron); | ||
assertTrue( | ||
library.getPatrons().contains(patron), "Patron should be registered with the library"); | ||
} | ||
|
||
/** Tests the patron-books checked out map. */ | ||
@Test | ||
public void testPatronBooksCheckedOutMap() { | ||
|
||
Map<String, Integer> patronBooksCheckedOutMap = new HashMap<>(); | ||
patronBooksCheckedOutMap.put("John Doe", 5); | ||
patronBooksCheckedOutMap.put("Jane Smith", 3); | ||
patronBooksCheckedOutMap.put("Alice Johnson", 0); | ||
|
||
assertEquals(5, patronBooksCheckedOutMap.get("John Doe").intValue()); | ||
assertEquals(3, patronBooksCheckedOutMap.get("Jane Smith").intValue()); | ||
assertEquals(0, patronBooksCheckedOutMap.get("Alice Johnson").intValue()); | ||
|
||
assertEquals(3, patronBooksCheckedOutMap.size()); | ||
|
||
patronBooksCheckedOutMap.put("John Doe", 8); | ||
assertEquals(8, patronBooksCheckedOutMap.get("John Doe").intValue()); | ||
|
||
assertNull(patronBooksCheckedOutMap.get("Nonexistent Patron")); | ||
} | ||
} |