Skip to content

Commit 97dd93b

Browse files
feat: adds Jamira lesson09 response (#285)
* feat:added laptop class and test * Feat: updated Lesson 09 * Feat: added Book, Patron, and Llibrary File with Test * chore: removes files not relevant to assignment Signed-off-by: Anthony D. Mays <[email protected]> * fix: code and tests Signed-off-by: Anthony D. Mays <[email protected]> --------- Signed-off-by: Anthony D. Mays <[email protected]> Co-authored-by: Anthony D. Mays <[email protected]>
1 parent dae48c5 commit 97dd93b

File tree

6 files changed

+256
-0
lines changed

6 files changed

+256
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.codedifferently.lesson9.jamira;
2+
3+
import java.util.List;
4+
import java.util.Objects;
5+
6+
public class Book {
7+
private String title;
8+
private String isbn;
9+
private List<String> authors;
10+
private int numPages;
11+
private boolean checkedOut;
12+
13+
public Book(String title, String isbn, List<String> authors, int numPages, boolean checkedOut) {
14+
this.title = title;
15+
this.isbn = isbn;
16+
this.authors = authors;
17+
this.numPages = numPages;
18+
this.checkedOut = checkedOut; // Initially not checked out
19+
}
20+
21+
// Getters and setters
22+
public String getTitle() {
23+
return title;
24+
}
25+
26+
public void setTitle(String title) {
27+
this.title = title;
28+
}
29+
30+
public String getIsbn() {
31+
return isbn;
32+
}
33+
34+
public void setIsbn(String isbn) {
35+
this.isbn = isbn;
36+
}
37+
38+
public List<String> getAuthors() {
39+
return authors;
40+
}
41+
42+
public void setAuthors(List<String> authors) {
43+
this.authors = authors;
44+
}
45+
46+
public int getNumPages() {
47+
return numPages;
48+
}
49+
50+
public void setNumPages(int numPages) {
51+
this.numPages = numPages;
52+
}
53+
54+
public boolean isCheckedOut() {
55+
return checkedOut;
56+
}
57+
58+
public void setCheckedOut(boolean checkedOut) {
59+
this.checkedOut = checkedOut;
60+
}
61+
62+
@Override
63+
public int hashCode() {
64+
return Objects.hash(this.isbn);
65+
}
66+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.codedifferently.lesson9.jamira;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Library {
7+
private List<Book> books;
8+
private List<Patron> patrons;
9+
10+
// Constructor
11+
public Library() {
12+
this.books = new ArrayList<>();
13+
this.patrons = new ArrayList<>();
14+
}
15+
16+
// Getters and setters
17+
public List<Book> getBooks() {
18+
return books;
19+
}
20+
21+
public void setBooks(List<Book> books) {
22+
this.books = books;
23+
}
24+
25+
public List<Patron> getPatrons() {
26+
return patrons;
27+
}
28+
29+
public void setPatrons(List<Patron> patrons) {
30+
this.patrons = patrons;
31+
}
32+
33+
public void removeBook(Book bookToRemove) {
34+
this.books.remove(bookToRemove);
35+
}
36+
37+
public void addBook(Book newBook) {
38+
this.books.add(newBook);
39+
}
40+
41+
// Methods to add/remove books, register new patrons, check out and return books
42+
// Add appropriate methods as needed...
43+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.codedifferently.lesson9.jamira;
2+
3+
import java.util.List;
4+
5+
public class Patron {
6+
private String name;
7+
private List<Book> checkedOutBooks;
8+
9+
public Patron(String name, List<Book> checkedOutBooks) {
10+
this.name = name;
11+
this.checkedOutBooks = checkedOutBooks;
12+
}
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public List<Book> getCheckedOutBooks() {
19+
return checkedOutBooks;
20+
}
21+
22+
public void addCheckedOutBook(Book newBook) {
23+
checkedOutBooks.add(newBook);
24+
}
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.codedifferently.lesson9.jamira;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class BookTest {
10+
11+
@Test
12+
public void testConstructorAndGetters() {
13+
Book book = new Book("Title", "ISBN123", List.of("author"), 200, false);
14+
assertEquals("Title", book.getTitle());
15+
assertEquals("ISBN123", book.getIsbn());
16+
assertEquals(List.of("author"), book.getAuthors());
17+
assertEquals(200, book.getNumPages());
18+
assertFalse(book.isCheckedOut());
19+
}
20+
21+
@Test
22+
public void testSetters() {
23+
Book java = new Book("Title", "ISBN123", List.of("author"), 200, false);
24+
java.setTitle("Title2");
25+
java.setIsbn("ISBN456");
26+
java.setAuthors(List.of("authors"));
27+
java.setNumPages(300);
28+
java.setCheckedOut(false);
29+
assertEquals("Title2", java.getTitle());
30+
assertEquals("ISBN456", java.getIsbn());
31+
assertEquals(List.of("authors"), java.getAuthors());
32+
assertEquals(300, java.getNumPages());
33+
assertFalse(java.isCheckedOut());
34+
}
35+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.codedifferently.lesson9.jamira;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class LibraryTest {
11+
private Library library;
12+
13+
@BeforeEach
14+
public void setUp() {
15+
List<Book> books = new ArrayList<>();
16+
books.add(new Book("Book1", "ISBN1", List.of("Author1"), 100, true));
17+
books.add(new Book("Book2", "ISBN2", List.of("Author2"), 150, true));
18+
19+
library = new Library();
20+
library.setBooks(books);
21+
}
22+
23+
@Test
24+
public void testAddBook() {
25+
Book newBook = new Book("New Book", "New ISBN", List.of("New Author"), 200, false);
26+
Book book1 = library.getBooks().get(0);
27+
Book book2 = library.getBooks().get(1);
28+
29+
library.addBook(newBook);
30+
31+
List<Book> expectedBooks = new ArrayList<>();
32+
expectedBooks.add(book1);
33+
expectedBooks.add(book2);
34+
expectedBooks.add(newBook);
35+
36+
assertEquals(expectedBooks.size(), library.getBooks().size());
37+
assertEquals(expectedBooks, library.getBooks());
38+
}
39+
40+
@Test
41+
public void testRemoveBook() {
42+
Book book1 = library.getBooks().get(0);
43+
Book book2 = library.getBooks().get(1);
44+
library.removeBook(book1);
45+
46+
List<Book> expectedBooks = new ArrayList<>();
47+
expectedBooks.add(book2);
48+
49+
assertEquals(expectedBooks.size(), library.getBooks().size());
50+
assertEquals(expectedBooks, library.getBooks());
51+
}
52+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.codedifferently.lesson9.jamira;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class PatronTest {
7+
public static void main(String[] args) {
8+
// Create some sample data
9+
List<Book> checkedOutBooks = new ArrayList<>();
10+
checkedOutBooks.add(new Book("Title1", "ISBN1", List.of("Author1"), 100, true));
11+
checkedOutBooks.add(new Book("Title2", "ISBN2", List.of("Author2"), 150, true));
12+
13+
// Create a patron object
14+
Patron patron = new Patron("John Doe", checkedOutBooks);
15+
16+
// Test the getName() method
17+
System.out.println("Patron's name: " + patron.getName());
18+
19+
// Test the getCheckedOutBooks() method
20+
System.out.println("Checked out books:");
21+
for (Book book : patron.getCheckedOutBooks()) {
22+
System.out.println(book.getTitle() + " - " + book.getIsbn());
23+
}
24+
25+
// Test the addCheckedOutBook() method
26+
Book newBook = new Book("New Title", "New ISBN", List.of("New Author"), 200, true);
27+
patron.addCheckedOutBook(newBook);
28+
29+
// Print the updated list of checked out books
30+
System.out.println("Updated checked out books:");
31+
for (Book book : patron.getCheckedOutBooks()) {
32+
System.out.println(book.getTitle() + " - " + book.getIsbn());
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)