Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: made library code and library test code #263

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.codedifferently.lesson9.KyvonThompsonlesson09;

public class Book {
// Book Characteristics
private Boolean checkedOut;
private String authors;
private String title;
private int pageNumber;
private float isbn;

// Book Constructor
public Book(Boolean checkedOut, String authors, String title, int pageNumber, float isbn) {

this.checkedOut = checkedOut;
this.authors = authors;
this.title = title;
this.pageNumber = pageNumber;
this.isbn = isbn;
}

// Getter for whether it was checked out
public Boolean getCheckedOut() {
return checkedOut;
}

// Setter for whether it was checked out
public void setCheckedOut(Boolean checkedOut) {
this.checkedOut = checkedOut;
}

// Getter for author(s) of the book
public String getAuthors() {
return authors;
}

// Setter for author(s) of the book
public void setAuthors(String authors) {
this.authors = authors;
}

// Getter for book title
public String getTitle() {
return title;
}

// Setter for book title
public void setTitle(String title) {
this.title = title;
}

// Getter for the # of pages
public int getPageNumber() {
return pageNumber;
}

// Setter for the # of pages
public void setPageNumbers(int pageNumber) {
this.pageNumber = pageNumber;
}

// Getter for the isbn
public float getIsbn() {
return isbn;
}

// Setter for isbn
public void setIsbn(float isbn) {
this.isbn = isbn;
}

public boolean checkOut() {
throw new UnsupportedOperationException("Unimplemented method 'checkOut'");
}

public void returnBook() {

throw new UnsupportedOperationException("Unimplemented method 'returnBook'");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.codedifferently.lesson9;

import com.codedifferently.lesson9.KyvonThompsonlesson09.Book;
import java.util.ArrayList;

public class Library {
ArrayList<Book> books;
ArrayList<Patron> patrons;

public Library() {
this.books = new ArrayList<>();
this.patrons = new ArrayList<>();
}

public void addBook(Book book) {
books.add(book);
}

public void removeBook(Book book) {
books.remove(book);
}

public void registerPatron(Patron patron) {
patrons.add(patron);
}

public boolean checkOutBook(Patron patron, Book book) {
if (books.contains(book) && patron.checkOutBook(book)) {
books.remove(book);
return true;
} else {
return false;
}
}

public boolean returnBook(Patron patron, Book book) {
if (patron.returnBook(book)) {
books.add(book);
return true;
} else {
return false;
}
}

public ArrayList<Book> getBooks() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getBooks'");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.codedifferently.lesson9;

import com.codedifferently.lesson9.KyvonThompsonlesson09.Book;
import java.util.ArrayList;

public class Patron {
// Patron information
private String patronName;
private ArrayList<Book> checkedOutBooks; // It's good practice to make fields private

// Patron constructor
public Patron(String patronName) {
this.patronName = patronName;
this.checkedOutBooks = new ArrayList<>();
}

// Getter for the Patron's name
public String getPatronName() {
return patronName;
}

// Setter for Patron's name
public void setPatronName(String patronName) {
this.patronName = patronName;
}

// Getter for the books the patron has checked out
public ArrayList<Book> getCheckedOutBooks() {
return checkedOutBooks;
}

// Method to add a book to the checkedOutBooks list
public void addBook(Book book) {
checkedOutBooks.add(book);
}

// Method to remove a book from the checkedOutBooks list
public boolean removeBook(Book book) {
return checkedOutBooks.remove(book);
}

// Optionally, if you really need a setter for replacing the whole list
public void setCheckedOutBooks(ArrayList<Book> checkedOutBooks) {
this.checkedOutBooks = checkedOutBooks;
}

public boolean checkOutBook(Book book) {
if (book.checkOut()) {
checkedOutBooks.add(book);
return true;
} else {
return false;
}
}

public boolean returnBook(Book book) {
if (checkedOutBooks.contains(book)) {
checkedOutBooks.remove(book);
book.returnBook();
return true;
} else {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.codedifferently.lesson9;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.codedifferently.lesson9.KyvonThompsonlesson09.Book;
import org.junit.jupiter.api.Test;

public class Booktest {

/** Test class for the {@link com.codedifferently.lesson9.LibraryManagementSystem.Book} class. */
class BookTest {

/** Test the constructor of the Book class. */
@Test
public void testBookConstructor() {
// Arrange
Boolean checkedOut = false;
String authors = "Frank Miller";
String title = "The Dark Knight Returns";
int pageNumber = 250;
float isbn = (float) 18.9595;

// Act
Book book = new Book(checkedOut, authors, title, pageNumber, isbn);

// Assert
assertEquals(title, book.getTitle());
assertEquals(isbn, book.getIsbn());
assertEquals(authors, book.getAuthors());
assertEquals(pageNumber, book.getPageNumber());
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.codedifferently.lesson9;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.codedifferently.lesson9.KyvonThompsonlesson09.Book;
import org.junit.jupiter.api.Test;

public class Librarytest {
private Library library;
private Book book;
private Patron patron;

@Before
public void setUp() {
library = new Library();
book = new Book(true, "Frank Miller", "The Dark Knight Returns", 250, (float) 18.9595);
library.addBook(book);
patron = new Patron("John Doe");
library.registerPatron(patron);
}

@Test
public void testAddBook() {
assertEquals(1, library.getBooks().size());
assertEquals(book, library.getBooks().get(0));
}

@Test
public void testRemoveBook() {
library.removeBook(book);
assertEquals(0, library.getBooks().size());
}

@Test
public void testCheckOutBook() {
library.checkOutBook(patron, book);
assertTrue(book.getCheckedOut());
assertTrue(patron.getCheckedOutBooks().contains(book));
}

private void assertTrue(boolean contains) {
throw new UnsupportedOperationException("Unimplemented method 'assertTrue'");
}

@Test
public void testReturnBook() {
library.checkOutBook(patron, book);
library.returnBook(patron, book);
assertFalse(book.getCheckedOut());
assertFalse(patron.getCheckedOutBooks().contains(book));
}

private void assertFalse(Boolean checkedOut) {
throw new UnsupportedOperationException("Unimplemented method 'assertFalse'");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class kyvonthompson test {

}
Loading