From 24ca5cd38b7a48fec1d05244c6f59fab9d7b0688 Mon Sep 17 00:00:00 2001 From: Vicente Vigueras Date: Tue, 19 Mar 2024 17:56:57 +0000 Subject: [PATCH 1/5] chore: uploading an outline of lesson09 to prevent the work from being lost. --- .../lesson9/LibraryManagementSystem/Book.java | 11 ++++++++ .../LibraryManagementSystem/Library.java | 12 +++++++++ .../LibraryManagementSystem/Patron.java | 5 ++++ .../LibraryManagementSystemTest.java | 26 +++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Book.java create mode 100644 lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Library.java create mode 100644 lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Patron.java create mode 100644 lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryManagementSystemTest.java 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..a3d14ce9 --- /dev/null +++ b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Book.java @@ -0,0 +1,11 @@ +package com.codedifferently.lesson9.LibraryManagementSystem; + +public class Book { + String title; + String author; + int isbn; + int numberofpages; + + + +} \ No newline at end of file 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..71ee57f9 --- /dev/null +++ b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Library.java @@ -0,0 +1,12 @@ +package com.codedifferently.lesson9.LibraryManagementSystem; + +public class Library { + private List Books; + + public List addBook(book) { + + return + + } + +} \ No newline at end of file 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..6dd1d4ac --- /dev/null +++ b/lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/LibraryManagementSystem/Patron.java @@ -0,0 +1,5 @@ +package com.codedifferently.lesson9.LibraryManagementSystem; + +public class Patron { + +} \ No newline at end of file diff --git a/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryManagementSystemTest.java b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryManagementSystemTest.java new file mode 100644 index 00000000..917706dc --- /dev/null +++ b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryManagementSystemTest.java @@ -0,0 +1,26 @@ +package com.codedifferently.lesson9.LibraryManagementSystemTest; + +import org.junit.jupiter.api.Test; + + +public class LibraryManagementSystemTest { + +@Test +public void testAddBook() { + Library library = new library(); // create a new library + Book book = new book(title,isbn,numberofpages,author); // create a new book + + library.addBook(book); // adds book to library + + /* addBook() method will be defined in library class + the logic inside addBook() will assign an element called book to a list + In my test, I'll need a way to check if an element is present within a list using assertThat() + + */ + + assertThat + +} + + +} \ No newline at end of file From 0155fa6be4992a6ef16dbaef8596b2bc9d7e8c1d Mon Sep 17 00:00:00 2001 From: Vicente Vigueras Date: Wed, 20 Mar 2024 13:03:33 +0000 Subject: [PATCH 2/5] chore: uploading work to prevent it from being lost --- .../lesson9/LibraryManagementSystem/Book.java | 39 ++++++++++++++++++- .../LibraryManagementSystem/Library.java | 9 ++--- .../LibraryManagementSystemTest.java | 16 ++++++-- 3 files changed, 55 insertions(+), 9 deletions(-) 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 index a3d14ce9..465c6dc4 100644 --- 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 @@ -5,7 +5,44 @@ public class Book { String author; int isbn; int numberofpages; - + + + public Book(String title, String isbn, String author, int pages) { + + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public int getIsbn() { + return isbn; + } + + public void setIsbn(int isbn) { + this.isbn = isbn; + } + + public int getNumberofpages() { + return numberofpages; + } + + public void setNumberofpages(int numberofpages) { + this.numberofpages = numberofpages; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + } \ No newline at end of file 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 index 71ee57f9..cbe15609 100644 --- 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 @@ -1,12 +1,11 @@ package com.codedifferently.lesson9.LibraryManagementSystem; public class Library { - private List Books; - - public List addBook(book) { - - return + public void addBook(Book book) { + } + + } \ No newline at end of file diff --git a/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryManagementSystemTest.java b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryManagementSystemTest.java index 917706dc..0bd5af76 100644 --- a/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryManagementSystemTest.java +++ b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryManagementSystemTest.java @@ -2,14 +2,24 @@ import org.junit.jupiter.api.Test; +import com.codedifferently.lesson9.LibraryManagementSystem.Book; +import com.codedifferently.lesson9.LibraryManagementSystem.Library; + public class LibraryManagementSystemTest { @Test public void testAddBook() { - Library library = new library(); // create a new library - Book book = new book(title,isbn,numberofpages,author); // create a new book + Library library = new Library(); // create a new library + + String title = "The Pragmatic Programmer, 20th Anniversary Edition"; + String isbn = "9780135957059"; + String author = "David Thomas, Andrew Hunt"; + int pages = 320; + + Book book = new Book(title,isbn,author,pages); // create a new book + library.addBook(book); // adds book to library /* addBook() method will be defined in library class @@ -18,7 +28,7 @@ the logic inside addBook() will assign an element called book to a list */ - assertThat + } From 8b4b2804014b6f59268d1cfdab286f113d1c6362 Mon Sep 17 00:00:00 2001 From: Vicente Vigueras Date: Wed, 20 Mar 2024 16:12:59 +0000 Subject: [PATCH 3/5] chore: Uploading my progress for LibraryManagementSystem assignment --- .../lesson9/LibraryManagementSystem/Book.java | 104 +++++++++++++++--- .../LibraryManagementSystem/Library.java | 102 ++++++++++++++++- .../LibraryManagementSystem/Patron.java | 64 ++++++++++- .../LibraryManagementSystemTest/BookTest.java | 33 ++++++ .../LibraryTest.java | 78 +++++++++++++ .../PatronTest.java | 79 +++++++++++++ 6 files changed, 438 insertions(+), 22 deletions(-) create mode 100644 lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/BookTest.java create mode 100644 lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryTest.java create mode 100644 lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/PatronTest.java 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 index 465c6dc4..66343596 100644 --- 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 @@ -1,48 +1,118 @@ package com.codedifferently.lesson9.LibraryManagementSystem; +/** + * Represents a book in the library. + */ public class Book { - String title; - String author; - int isbn; - int numberofpages; + private String title; + private String author; + private String isbn; + private int numberOfPages; + private boolean checkedOut; - - public Book(String title, String isbn, String author, int pages) { - + /** + * 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; } - public int getIsbn() { + /** + * Gets the ISBN of the book. + * + * @return The ISBN of the book. + */ + public String getIsbn() { return isbn; } - public void setIsbn(int isbn) { + /** + * Sets the ISBN of the book. + * + * @param isbn The ISBN of the book. + */ + public void setIsbn(String isbn) { this.isbn = isbn; } - public int getNumberofpages() { - return numberofpages; + /** + * Gets the number of pages in the book. + * + * @return The number of pages in the book. + */ + public int getNumberOfPages() { + return numberOfPages; } - public void setNumberofpages(int numberofpages) { - this.numberofpages = 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; + return title; } + /** + * Sets the title of the book. + * + * @param title The title of the book. + */ public void setTitle(String title) { - this.title = 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; + } - -} \ No newline at end of file + /** + * 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 index cbe15609..ee0c4c3e 100644 --- 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 @@ -1,11 +1,105 @@ 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); } - - -} \ No newline at end of file + /** + * 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 index 6dd1d4ac..9543fb63 100644 --- 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 @@ -1,5 +1,67 @@ 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); + } -} \ No newline at end of file + /** + * 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..05bdec6e --- /dev/null +++ b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/BookTest.java @@ -0,0 +1,33 @@ +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..1b3e788b --- /dev/null +++ b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryTest.java @@ -0,0 +1,78 @@ +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; + +/** + * Test class for the {@link com.codedifferently.lesson9.LibraryManagementSystem.Library} class. + */ +public class LibraryTest { + private Library library; + private Book book; + + /** + * Set up the library and book for each test. + */ + @BeforeEach + public void setUp() { + library = new Library(); + book = new Book("The Pragmatic Programmer", "9780135957059", "David Thomas, Andrew Hunt", 320); + } + + /** + * Test adding a book to the library. + */ + @Test + public void testAddBook() { + library.addBook(book); + List books = library.getBooks(); + assertEquals(1, books.size()); + assertEquals(book, books.get(0)); + } + + /** + * Test removing a book from the library. + */ + @Test + public void testRemoveBook() { + library.addBook(book); + library.removeBook(book); + List books = library.getBooks(); + assertEquals(0, books.size()); + } + + /** + * Test checking out a book from the library. + */ + @Test + public void testCheckOutBook() { + Patron patron = new Patron("John Doe"); + library.registerPatron(patron); + + library.checkOutBook(book, patron); + assertTrue(book.isCheckedOut()); + assertTrue(patron.getCheckedOutBooks().contains(book)); + } + + /** + * Test returning a book to the library. + */ + @Test + public void testReturnBook() { + Patron patron = new Patron("John Doe"); + library.registerPatron(patron); + 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..4db1649a --- /dev/null +++ b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/PatronTest.java @@ -0,0 +1,79 @@ +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.Patron; +import com.codedifferently.lesson9.LibraryManagementSystem.Book; +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)); + } +} From 33ba54e40e562c33a929ca3092256b89f9ac43fc Mon Sep 17 00:00:00 2001 From: Vicente Vigueras Date: Wed, 20 Mar 2024 16:25:09 +0000 Subject: [PATCH 4/5] fix: ran spotlessApply and fixed some of my tests in LibraryTest --- .../lesson9/LibraryManagementSystem/Book.java | 204 +++++++++--------- .../LibraryManagementSystem/Library.java | 172 ++++++++------- .../LibraryManagementSystem/Patron.java | 106 +++++---- .../LibraryManagementSystemTest/BookTest.java | 8 +- .../LibraryTest.java | 105 ++++----- .../PatronTest.java | 104 +++++---- 6 files changed, 329 insertions(+), 370 deletions(-) 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 index 66343596..d8a4a571 100644 --- 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 @@ -1,118 +1,116 @@ package com.codedifferently.lesson9.LibraryManagementSystem; -/** - * Represents a book in the library. - */ +/** Represents a book in the library. */ public class Book { - private String title; - private String author; - private String isbn; - private int numberOfPages; - private boolean checkedOut; + 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 - } + /** + * 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; - } + /** + * 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; - } + /** + * 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; - } + /** + * 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; - } + /** + * 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; - } + /** + * 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; - } + /** + * 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; - } + /** + * 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; - } + /** + * 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; - } + /** + * 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; - } + /** + * 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 index ee0c4c3e..39039e10 100644 --- 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 @@ -3,103 +3,99 @@ import java.util.ArrayList; import java.util.List; -/** - * Represents a library that manages a collection of books and patrons. - */ +/** Represents a library that manages a collection of books and patrons. */ public class Library { - private List books; - private List patrons; + 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<>(); - } + /** 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); - } + /** + * 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); - } + /** + * 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); - } + /** + * 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); + /** + * 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"); } - - /** - * 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); + if (!patrons.contains(patron)) { + throw new IllegalArgumentException("Patron not registered in the library"); } - - /** - * Gets the list of books in the library. - * - * @return The list of books in the library. - */ - public List getBooks() { - return books; + if (book.isCheckedOut()) { + throw new IllegalStateException("Book is already checked out"); } + book.setCheckedOut(true); + patron.addCheckedOutBook(book); + } - /** - * Gets the list of patrons registered in the library. - * - * @return The list of patrons registered in the library. - */ - public List getPatrons() { - return patrons; + /** + * 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 index 9543fb63..a2378b99 100644 --- 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 @@ -3,65 +3,63 @@ import java.util.ArrayList; import java.util.List; -/** - * Represents a patron of the library. - */ +/** Represents a patron of the library. */ public class Patron { - private String name; - private List checkedOutBooks; + 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<>(); - } + /** + * 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; - } + /** + * 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; - } + /** + * 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; - } + /** + * 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); - } + /** + * 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); - } + /** + * 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 index 05bdec6e..d996cf2a 100644 --- 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 @@ -5,14 +5,10 @@ import com.codedifferently.lesson9.LibraryManagementSystem.Book; import org.junit.jupiter.api.Test; -/** - * Test class for the {@link com.codedifferently.lesson9.LibraryManagementSystem.Book} class. - */ +/** Test class for the {@link com.codedifferently.lesson9.LibraryManagementSystem.Book} class. */ public class BookTest { - /** - * Test the constructor of the Book class. - */ + /** Test the constructor of the Book class. */ @Test public void testBookConstructor() { // Arrange 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 index 1b3e788b..05901b78 100644 --- 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 @@ -11,68 +11,47 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -/** - * Test class for the {@link com.codedifferently.lesson9.LibraryManagementSystem.Library} class. - */ public class LibraryTest { - private Library library; - private Book book; - - /** - * Set up the library and book for each test. - */ - @BeforeEach - public void setUp() { - library = new Library(); - book = new Book("The Pragmatic Programmer", "9780135957059", "David Thomas, Andrew Hunt", 320); - } - - /** - * Test adding a book to the library. - */ - @Test - public void testAddBook() { - library.addBook(book); - List books = library.getBooks(); - assertEquals(1, books.size()); - assertEquals(book, books.get(0)); - } - - /** - * Test removing a book from the library. - */ - @Test - public void testRemoveBook() { - library.addBook(book); - library.removeBook(book); - List books = library.getBooks(); - assertEquals(0, books.size()); - } - - /** - * Test checking out a book from the library. - */ - @Test - public void testCheckOutBook() { - Patron patron = new Patron("John Doe"); - library.registerPatron(patron); - - library.checkOutBook(book, patron); - assertTrue(book.isCheckedOut()); - assertTrue(patron.getCheckedOutBooks().contains(book)); - } - - /** - * Test returning a book to the library. - */ - @Test - public void testReturnBook() { - Patron patron = new Patron("John Doe"); - library.registerPatron(patron); - library.checkOutBook(book, patron); - - library.returnBook(book, patron); - assertFalse(book.isCheckedOut()); - assertFalse(patron.getCheckedOutBooks().contains(book)); - } + 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 index 4db1649a..683cbac9 100644 --- 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 @@ -4,76 +4,68 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import com.codedifferently.lesson9.LibraryManagementSystem.Patron; import com.codedifferently.lesson9.LibraryManagementSystem.Book; +import com.codedifferently.lesson9.LibraryManagementSystem.Patron; import org.junit.jupiter.api.Test; -/** - * Test class for Patron class. - */ +/** Test class for Patron class. */ public class PatronTest { - /** - * Test the constructor of the Patron class. - */ - @Test - public void testPatronConstructor() { - // Arrange - String name = "John Doe"; + /** Test the constructor of the Patron class. */ + @Test + public void testPatronConstructor() { + // Arrange + String name = "John Doe"; - // Act - Patron patron = new Patron(name); + // Act + Patron patron = new Patron(name); - // Assert - assertEquals(name, patron.getName()); - } + // 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"; + /** 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); + // Act + patron.setName(newName); - // Assert - assertEquals(newName, patron.getName()); - } + // 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); + /** 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); + // Act + patron.addCheckedOutBook(book); - // Assert - assertTrue(patron.getCheckedOutBooks().contains(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); + /** 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); + // Act + patron.removeCheckedOutBook(book); - // Assert - assertFalse(patron.getCheckedOutBooks().contains(book)); - } + // Assert + assertFalse(patron.getCheckedOutBooks().contains(book)); + } } From 4e272f9670facc72ce265848d3f5f3635f70b31c Mon Sep 17 00:00:00 2001 From: Vicente Vigueras Date: Wed, 20 Mar 2024 16:28:47 +0000 Subject: [PATCH 5/5] fix: deleted a file that doesn't make sense anymore --- .../LibraryManagementSystemTest.java | 36 ------------------- 1 file changed, 36 deletions(-) delete mode 100644 lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryManagementSystemTest.java diff --git a/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryManagementSystemTest.java b/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryManagementSystemTest.java deleted file mode 100644 index 0bd5af76..00000000 --- a/lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/LibraryManagementSystemTest/LibraryManagementSystemTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.codedifferently.lesson9.LibraryManagementSystemTest; - -import org.junit.jupiter.api.Test; - -import com.codedifferently.lesson9.LibraryManagementSystem.Book; -import com.codedifferently.lesson9.LibraryManagementSystem.Library; - - -public class LibraryManagementSystemTest { - -@Test -public void testAddBook() { - Library library = new Library(); // create a new library - - String title = "The Pragmatic Programmer, 20th Anniversary Edition"; - String isbn = "9780135957059"; - String author = "David Thomas, Andrew Hunt"; - int pages = 320; - - Book book = new Book(title,isbn,author,pages); // create a new book - - - library.addBook(book); // adds book to library - - /* addBook() method will be defined in library class - the logic inside addBook() will assign an element called book to a list - In my test, I'll need a way to check if an element is present within a list using assertThat() - - */ - - - -} - - -} \ No newline at end of file