Skip to content

Commit 16a282c

Browse files
authored
feat: adds Nataya's test files for book, patron, library and an exception (code-differently#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
1 parent 1cf1a9f commit 16a282c

File tree

6 files changed

+317
-0
lines changed

6 files changed

+317
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.codedifferently.lesson9.natayaprice;
2+
3+
public class Book {
4+
5+
private String Titles;
6+
private int ISBN;
7+
private String Authors;
8+
private int TotalPages;
9+
private boolean CheckedOut;
10+
11+
/**
12+
* Constructs a new Book object.
13+
*
14+
* @param Titles the title of the book
15+
* @param ISBN the ISBN number of the book
16+
* @param Authors the author(s) of the book
17+
* @param TotalPages the total number of pages in the book
18+
* @param CheckedOut indicates whether the book is currently checked out
19+
*/
20+
public Book(String Titles, int ISBN, String Authors, int TotalPages, boolean CheckedOut) {
21+
this.Titles = Titles;
22+
this.ISBN = ISBN;
23+
this.Authors = Authors;
24+
this.TotalPages = TotalPages;
25+
this.CheckedOut = CheckedOut;
26+
}
27+
28+
/**
29+
* Gets the title of the book.
30+
*
31+
* @return the title of the book
32+
*/
33+
public String getTitles() {
34+
return Titles;
35+
}
36+
37+
/**
38+
* Gets the ISBN number of the book.
39+
*
40+
* @return the ISBN number of the book
41+
*/
42+
public int getISBN() {
43+
return ISBN;
44+
}
45+
46+
/**
47+
* Gets the author(s) of the book.
48+
*
49+
* @return the author(s) of the book
50+
*/
51+
public String getAuthors() {
52+
return Authors;
53+
}
54+
55+
/**
56+
* Gets the total number of pages in the book.
57+
*
58+
* @return the total number of pages in the book
59+
*/
60+
public int getTotalPages() {
61+
return TotalPages;
62+
}
63+
64+
/**
65+
* Checks if the book is currently checked out.
66+
*
67+
* @return true if the book is checked out, otherwise false
68+
*/
69+
public boolean getCheckedOut() {
70+
return CheckedOut;
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.codedifferently.lesson9.natayaprice;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Library {
7+
8+
private List<Book> books;
9+
private List<Patron> patrons;
10+
11+
public Library() {
12+
this.books = new ArrayList<>();
13+
this.patrons = new ArrayList<>();
14+
}
15+
16+
public void registerPatron(Patron patron) {
17+
patrons.add(patron);
18+
}
19+
20+
public List<Patron> getPatrons() {
21+
return patrons;
22+
}
23+
24+
public void addBook(Book book) {
25+
books.add(book);
26+
}
27+
28+
public List<Book> getBooks() {
29+
return books;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.codedifferently.lesson9.natayaprice;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class Patron {
7+
8+
/**
9+
* Constructs a new Patron with the specified name and email.
10+
*
11+
* @param name the name of the patron
12+
* @param email the email of the patron
13+
*/
14+
public Patron(String string, String string2) {
15+
// TODO Auto-generated constructor stub
16+
}
17+
18+
/**
19+
* The main method to execute tests for Patron functionalities.
20+
*
21+
* @param args the command line arguments
22+
*/
23+
public static void main(String[] args) {
24+
testRegisterPatron();
25+
testPatronBooksCheckedOutMap();
26+
}
27+
28+
/** Tests the registration of a patron with the library. */
29+
public static void testRegisterPatron() {
30+
Patron patron = new Patron("John Doe", "[email protected]");
31+
Library library = new Library();
32+
library.registerPatron(patron);
33+
System.out.println(
34+
library.getPatrons().contains(patron)
35+
? "Patron should be registered with the library"
36+
: "Patron is not registered with the library");
37+
}
38+
39+
/** Tests the patron-books checked out map. */
40+
public static void testPatronBooksCheckedOutMap() {
41+
Map<String, Integer> patronBooksCheckedOutMap = new HashMap<>();
42+
patronBooksCheckedOutMap.put("John Doe", 5);
43+
patronBooksCheckedOutMap.put("Jane Smith", 3);
44+
patronBooksCheckedOutMap.put("Alice Johnson", 0);
45+
46+
System.out.println(
47+
patronBooksCheckedOutMap.get("John Doe") == 5
48+
? "John Doe should have 5 books checked out"
49+
: "Incorrect number of books checked out for John Doe");
50+
System.out.println(
51+
patronBooksCheckedOutMap.get("Jane Smith") == 3
52+
? "Jane Smith should have 3 books checked out"
53+
: "Incorrect number of books checked out for Jane Smith");
54+
System.out.println(
55+
patronBooksCheckedOutMap.get("Alice Johnson") == 0
56+
? "Alice Johnson should have 0 books checked out"
57+
: "Incorrect number of books checked out for Alice Johnson");
58+
59+
System.out.println(
60+
patronBooksCheckedOutMap.size() == 3
61+
? "Size of the map is correct"
62+
: "Incorrect size of the map");
63+
64+
patronBooksCheckedOutMap.put("John Doe", 8);
65+
System.out.println(
66+
patronBooksCheckedOutMap.get("John Doe") == 8
67+
? "John Doe now has 8 books checked out"
68+
: "Incorrect number of books checked out for John Doe");
69+
70+
System.out.println(
71+
patronBooksCheckedOutMap.get("Nonexistent Patron") == null
72+
? "Nonexistent Patron is not found in the map"
73+
: "Nonexistent Patron is found in the map");
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.codedifferently.lesson9.natayaprice;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
public class BookTest {
10+
11+
/** Tests the constructor of the {@link Book} class. */
12+
@Test
13+
public void testConstructor() {
14+
Book book = new Book("Title", 1234567890, "Author", 200, false);
15+
assertEquals("Title", book.getTitles());
16+
assertEquals(1234567890, book.getISBN());
17+
assertEquals("Author", book.getAuthors());
18+
assertEquals(200, book.getTotalPages());
19+
assertFalse(book.getCheckedOut());
20+
}
21+
22+
/** Tests the {@code getTitles()} method of the {@link Book} class. */
23+
@Test
24+
public void testGetTitle() {
25+
Book book = new Book("Children of Blood and Bone", 972, "Tomi Adeyemi", 544, false);
26+
assertEquals("Children of Blood and Bone", book.getTitles());
27+
}
28+
29+
/** Tests the {@code getISBN()} method of the {@link Book} class. */
30+
@Test
31+
public void testGetISBN() {
32+
Book book =
33+
new Book("The Sisters Grimm:The Fairy-Tale Detectives", 571, "Michael Buckley", 176, true);
34+
assertEquals(571, book.getISBN());
35+
}
36+
37+
/** Tests the {@code getAuthors()} method of the {@link Book} class. */
38+
@Test
39+
public void testGetAuthors() {
40+
Book book = new Book("A Light in the Attic", 739, "Shel Silverstein", 176, true);
41+
assertEquals("Shel Silverstein", book.getAuthors());
42+
}
43+
44+
/** Tests the {@code getTotalPages()} method of the {@link Book} class. */
45+
@Test
46+
public void testGetTotalPages() {
47+
Book book =
48+
new Book("Junie B. Jones and the Mushy Gushy Valentine", 408, "Barbara Park", 80, true);
49+
assertEquals(80, book.getTotalPages());
50+
}
51+
52+
/** Tests the {@code getCheckedOut()} method of the {@link Book} class. */
53+
@Test
54+
public void testGetCheckedOut() {
55+
Book book =
56+
new Book("Thirst: Human Urges, Fatal Consequences", 060, "Michael Farquhar", 384, false);
57+
assertFalse(book.getCheckedOut());
58+
}
59+
60+
@Test
61+
public void testIsCheckedOut() {
62+
Book book = new Book("Women Who Run with the Wolves", 874, "Clarissa Pinkola Estés", 560, true);
63+
assertTrue(book.getCheckedOut());
64+
}
65+
66+
/** Tests the {@code getCheckedIn()} method of the {@link Book} class. */
67+
@Test
68+
public void testGetCheckedIn() {
69+
Book book = new Book("Beloved", 416, "Toni Morrison", 324, false);
70+
assertFalse(book.getCheckedOut());
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.codedifferently.lesson9.natayaprice;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
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+
Library library = new Library();
13+
Book book =
14+
new Book(
15+
"The Sisters Grimm: The Fairy-Tale Detectives", 571, "Michael Buckley", 176, false);
16+
library.addBook(book);
17+
List<Book> booksInLibrary = library.getBooks();
18+
// Assert that one book should be added to the library
19+
assertEquals(1, booksInLibrary.size(), "One book should be added to the library");
20+
// Assert that the added book is the same as the book in the library
21+
assertEquals(
22+
book, booksInLibrary.get(0), "Added book should be the same as the book in the library");
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.codedifferently.lesson9.natayaprice;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class PatronTest {
12+
13+
/** Tests the registration of a patron with the library. */
14+
@Test
15+
public void testRegisterPatron() {
16+
Patron patron = new Patron("John Doe", "[email protected]");
17+
Library library = new Library();
18+
library.registerPatron(patron);
19+
assertTrue(
20+
library.getPatrons().contains(patron), "Patron should be registered with the library");
21+
}
22+
23+
/** Tests the patron-books checked out map. */
24+
@Test
25+
public void testPatronBooksCheckedOutMap() {
26+
27+
Map<String, Integer> patronBooksCheckedOutMap = new HashMap<>();
28+
patronBooksCheckedOutMap.put("John Doe", 5);
29+
patronBooksCheckedOutMap.put("Jane Smith", 3);
30+
patronBooksCheckedOutMap.put("Alice Johnson", 0);
31+
32+
assertEquals(5, patronBooksCheckedOutMap.get("John Doe").intValue());
33+
assertEquals(3, patronBooksCheckedOutMap.get("Jane Smith").intValue());
34+
assertEquals(0, patronBooksCheckedOutMap.get("Alice Johnson").intValue());
35+
36+
assertEquals(3, patronBooksCheckedOutMap.size());
37+
38+
patronBooksCheckedOutMap.put("John Doe", 8);
39+
assertEquals(8, patronBooksCheckedOutMap.get("John Doe").intValue());
40+
41+
assertNull(patronBooksCheckedOutMap.get("Nonexistent Patron"));
42+
}
43+
}

0 commit comments

Comments
 (0)