|
| 1 | +package com.codedifferently.lesson9.sherllin; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +// We want to define a book and keep track of its title, isbn, author(s), number of pages, and |
| 10 | +// whether it is checked out or not. |
| 11 | +// We want define a patron with properties including their name and the books they have checked out. |
| 12 | +// We want to define a library that: |
| 13 | +// Allows us to add/remove a book to and from its collection of books. |
| 14 | +// Allows us to register new patrons |
| 15 | +// Allows us to check out books to patrons. |
| 16 | +// Allows us to return books from patrons. |
| 17 | + |
| 18 | +public class BookTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testBookProperties() { |
| 22 | + // Arrange |
| 23 | + String title = "The Alchemist"; |
| 24 | + long isbn = 9780061122415L; |
| 25 | + String author = "Paulo Coelho"; |
| 26 | + int pages = 208; |
| 27 | + boolean checkedOut = false; |
| 28 | + |
| 29 | + // Act |
| 30 | + Book theAlchemist = new Book(title, isbn, author, pages, checkedOut); |
| 31 | + |
| 32 | + // Assert |
| 33 | + assertEquals(title, theAlchemist.getBookTitle()); |
| 34 | + assertEquals(isbn, theAlchemist.getIsbn()); |
| 35 | + assertEquals(author, theAlchemist.getAuthor()); |
| 36 | + assertEquals(pages, theAlchemist.getPages()); |
| 37 | + assertEquals(checkedOut, theAlchemist.checkedOut()); |
| 38 | + |
| 39 | + // Test additional books |
| 40 | + Book theCatInTheHat = new Book(title, isbn, author, pages, checkedOut); |
| 41 | + assertEquals(title, theCatInTheHat.getBookTitle()); |
| 42 | + assertEquals(isbn, theCatInTheHat.getIsbn()); |
| 43 | + assertEquals(author, theCatInTheHat.getAuthor()); |
| 44 | + assertEquals(pages, theCatInTheHat.getPages()); |
| 45 | + assertEquals(checkedOut, theCatInTheHat.checkedOut()); |
| 46 | + |
| 47 | + Book atomicHabits = new Book(title, isbn, author, pages, checkedOut); |
| 48 | + assertEquals(title, atomicHabits.getBookTitle()); |
| 49 | + assertEquals(isbn, atomicHabits.getIsbn()); |
| 50 | + assertEquals(author, atomicHabits.getAuthor()); |
| 51 | + assertEquals(pages, atomicHabits.getPages()); |
| 52 | + assertEquals(checkedOut, atomicHabits.checkedOut()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testPatronProperties() { |
| 57 | + Patron patron = new Patron("Nathalie", 3); |
| 58 | + String name = "Nathalie"; |
| 59 | + int bookCheckedOut = 3; |
| 60 | + assertEquals(name, patron.getName()); |
| 61 | + assertEquals(bookCheckedOut, patron.getBookCheckedOut()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testAddBook() { |
| 66 | + Library library = new Library(); |
| 67 | + library.addBook(9780316769488L); |
| 68 | + library.addBook(9780142437209L); |
| 69 | + library.addBook(9780743273565L); |
| 70 | + |
| 71 | + assertEquals(3, library.getBookStatus().size()); |
| 72 | + assertEquals(Boolean.FALSE, library.getBookStatus().get(9780316769488L)); |
| 73 | + assertEquals(Boolean.FALSE, library.getBookStatus().get(9780142437209L)); |
| 74 | + assertEquals(Boolean.FALSE, library.getBookStatus().get(9780743273565L)); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testRemoveBook() { |
| 79 | + Library library = new Library(); |
| 80 | + library.addBook(9780316769488L); |
| 81 | + library.addBook(9780142437209L); |
| 82 | + library.removeBook(9780316769488L); |
| 83 | + |
| 84 | + Map<Long, Boolean> expectedBookStatus = new HashMap<>(); |
| 85 | + expectedBookStatus.put(9780142437209L, false); |
| 86 | + |
| 87 | + assertEquals(expectedBookStatus, library.getBookStatus()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testRegisterPatron() { |
| 92 | + Library library = new Library(); |
| 93 | + library.registerPatron("Lily"); |
| 94 | + library.registerPatron("Dali"); |
| 95 | + |
| 96 | + assertEquals(2, library.getPatronBooks().size()); |
| 97 | + assertEquals(Integer.valueOf(0), library.getPatronBooks().get("Lily")); |
| 98 | + assertEquals(Integer.valueOf(0), library.getPatronBooks().get("Dali")); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testCheckoutAndReturnBook() { |
| 103 | + Library library = new Library(); |
| 104 | + library.addBook(9780316769488L); |
| 105 | + library.registerPatron("Lily"); |
| 106 | + library.registerPatron("Dali"); |
| 107 | + |
| 108 | + library.checkoutOrReturnBook(9780316769488L, "Lily", true); |
| 109 | + library.checkoutOrReturnBook(9780316769488L, "Dali", true); |
| 110 | + library.checkoutOrReturnBook(9780316769488L, "Dali", true); |
| 111 | + |
| 112 | + assertEquals(Boolean.TRUE, library.getBookStatus().get(9780316769488L)); |
| 113 | + assertEquals(Integer.valueOf(1), library.getPatronBooks().get("Lily")); |
| 114 | + assertEquals(Integer.valueOf(0), library.getPatronBooks().get("Dali")); |
| 115 | + |
| 116 | + library.checkoutOrReturnBook(9780316769488L, "Dali", false); |
| 117 | + library.checkoutOrReturnBook(9780316769488L, "Lily", false); |
| 118 | + |
| 119 | + assertEquals(Boolean.FALSE, library.getBookStatus().get(9780316769488L)); |
| 120 | + assertEquals(Integer.valueOf(0), library.getPatronBooks().get("Lily")); |
| 121 | + assertEquals(Integer.valueOf(0), library.getPatronBooks().get("Dali")); |
| 122 | + } |
| 123 | +} |
0 commit comments