Skip to content

Commit 4027724

Browse files
authored
feat: adds Sherllin's library management system with tests (#259)
1 parent b8b615d commit 4027724

File tree

4 files changed

+255
-0
lines changed

4 files changed

+255
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.codedifferently.lesson9.sherllin;
2+
3+
// We want to define a book and keep track of its title, isbn, author(s), number of pages, and
4+
// whether it is checked out or not.
5+
// We want define a patron with properties including their name and the books they have checked out.
6+
// We want to define a library that:
7+
// Allows us to add/remove a book to and from its collection of books.
8+
// Allows us to register new patrons
9+
// Allows us to check out books to patrons.
10+
// Allows us to return books from patrons.
11+
12+
public class Book {
13+
private String bookTitle;
14+
private long isbn;
15+
private String author;
16+
private int pages;
17+
private boolean checkedOut;
18+
19+
public Book(String bookTitle, long isbn, String author, int pages, boolean checkedOut) {
20+
this.bookTitle = bookTitle;
21+
this.isbn = isbn;
22+
this.author = author;
23+
this.pages = pages;
24+
this.checkedOut = checkedOut;
25+
}
26+
27+
public String getBookTitle() {
28+
return bookTitle;
29+
}
30+
31+
public long getIsbn() {
32+
return isbn;
33+
}
34+
35+
public String getAuthor() {
36+
return author;
37+
}
38+
39+
public int getPages() {
40+
return pages;
41+
}
42+
43+
public boolean checkedOut() {
44+
return checkedOut;
45+
}
46+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.codedifferently.lesson9.sherllin;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
// We want to define a library that:
7+
// Allows us to add/remove a book to and from its collection of books.
8+
// Allows us to register new patrons
9+
// Allows us to check out books to patrons.
10+
11+
public class Library {
12+
private Map<Long, Boolean> bookStatus; // Used ISBN as the key for book status
13+
private Map<String, Integer> patronBooks; // Used name as key for patron's checked-out books count
14+
15+
public Library() {
16+
this.bookStatus = new HashMap<>();
17+
this.patronBooks = new HashMap<>();
18+
}
19+
20+
public void addBook(long isbn) {
21+
bookStatus.put(isbn, false); // Book is not checked out
22+
}
23+
24+
public void removeBook(long isbn) {
25+
bookStatus.remove(isbn);
26+
}
27+
28+
public void registerPatron(String patronName) {
29+
patronBooks.put(patronName, 0); // Patron has no books checked out
30+
}
31+
32+
public void checkoutOrReturnBook(long isbn, String patronName, boolean checkOut) {
33+
Boolean isBookCheckedOut = bookStatus.get(isbn);
34+
Integer checkedOutBooksCount = patronBooks.getOrDefault(patronName, 0);
35+
36+
if (isBookCheckedOut == null || checkedOutBooksCount == null) {
37+
System.out.println("Book or patron not found");
38+
return;
39+
}
40+
41+
if (checkOut) {
42+
if (!isBookCheckedOut) {
43+
bookStatus.put(isbn, true);
44+
patronBooks.put(patronName, checkedOutBooksCount + 1);
45+
System.out.println("Book checked out to " + patronName);
46+
} else {
47+
System.out.println("Sorry! Book already checked out");
48+
}
49+
} else {
50+
if (isBookCheckedOut && checkedOutBooksCount > 0) {
51+
bookStatus.put(isbn, false);
52+
patronBooks.put(patronName, checkedOutBooksCount - 1);
53+
System.out.println("Book returned by " + patronName);
54+
} else {
55+
System.out.println("Book not checked out or invalid return operation");
56+
}
57+
}
58+
}
59+
60+
public Map<Long, Boolean> getBookStatus() {
61+
return bookStatus;
62+
}
63+
64+
public Map<String, Integer> getPatronBooks() {
65+
return patronBooks;
66+
}
67+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.codedifferently.lesson9.sherllin;
2+
3+
public class Patron {
4+
private String name;
5+
private int bookCheckedOut;
6+
7+
public Patron(String name, int bookCheckedOut) {
8+
this.name = name;
9+
this.bookCheckedOut = bookCheckedOut;
10+
}
11+
12+
public String getName() {
13+
return name;
14+
}
15+
16+
public int getBookCheckedOut() {
17+
return bookCheckedOut;
18+
}
19+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)