Skip to content

Commit dae48c5

Browse files
feat: adds Vicente's Library Management System containing related classes and tests (#267)
* chore: uploading an outline of lesson09 to prevent the work from being lost. * chore: uploading work to prevent it from being lost * chore: Uploading my progress for LibraryManagementSystem assignment * fix: ran spotlessApply and fixed some of my tests in LibraryTest * fix: deleted a file that doesn't make sense anymore
1 parent 0f61d3f commit dae48c5

File tree

6 files changed

+439
-0
lines changed

6 files changed

+439
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.codedifferently.lesson9.LibraryManagementSystem;
2+
3+
/** Represents a book in the library. */
4+
public class Book {
5+
private String title;
6+
private String author;
7+
private String isbn;
8+
private int numberOfPages;
9+
private boolean checkedOut;
10+
11+
/**
12+
* Constructs a new Book instance with the given parameters.
13+
*
14+
* @param title The title of the book.
15+
* @param isbn The ISBN of the book.
16+
* @param author The author(s) of the book.
17+
* @param numberOfPages The number of pages in the book.
18+
*/
19+
public Book(String title, String isbn, String author, int numberOfPages) {
20+
this.title = title;
21+
this.isbn = isbn;
22+
this.author = author;
23+
this.numberOfPages = numberOfPages;
24+
this.checkedOut = false; // Initially, the book is not checked out
25+
}
26+
27+
/**
28+
* Gets the author(s) of the book.
29+
*
30+
* @return The author(s) of the book.
31+
*/
32+
public String getAuthor() {
33+
return author;
34+
}
35+
36+
/**
37+
* Sets the author(s) of the book.
38+
*
39+
* @param author The author(s) of the book.
40+
*/
41+
public void setAuthor(String author) {
42+
this.author = author;
43+
}
44+
45+
/**
46+
* Gets the ISBN of the book.
47+
*
48+
* @return The ISBN of the book.
49+
*/
50+
public String getIsbn() {
51+
return isbn;
52+
}
53+
54+
/**
55+
* Sets the ISBN of the book.
56+
*
57+
* @param isbn The ISBN of the book.
58+
*/
59+
public void setIsbn(String isbn) {
60+
this.isbn = isbn;
61+
}
62+
63+
/**
64+
* Gets the number of pages in the book.
65+
*
66+
* @return The number of pages in the book.
67+
*/
68+
public int getNumberOfPages() {
69+
return numberOfPages;
70+
}
71+
72+
/**
73+
* Sets the number of pages in the book.
74+
*
75+
* @param numberOfPages The number of pages in the book.
76+
*/
77+
public void setNumberOfPages(int numberOfPages) {
78+
this.numberOfPages = numberOfPages;
79+
}
80+
81+
/**
82+
* Gets the title of the book.
83+
*
84+
* @return The title of the book.
85+
*/
86+
public String getTitle() {
87+
return title;
88+
}
89+
90+
/**
91+
* Sets the title of the book.
92+
*
93+
* @param title The title of the book.
94+
*/
95+
public void setTitle(String title) {
96+
this.title = title;
97+
}
98+
99+
/**
100+
* Checks if the book is currently checked out.
101+
*
102+
* @return True if the book is checked out, false otherwise.
103+
*/
104+
public boolean isCheckedOut() {
105+
return checkedOut;
106+
}
107+
108+
/**
109+
* Sets the checked out status of the book.
110+
*
111+
* @param checkedOut True if the book is to be checked out, false otherwise.
112+
*/
113+
public void setCheckedOut(boolean checkedOut) {
114+
this.checkedOut = checkedOut;
115+
}
116+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.codedifferently.lesson9.LibraryManagementSystem;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/** Represents a library that manages a collection of books and patrons. */
7+
public class Library {
8+
private List<Book> books;
9+
private List<Patron> patrons;
10+
11+
/** Constructs a new Library instance with empty collections of books and patrons. */
12+
public Library() {
13+
this.books = new ArrayList<>();
14+
this.patrons = new ArrayList<>();
15+
}
16+
17+
/**
18+
* Adds a book to the library's collection.
19+
*
20+
* @param book The book to be added.
21+
*/
22+
public void addBook(Book book) {
23+
books.add(book);
24+
}
25+
26+
/**
27+
* Removes a book from the library's collection.
28+
*
29+
* @param book The book to be removed.
30+
*/
31+
public void removeBook(Book book) {
32+
books.remove(book);
33+
}
34+
35+
/**
36+
* Registers a new patron with the library.
37+
*
38+
* @param patron The patron to be registered.
39+
*/
40+
public void registerPatron(Patron patron) {
41+
patrons.add(patron);
42+
}
43+
44+
/**
45+
* Checks out a book to a patron.
46+
*
47+
* @param book The book to be checked out.
48+
* @param patron The patron who is checking out the book.
49+
*/
50+
public void checkOutBook(Book book, Patron patron) {
51+
if (!books.contains(book)) {
52+
throw new IllegalArgumentException("Book not found in the library");
53+
}
54+
if (!patrons.contains(patron)) {
55+
throw new IllegalArgumentException("Patron not registered in the library");
56+
}
57+
if (book.isCheckedOut()) {
58+
throw new IllegalStateException("Book is already checked out");
59+
}
60+
book.setCheckedOut(true);
61+
patron.addCheckedOutBook(book);
62+
}
63+
64+
/**
65+
* Returns a book that was checked out by a patron.
66+
*
67+
* @param book The book to be returned.
68+
* @param patron The patron who is returning the book.
69+
*/
70+
public void returnBook(Book book, Patron patron) {
71+
if (!books.contains(book)) {
72+
throw new IllegalArgumentException("Book not found in the library");
73+
}
74+
if (!patrons.contains(patron)) {
75+
throw new IllegalArgumentException("Patron not registered in the library");
76+
}
77+
if (!book.isCheckedOut()) {
78+
throw new IllegalStateException("Book is not checked out");
79+
}
80+
book.setCheckedOut(false);
81+
patron.removeCheckedOutBook(book);
82+
}
83+
84+
/**
85+
* Gets the list of books in the library.
86+
*
87+
* @return The list of books in the library.
88+
*/
89+
public List<Book> getBooks() {
90+
return books;
91+
}
92+
93+
/**
94+
* Gets the list of patrons registered in the library.
95+
*
96+
* @return The list of patrons registered in the library.
97+
*/
98+
public List<Patron> getPatrons() {
99+
return patrons;
100+
}
101+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.codedifferently.lesson9.LibraryManagementSystem;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/** Represents a patron of the library. */
7+
public class Patron {
8+
private String name;
9+
private List<Book> checkedOutBooks;
10+
11+
/**
12+
* Constructs a new Patron instance with the given name.
13+
*
14+
* @param name The name of the patron.
15+
*/
16+
public Patron(String name) {
17+
this.name = name;
18+
this.checkedOutBooks = new ArrayList<>();
19+
}
20+
21+
/**
22+
* Gets the name of the patron.
23+
*
24+
* @return The name of the patron.
25+
*/
26+
public String getName() {
27+
return name;
28+
}
29+
30+
/**
31+
* Sets the name of the patron.
32+
*
33+
* @param name The name of the patron.
34+
*/
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
/**
40+
* Gets the list of books checked out by the patron.
41+
*
42+
* @return The list of checked out books.
43+
*/
44+
public List<Book> getCheckedOutBooks() {
45+
return checkedOutBooks;
46+
}
47+
48+
/**
49+
* Adds a book to the list of books checked out by the patron.
50+
*
51+
* @param book The book to be added.
52+
*/
53+
public void addCheckedOutBook(Book book) {
54+
checkedOutBooks.add(book);
55+
}
56+
57+
/**
58+
* Removes a book from the list of books checked out by the patron.
59+
*
60+
* @param book The book to be removed.
61+
*/
62+
public void removeCheckedOutBook(Book book) {
63+
checkedOutBooks.remove(book);
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.codedifferently.lesson9.LibraryManagementSystemTest;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.codedifferently.lesson9.LibraryManagementSystem.Book;
6+
import org.junit.jupiter.api.Test;
7+
8+
/** Test class for the {@link com.codedifferently.lesson9.LibraryManagementSystem.Book} class. */
9+
public class BookTest {
10+
11+
/** Test the constructor of the Book class. */
12+
@Test
13+
public void testBookConstructor() {
14+
// Arrange
15+
String title = "The Pragmatic Programmer";
16+
String isbn = "9780135957059";
17+
String author = "David Thomas, Andrew Hunt";
18+
int numberOfPages = 320;
19+
20+
// Act
21+
Book book = new Book(title, isbn, author, numberOfPages);
22+
23+
// Assert
24+
assertEquals(title, book.getTitle());
25+
assertEquals(isbn, book.getIsbn());
26+
assertEquals(author, book.getAuthor());
27+
assertEquals(numberOfPages, book.getNumberOfPages());
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.codedifferently.lesson9.LibraryManagementSystemTest;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import com.codedifferently.lesson9.LibraryManagementSystem.Book;
8+
import com.codedifferently.lesson9.LibraryManagementSystem.Library;
9+
import com.codedifferently.lesson9.LibraryManagementSystem.Patron;
10+
import java.util.List;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
public class LibraryTest {
15+
private Library library;
16+
private Book book;
17+
private Patron patron;
18+
19+
@BeforeEach
20+
public void setUp() {
21+
library = new Library();
22+
book = new Book("The Pragmatic Programmer", "9780135957059", "David Thomas, Andrew Hunt", 320);
23+
// Add the book to the library
24+
library.addBook(book);
25+
patron = new Patron("John Doe");
26+
library.registerPatron(patron);
27+
}
28+
29+
@Test
30+
public void testAddBook() {
31+
List<Book> books = library.getBooks();
32+
assertEquals(1, books.size());
33+
assertEquals(book, books.get(0));
34+
}
35+
36+
@Test
37+
public void testRemoveBook() {
38+
library.removeBook(book);
39+
List<Book> books = library.getBooks();
40+
assertEquals(0, books.size());
41+
}
42+
43+
@Test
44+
public void testCheckOutBook() {
45+
library.checkOutBook(book, patron);
46+
assertTrue(book.isCheckedOut());
47+
assertTrue(patron.getCheckedOutBooks().contains(book));
48+
}
49+
50+
@Test
51+
public void testReturnBook() {
52+
library.checkOutBook(book, patron);
53+
library.returnBook(book, patron);
54+
assertFalse(book.isCheckedOut());
55+
assertFalse(patron.getCheckedOutBooks().contains(book));
56+
}
57+
}

0 commit comments

Comments
 (0)