Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Created test files for book, patron, library and an exception #264

Merged
merged 8 commits into from
Mar 22, 2024
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;
anthonydmays marked this conversation as resolved.
Show resolved Hide resolved
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;
anthonydmays marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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;
}
}
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 {
anthonydmays marked this conversation as resolved.
Show resolved Hide resolved

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;
}
}
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) {
anthonydmays marked this conversation as resolved.
Show resolved Hide resolved
// 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) {
anthonydmays marked this conversation as resolved.
Show resolved Hide resolved
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");
}
}
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());
}
}
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");
}
}
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"));
}
}