Skip to content

Commit

Permalink
feat: adds Vicente's Library Management System containing related cla…
Browse files Browse the repository at this point in the history
…sses 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
  • Loading branch information
VicenteVigueras committed Mar 22, 2024
1 parent 0f61d3f commit dae48c5
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 0 deletions.
@@ -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;
}
}
@@ -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<Book> books;
private List<Patron> 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<Book> getBooks() {
return books;
}

/**
* Gets the list of patrons registered in the library.
*
* @return The list of patrons registered in the library.
*/
public List<Patron> getPatrons() {
return patrons;
}
}
@@ -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<Book> 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<Book> 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);
}
}
@@ -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());
}
}
@@ -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<Book> books = library.getBooks();
assertEquals(1, books.size());
assertEquals(book, books.get(0));
}

@Test
public void testRemoveBook() {
library.removeBook(book);
List<Book> 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));
}
}

0 comments on commit dae48c5

Please sign in to comment.