Skip to content

Commit b44fb9c

Browse files
authored
feat: adds Aaron's LibraryLog ,Book, and Guest with test blocks (#261)
1 parent 65248ba commit b44fb9c

File tree

6 files changed

+219
-0
lines changed

6 files changed

+219
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.codedifferently.lesson9.library;
2+
3+
class Book {
4+
private String title;
5+
private String author;
6+
private String ISBN;
7+
8+
public Book(String title, String author, String ISBN) {
9+
this.title = title;
10+
this.author = author;
11+
this.ISBN = ISBN;
12+
}
13+
14+
public String getTitle() {
15+
return title;
16+
}
17+
18+
public String getAuthor() {
19+
return author;
20+
}
21+
22+
public String getISBN() {
23+
return ISBN;
24+
}
25+
26+
public void displayBookDetails() {
27+
System.out.println("Title: " + title);
28+
System.out.println("Author: " + author);
29+
System.out.println("ISBN: " + ISBN);
30+
}
31+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.codedifferently.lesson9.library;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class LibraryBuildingLog {
9+
private Map<String, Book> books;
10+
private List<LibraryGuest> guests;
11+
12+
public LibraryBuildingLog() {
13+
this.books = new HashMap<>();
14+
this.guests = new ArrayList<>();
15+
}
16+
17+
public void addBook(Book book) {
18+
books.put(book.getISBN(), book);
19+
}
20+
21+
public boolean removeBook(String ISBN) {
22+
return books.remove(ISBN) != null;
23+
}
24+
25+
public void registerGuest(LibraryGuest guest) {
26+
guests.add(guest);
27+
}
28+
29+
public boolean checkOutBook(String ISBN, LibraryGuest guest) {
30+
Book book = books.get(ISBN);
31+
if (book != null && !isBookCheckedOut(book)) {
32+
guest.checkOutBook(book);
33+
return true;
34+
}
35+
return false;
36+
}
37+
38+
public boolean returnBook(String ISBN, LibraryGuest guest) {
39+
Book book = books.get(ISBN);
40+
if (book != null) {
41+
return guest.returnBook(book);
42+
}
43+
return false;
44+
}
45+
46+
private boolean isBookCheckedOut(Book book) {
47+
for (LibraryGuest guest : guests) {
48+
if (guest.getCheckedOutBooks().contains(book)) {
49+
return true;
50+
}
51+
}
52+
return false;
53+
}
54+
55+
public void displayLibraryInformation() {
56+
System.out.println("Library Books:");
57+
books.values().forEach(Book::displayBookDetails);
58+
System.out.println("\nLibrary Guests:");
59+
guests.forEach(
60+
guest -> {
61+
System.out.println("Name: " + guest.getName());
62+
if (!guest.getCheckedOutBooks().isEmpty()) {
63+
guest.displayGuestDetails();
64+
} else {
65+
System.out.println("No books checked out.");
66+
}
67+
});
68+
}
69+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.codedifferently.lesson9.library;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
class LibraryGuest {
7+
private String name;
8+
private List<Book> checkedOutBooks;
9+
10+
public LibraryGuest(String name) {
11+
this.name = name;
12+
this.checkedOutBooks = new ArrayList<>();
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
23+
public void checkOutBook(Book book) {
24+
checkedOutBooks.add(book);
25+
}
26+
27+
public boolean returnBook(Book book) {
28+
return checkedOutBooks.remove(book);
29+
}
30+
31+
public List<Book> getCheckedOutBooks() {
32+
return checkedOutBooks;
33+
}
34+
35+
public void displayGuestDetails() {
36+
System.out.println("Guest Name: " + name);
37+
System.out.println("Checked Out Books:");
38+
for (Book book : checkedOutBooks) {
39+
book.displayBookDetails();
40+
}
41+
}
42+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.codedifferently.lesson9.library;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class BookTest {
8+
9+
@Test
10+
public void testBookDetails() {
11+
Book book = new Book("The Great Gatsby", "F. Scott Fitzgerald", "123456789");
12+
assertEquals("The Great Gatsby", book.getTitle());
13+
assertEquals("F. Scott Fitzgerald", book.getAuthor());
14+
assertEquals("123456789", book.getISBN());
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.codedifferently.lesson9.library;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class LibraryBuildingLogTest {
9+
private LibraryBuildingLog library;
10+
private Book book1;
11+
private LibraryGuest guest;
12+
13+
@BeforeEach
14+
public void setUp() {
15+
library = new LibraryBuildingLog();
16+
book1 = new Book("Harry Potter: Half-Blood Prince", "J.K. Rowling", "1122334455");
17+
guest = new LibraryGuest("Voldemort");
18+
19+
library.addBook(book1);
20+
library.registerGuest(guest);
21+
}
22+
23+
@Test
24+
public void testCheckOutAndReturnBook() {
25+
assertFalse(library.checkOutBook("NonExistentISBN", guest));
26+
assertTrue(library.checkOutBook(book1.getISBN(), guest));
27+
assertFalse(library.checkOutBook(book1.getISBN(), guest), "Book should already be checked out");
28+
29+
assertTrue(library.returnBook(book1.getISBN(), guest));
30+
assertFalse(library.returnBook(book1.getISBN(), guest), "Book was already returned");
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.codedifferently.lesson9.library;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class LibraryGuestTest {
9+
private Book book1;
10+
private LibraryGuest guest;
11+
12+
@BeforeEach
13+
public void setUp() {
14+
book1 = new Book("No Man's Land", "Greg Rucka", "987654321");
15+
guest = new LibraryGuest("Batman");
16+
}
17+
18+
@Test
19+
public void testCheckOutAndReturnBook() {
20+
assertTrue(guest.getCheckedOutBooks().isEmpty());
21+
22+
guest.checkOutBook(book1);
23+
assertEquals(1, guest.getCheckedOutBooks().size());
24+
assertEquals(book1, guest.getCheckedOutBooks().get(0));
25+
26+
guest.returnBook(book1);
27+
assertTrue(guest.getCheckedOutBooks().isEmpty());
28+
}
29+
}

0 commit comments

Comments
 (0)