Skip to content

Commit

Permalink
feat: adds Aaron's LibraryLog ,Book, and Guest with test blocks (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron2278 committed Mar 22, 2024
1 parent 65248ba commit b44fb9c
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 0 deletions.
@@ -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);
}
}
@@ -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.");
}
});
}
}
@@ -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();
}
}
}
@@ -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());
}
}
@@ -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");
}
}
@@ -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());
}
}

0 comments on commit b44fb9c

Please sign in to comment.