-
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 Aaron's LibraryLog ,Book, and Guest with test blocks (#261)
- Loading branch information
Showing
6 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/AaronSantiago/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,31 @@ | ||
package com.codedifferently.lesson9.library; | ||
|
||
class Book { | ||
private String title; | ||
private String author; | ||
private String ISBN; | ||
|
||
public Book(String title, String author, String ISBN) { | ||
this.title = title; | ||
this.author = author; | ||
this.ISBN = ISBN; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public String getAuthor() { | ||
return author; | ||
} | ||
|
||
public String getISBN() { | ||
return ISBN; | ||
} | ||
|
||
public void displayBookDetails() { | ||
System.out.println("Title: " + title); | ||
System.out.println("Author: " + author); | ||
System.out.println("ISBN: " + ISBN); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...p/oop_app/src/main/java/com/codedifferently/lesson9/AaronSantiago/LibraryBuildingLog.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,69 @@ | ||
package com.codedifferently.lesson9.library; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class LibraryBuildingLog { | ||
private Map<String, Book> books; | ||
private List<LibraryGuest> guests; | ||
|
||
public LibraryBuildingLog() { | ||
this.books = new HashMap<>(); | ||
this.guests = new ArrayList<>(); | ||
} | ||
|
||
public void addBook(Book book) { | ||
books.put(book.getISBN(), book); | ||
} | ||
|
||
public boolean removeBook(String ISBN) { | ||
return books.remove(ISBN) != null; | ||
} | ||
|
||
public void registerGuest(LibraryGuest guest) { | ||
guests.add(guest); | ||
} | ||
|
||
public boolean checkOutBook(String ISBN, LibraryGuest guest) { | ||
Book book = books.get(ISBN); | ||
if (book != null && !isBookCheckedOut(book)) { | ||
guest.checkOutBook(book); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public boolean returnBook(String ISBN, LibraryGuest guest) { | ||
Book book = books.get(ISBN); | ||
if (book != null) { | ||
return guest.returnBook(book); | ||
} | ||
return false; | ||
} | ||
|
||
private boolean isBookCheckedOut(Book book) { | ||
for (LibraryGuest guest : guests) { | ||
if (guest.getCheckedOutBooks().contains(book)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public void displayLibraryInformation() { | ||
System.out.println("Library Books:"); | ||
books.values().forEach(Book::displayBookDetails); | ||
System.out.println("\nLibrary Guests:"); | ||
guests.forEach( | ||
guest -> { | ||
System.out.println("Name: " + guest.getName()); | ||
if (!guest.getCheckedOutBooks().isEmpty()) { | ||
guest.displayGuestDetails(); | ||
} else { | ||
System.out.println("No books checked out."); | ||
} | ||
}); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
..._09/oop/oop_app/src/main/java/com/codedifferently/lesson9/AaronSantiago/LibraryGuest.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.library; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
class LibraryGuest { | ||
private String name; | ||
private List<Book> checkedOutBooks; | ||
|
||
public LibraryGuest(String name) { | ||
this.name = name; | ||
this.checkedOutBooks = new ArrayList<>(); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void checkOutBook(Book book) { | ||
checkedOutBooks.add(book); | ||
} | ||
|
||
public boolean returnBook(Book book) { | ||
return checkedOutBooks.remove(book); | ||
} | ||
|
||
public List<Book> getCheckedOutBooks() { | ||
return checkedOutBooks; | ||
} | ||
|
||
public void displayGuestDetails() { | ||
System.out.println("Guest Name: " + name); | ||
System.out.println("Checked Out Books:"); | ||
for (Book book : checkedOutBooks) { | ||
book.displayBookDetails(); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/AaronSantiago/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,16 @@ | ||
package com.codedifferently.lesson9.library; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class BookTest { | ||
|
||
@Test | ||
public void testBookDetails() { | ||
Book book = new Book("The Great Gatsby", "F. Scott Fitzgerald", "123456789"); | ||
assertEquals("The Great Gatsby", book.getTitle()); | ||
assertEquals("F. Scott Fitzgerald", book.getAuthor()); | ||
assertEquals("123456789", book.getISBN()); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...p_app/src/test/java/com/codedifferently/lesson9/AaronSantiago/LibraryBuildingLogTest.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,32 @@ | ||
package com.codedifferently.lesson9.library; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class LibraryBuildingLogTest { | ||
private LibraryBuildingLog library; | ||
private Book book1; | ||
private LibraryGuest guest; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
library = new LibraryBuildingLog(); | ||
book1 = new Book("Harry Potter: Half-Blood Prince", "J.K. Rowling", "1122334455"); | ||
guest = new LibraryGuest("Voldemort"); | ||
|
||
library.addBook(book1); | ||
library.registerGuest(guest); | ||
} | ||
|
||
@Test | ||
public void testCheckOutAndReturnBook() { | ||
assertFalse(library.checkOutBook("NonExistentISBN", guest)); | ||
assertTrue(library.checkOutBook(book1.getISBN(), guest)); | ||
assertFalse(library.checkOutBook(book1.getISBN(), guest), "Book should already be checked out"); | ||
|
||
assertTrue(library.returnBook(book1.getISBN(), guest)); | ||
assertFalse(library.returnBook(book1.getISBN(), guest), "Book was already returned"); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...oop/oop_app/src/test/java/com/codedifferently/lesson9/AaronSantiago/LibraryGuestTest.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.library; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class LibraryGuestTest { | ||
private Book book1; | ||
private LibraryGuest guest; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
book1 = new Book("No Man's Land", "Greg Rucka", "987654321"); | ||
guest = new LibraryGuest("Batman"); | ||
} | ||
|
||
@Test | ||
public void testCheckOutAndReturnBook() { | ||
assertTrue(guest.getCheckedOutBooks().isEmpty()); | ||
|
||
guest.checkOutBook(book1); | ||
assertEquals(1, guest.getCheckedOutBooks().size()); | ||
assertEquals(book1, guest.getCheckedOutBooks().get(0)); | ||
|
||
guest.returnBook(book1); | ||
assertTrue(guest.getCheckedOutBooks().isEmpty()); | ||
} | ||
} |