diff --git a/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Book.java b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Book.java new file mode 100644 index 00000000..d8a4a571 --- /dev/null +++ b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Book.java @@ -0,0 +1,116 @@ +package com.codedifferently.lesson9.LibraryManagementSystem; + +/** Represents a book in the library. */ +public class Book { + private String title; + private String author; + private String isbn; + private int numberOfPages; + private boolean checkedOut; + + /** + * Constructs a new Book instance with the given parameters. + * + * @param title The title of the book. + * @param isbn The ISBN of the book. + * @param author The author(s) of the book. + * @param numberOfPages The number of pages in the book. + */ + public Book(String title, String isbn, String author, int numberOfPages) { + this.title = title; + this.isbn = isbn; + this.author = author; + this.numberOfPages = numberOfPages; + this.checkedOut = false; // Initially, the book is not checked out + } + + /** + * Gets the author(s) of the book. + * + * @return The author(s) of the book. + */ + public String getAuthor() { + return author; + } + + /** + * Sets the author(s) of the book. + * + * @param author The author(s) of the book. + */ + public void setAuthor(String author) { + this.author = author; + } + + /** + * Gets the ISBN of the book. + * + * @return The ISBN of the book. + */ + public String getIsbn() { + return isbn; + } + + /** + * Sets the ISBN of the book. + * + * @param isbn The ISBN of the book. + */ + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + /** + * Gets the number of pages in the book. + * + * @return The number of pages in the book. + */ + public int getNumberOfPages() { + return numberOfPages; + } + + /** + * Sets the number of pages in the book. + * + * @param numberOfPages The number of pages in the book. + */ + public void setNumberOfPages(int numberOfPages) { + this.numberOfPages = numberOfPages; + } + + /** + * Gets the title of the book. + * + * @return The title of the book. + */ + public String getTitle() { + return title; + } + + /** + * Sets the title of the book. + * + * @param title The title of the book. + */ + public void setTitle(String title) { + this.title = title; + } + + /** + * Checks if the book is currently checked out. + * + * @return True if the book is checked out, false otherwise. + */ + public boolean isCheckedOut() { + return checkedOut; + } + + /** + * Sets the checked out status of the book. + * + * @param checkedOut True if the book is to be checked out, false otherwise. + */ + public void setCheckedOut(boolean checkedOut) { + this.checkedOut = checkedOut; + } +} diff --git a/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Library.java b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Library.java new file mode 100644 index 00000000..39039e10 --- /dev/null +++ b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Library.java @@ -0,0 +1,101 @@ +package com.codedifferently.lesson9.LibraryManagementSystem; + +import java.util.ArrayList; +import java.util.List; + +/** Represents a library that manages a collection of books and patrons. */ +public class Library { + private List books; + private List patrons; + + /** Constructs a new Library instance with empty collections of books and patrons. */ + public Library() { + this.books = new ArrayList<>(); + this.patrons = new ArrayList<>(); + } + + /** + * Adds a book to the library's collection. + * + * @param book The book to be added. + */ + public void addBook(Book book) { + books.add(book); + } + + /** + * Removes a book from the library's collection. + * + * @param book The book to be removed. + */ + public void removeBook(Book book) { + books.remove(book); + } + + /** + * Registers a new patron with the library. + * + * @param patron The patron to be registered. + */ + public void registerPatron(Patron patron) { + patrons.add(patron); + } + + /** + * Checks out a book to a patron. + * + * @param book The book to be checked out. + * @param patron The patron who is checking out the book. + */ + public void checkOutBook(Book book, Patron patron) { + if (!books.contains(book)) { + throw new IllegalArgumentException("Book not found in the library"); + } + if (!patrons.contains(patron)) { + throw new IllegalArgumentException("Patron not registered in the library"); + } + if (book.isCheckedOut()) { + throw new IllegalStateException("Book is already checked out"); + } + book.setCheckedOut(true); + patron.addCheckedOutBook(book); + } + + /** + * Returns a book that was checked out by a patron. + * + * @param book The book to be returned. + * @param patron The patron who is returning the book. + */ + public void returnBook(Book book, Patron patron) { + if (!books.contains(book)) { + throw new IllegalArgumentException("Book not found in the library"); + } + if (!patrons.contains(patron)) { + throw new IllegalArgumentException("Patron not registered in the library"); + } + if (!book.isCheckedOut()) { + throw new IllegalStateException("Book is not checked out"); + } + book.setCheckedOut(false); + patron.removeCheckedOutBook(book); + } + + /** + * Gets the list of books in the library. + * + * @return The list of books in the library. + */ + public List getBooks() { + return books; + } + + /** + * Gets the list of patrons registered in the library. + * + * @return The list of patrons registered in the library. + */ + public List getPatrons() { + return patrons; + } +} diff --git a/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Patron.java b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Patron.java new file mode 100644 index 00000000..a2378b99 --- /dev/null +++ b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Patron.java @@ -0,0 +1,65 @@ +package com.codedifferently.lesson9.LibraryManagementSystem; + +import java.util.ArrayList; +import java.util.List; + +/** Represents a patron of the library. */ +public class Patron { + private String name; + private List checkedOutBooks; + + /** + * Constructs a new Patron instance with the given name. + * + * @param name The name of the patron. + */ + public Patron(String name) { + this.name = name; + this.checkedOutBooks = new ArrayList<>(); + } + + /** + * Gets the name of the patron. + * + * @return The name of the patron. + */ + public String getName() { + return name; + } + + /** + * Sets the name of the patron. + * + * @param name The name of the patron. + */ + public void setName(String name) { + this.name = name; + } + + /** + * Gets the list of books checked out by the patron. + * + * @return The list of checked out books. + */ + public List getCheckedOutBooks() { + return checkedOutBooks; + } + + /** + * Adds a book to the list of books checked out by the patron. + * + * @param book The book to be added. + */ + public void addCheckedOutBook(Book book) { + checkedOutBooks.add(book); + } + + /** + * Removes a book from the list of books checked out by the patron. + * + * @param book The book to be removed. + */ + public void removeCheckedOutBook(Book book) { + checkedOutBooks.remove(book); + } +} diff --git a/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/BookTest.java b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/BookTest.java new file mode 100644 index 00000000..d996cf2a --- /dev/null +++ b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/BookTest.java @@ -0,0 +1,29 @@ +package com.codedifferently.lesson9.LibraryManagementSystemTest; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.codedifferently.lesson9.LibraryManagementSystem.Book; +import org.junit.jupiter.api.Test; + +/** Test class for the {@link com.codedifferently.lesson9.LibraryManagementSystem.Book} class. */ +public class BookTest { + + /** Test the constructor of the Book class. */ + @Test + public void testBookConstructor() { + // Arrange + String title = "The Pragmatic Programmer"; + String isbn = "9780135957059"; + String author = "David Thomas, Andrew Hunt"; + int numberOfPages = 320; + + // Act + Book book = new Book(title, isbn, author, numberOfPages); + + // Assert + assertEquals(title, book.getTitle()); + assertEquals(isbn, book.getIsbn()); + assertEquals(author, book.getAuthor()); + assertEquals(numberOfPages, book.getNumberOfPages()); + } +} diff --git a/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryTest.java b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryTest.java new file mode 100644 index 00000000..05901b78 --- /dev/null +++ b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryTest.java @@ -0,0 +1,57 @@ +package com.codedifferently.lesson9.LibraryManagementSystemTest; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.codedifferently.lesson9.LibraryManagementSystem.Book; +import com.codedifferently.lesson9.LibraryManagementSystem.Library; +import com.codedifferently.lesson9.LibraryManagementSystem.Patron; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class LibraryTest { + private Library library; + private Book book; + private Patron patron; + + @BeforeEach + public void setUp() { + library = new Library(); + book = new Book("The Pragmatic Programmer", "9780135957059", "David Thomas, Andrew Hunt", 320); + // Add the book to the library + library.addBook(book); + patron = new Patron("John Doe"); + library.registerPatron(patron); + } + + @Test + public void testAddBook() { + List books = library.getBooks(); + assertEquals(1, books.size()); + assertEquals(book, books.get(0)); + } + + @Test + public void testRemoveBook() { + library.removeBook(book); + List books = library.getBooks(); + assertEquals(0, books.size()); + } + + @Test + public void testCheckOutBook() { + library.checkOutBook(book, patron); + assertTrue(book.isCheckedOut()); + assertTrue(patron.getCheckedOutBooks().contains(book)); + } + + @Test + public void testReturnBook() { + library.checkOutBook(book, patron); + library.returnBook(book, patron); + assertFalse(book.isCheckedOut()); + assertFalse(patron.getCheckedOutBooks().contains(book)); + } +} diff --git a/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/PatronTest.java b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/PatronTest.java new file mode 100644 index 00000000..683cbac9 --- /dev/null +++ b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/PatronTest.java @@ -0,0 +1,71 @@ +package com.codedifferently.lesson9.LibraryManagementSystemTest; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.codedifferently.lesson9.LibraryManagementSystem.Book; +import com.codedifferently.lesson9.LibraryManagementSystem.Patron; +import org.junit.jupiter.api.Test; + +/** Test class for Patron class. */ +public class PatronTest { + + /** Test the constructor of the Patron class. */ + @Test + public void testPatronConstructor() { + // Arrange + String name = "John Doe"; + + // Act + Patron patron = new Patron(name); + + // Assert + assertEquals(name, patron.getName()); + } + + /** Test setting the name of the patron. */ + @Test + public void testSetName() { + // Arrange + Patron patron = new Patron("John Doe"); + String newName = "Jane Smith"; + + // Act + patron.setName(newName); + + // Assert + assertEquals(newName, patron.getName()); + } + + /** Test adding a checked out book to the patron's list of checked out books. */ + @Test + public void testAddCheckedOutBook() { + // Arrange + Patron patron = new Patron("John Doe"); + Book book = + new Book("The Pragmatic Programmer", "9780135957059", "David Thomas, Andrew Hunt", 320); + + // Act + patron.addCheckedOutBook(book); + + // Assert + assertTrue(patron.getCheckedOutBooks().contains(book)); + } + + /** Test removing a checked out book from the patron's list of checked out books. */ + @Test + public void testRemoveCheckedOutBook() { + // Arrange + Patron patron = new Patron("John Doe"); + Book book = + new Book("The Pragmatic Programmer", "9780135957059", "David Thomas, Andrew Hunt", 320); + patron.addCheckedOutBook(book); + + // Act + patron.removeCheckedOutBook(book); + + // Assert + assertFalse(patron.getCheckedOutBooks().contains(book)); + } +}