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

Jamira lesson09 #285

Merged
merged 5 commits into from
Mar 22, 2024
Merged
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,66 @@
package com.codedifferently.lesson9.jamira;

import java.util.List;
import java.util.Objects;

public class Book {
private String title;
private String isbn;
private List<String> authors;
private int numPages;
private boolean checkedOut;

public Book(String title, String isbn, List<String> authors, int numPages, boolean checkedOut) {
this.title = title;
this.isbn = isbn;
this.authors = authors;
this.numPages = numPages;
this.checkedOut = checkedOut; // Initially not checked out
}

// Getters and setters
public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public List<String> getAuthors() {
return authors;
}

public void setAuthors(List<String> authors) {
this.authors = authors;
}

public int getNumPages() {
return numPages;
}

public void setNumPages(int numPages) {
this.numPages = numPages;
}

public boolean isCheckedOut() {
return checkedOut;
}

public void setCheckedOut(boolean checkedOut) {
this.checkedOut = checkedOut;
}

@Override
public int hashCode() {
return Objects.hash(this.isbn);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.codedifferently.lesson9.jamira;

import java.util.ArrayList;
import java.util.List;

public class Library {
private List<Book> books;
private List<Patron> patrons;

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

// Getters and setters
public List<Book> getBooks() {
return books;
}

public void setBooks(List<Book> books) {
this.books = books;
}

public List<Patron> getPatrons() {
return patrons;
}

public void setPatrons(List<Patron> patrons) {
this.patrons = patrons;
}

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

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

// Methods to add/remove books, register new patrons, check out and return books
// Add appropriate methods as needed...
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.codedifferently.lesson9.jamira;

import java.util.List;

public class Patron {
private String name;
private List<Book> checkedOutBooks;

public Patron(String name, List<Book> checkedOutBooks) {
this.name = name;
this.checkedOutBooks = checkedOutBooks;
}

public String getName() {
return name;
}

public List<Book> getCheckedOutBooks() {
return checkedOutBooks;
}

public void addCheckedOutBook(Book newBook) {
checkedOutBooks.add(newBook);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.codedifferently.lesson9.jamira;

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

import java.util.List;
import org.junit.jupiter.api.Test;

public class BookTest {

@Test
public void testConstructorAndGetters() {
Book book = new Book("Title", "ISBN123", List.of("author"), 200, false);
assertEquals("Title", book.getTitle());
assertEquals("ISBN123", book.getIsbn());
assertEquals(List.of("author"), book.getAuthors());
assertEquals(200, book.getNumPages());
assertFalse(book.isCheckedOut());
}

@Test
public void testSetters() {
Book java = new Book("Title", "ISBN123", List.of("author"), 200, false);
java.setTitle("Title2");
java.setIsbn("ISBN456");
java.setAuthors(List.of("authors"));
java.setNumPages(300);
java.setCheckedOut(false);
assertEquals("Title2", java.getTitle());
assertEquals("ISBN456", java.getIsbn());
assertEquals(List.of("authors"), java.getAuthors());
assertEquals(300, java.getNumPages());
assertFalse(java.isCheckedOut());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.codedifferently.lesson9.jamira;

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

import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class LibraryTest {
private Library library;

@BeforeEach
public void setUp() {
List<Book> books = new ArrayList<>();
books.add(new Book("Book1", "ISBN1", List.of("Author1"), 100, true));
books.add(new Book("Book2", "ISBN2", List.of("Author2"), 150, true));

library = new Library();
library.setBooks(books);
}

@Test
public void testAddBook() {
Book newBook = new Book("New Book", "New ISBN", List.of("New Author"), 200, false);
Book book1 = library.getBooks().get(0);
Book book2 = library.getBooks().get(1);

library.addBook(newBook);

List<Book> expectedBooks = new ArrayList<>();
expectedBooks.add(book1);
expectedBooks.add(book2);
expectedBooks.add(newBook);

assertEquals(expectedBooks.size(), library.getBooks().size());
assertEquals(expectedBooks, library.getBooks());
}

@Test
public void testRemoveBook() {
Book book1 = library.getBooks().get(0);
Book book2 = library.getBooks().get(1);
library.removeBook(book1);

List<Book> expectedBooks = new ArrayList<>();
expectedBooks.add(book2);

assertEquals(expectedBooks.size(), library.getBooks().size());
assertEquals(expectedBooks, library.getBooks());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.codedifferently.lesson9.jamira;

import java.util.ArrayList;
import java.util.List;

public class PatronTest {
public static void main(String[] args) {
// Create some sample data
List<Book> checkedOutBooks = new ArrayList<>();
checkedOutBooks.add(new Book("Title1", "ISBN1", List.of("Author1"), 100, true));
checkedOutBooks.add(new Book("Title2", "ISBN2", List.of("Author2"), 150, true));

// Create a patron object
Patron patron = new Patron("John Doe", checkedOutBooks);

// Test the getName() method
System.out.println("Patron's name: " + patron.getName());

// Test the getCheckedOutBooks() method
System.out.println("Checked out books:");
for (Book book : patron.getCheckedOutBooks()) {
System.out.println(book.getTitle() + " - " + book.getIsbn());
}

// Test the addCheckedOutBook() method
Book newBook = new Book("New Title", "New ISBN", List.of("New Author"), 200, true);
patron.addCheckedOutBook(newBook);

// Print the updated list of checked out books
System.out.println("Updated checked out books:");
for (Book book : patron.getCheckedOutBooks()) {
System.out.println(book.getTitle() + " - " + book.getIsbn());
}
}
}