Skip to content

Commit 261cb50

Browse files
feat: adds Jada's created structure for Library Management System (#244)
* feat: Created structure for Library Management System * feat: Q * feat: quiz completed * Revert "feat: quiz completed" This reverts commit 5dae095. * fix: mistakes * Update Jadaslibrary.java * chore: deletes early quiz. * chore: moving files to correct package Signed-off-by: Anthony D. Mays <[email protected]> * chore: moving test file Signed-off-by: Anthony D. Mays <[email protected]> * fix: library test and impl Signed-off-by: Anthony D. Mays <[email protected]> --------- Signed-off-by: Anthony D. Mays <[email protected]> Co-authored-by: Anthony D. Mays <[email protected]> Co-authored-by: Anthony D. Mays <[email protected]>
1 parent e97c24c commit 261cb50

File tree

5 files changed

+214
-0
lines changed

5 files changed

+214
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.codedifferently.lesson9.JadasLibrary;
2+
3+
public class Book {
4+
private String title;
5+
private String isbn;
6+
private String author;
7+
private int numberOfPages;
8+
private boolean checkedOut;
9+
10+
// Constructor
11+
public Book(String title, String isbn, String author, int numberOfPages) {
12+
this.title = title;
13+
this.isbn = isbn;
14+
this.author = author;
15+
this.numberOfPages = numberOfPages;
16+
this.checkedOut = false;
17+
}
18+
19+
// Getters and setters
20+
public String getTitle() {
21+
return title;
22+
}
23+
24+
public void setTitle(String title) {
25+
this.title = title;
26+
}
27+
28+
public String getIsbn() {
29+
return isbn;
30+
}
31+
32+
public void setIsbn(String isbn) {
33+
this.isbn = isbn;
34+
}
35+
36+
public String getAuthor() {
37+
return author;
38+
}
39+
40+
public void setAuthor(String author) {
41+
this.author = author;
42+
}
43+
44+
public int getNumberOfPages() {
45+
return numberOfPages;
46+
}
47+
48+
public void setNumberOfPages(int numberOfPages) {
49+
this.numberOfPages = numberOfPages;
50+
}
51+
52+
public boolean isCheckedOut() {
53+
return checkedOut;
54+
}
55+
56+
public void setCheckedOut(boolean checkedOut) {
57+
this.checkedOut = checkedOut;
58+
}
59+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.codedifferently.lesson9.JadasLibrary;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Jadaslibrary {
7+
private List<Book> books;
8+
private List<Patron> patrons;
9+
10+
public Jadaslibrary() {
11+
this.books = new ArrayList<>();
12+
this.patrons = new ArrayList<>();
13+
}
14+
15+
public void addBook(Book book) {
16+
books.add(book);
17+
}
18+
19+
public void removeBook(Book book) {
20+
books.remove(book);
21+
}
22+
23+
public void registerPatron(Patron patron) {
24+
patrons.add(patron);
25+
}
26+
27+
public void checkOutBook(Book book, Patron patron) {
28+
if (books.contains(book) && !book.isCheckedOut()) {
29+
book.setCheckedOut(true);
30+
patron.getCheckedOutBooks().add(book);
31+
}
32+
}
33+
34+
public void returnBook(Book book, Patron patron) {
35+
if (patron.getCheckedOutBooks().contains(book)) {
36+
book.setCheckedOut(false);
37+
patron.getCheckedOutBooks().remove(book);
38+
}
39+
}
40+
41+
public List<Book> getBooks() {
42+
return new ArrayList<>(books);
43+
}
44+
45+
public List<Patron> getPatrons() {
46+
return new ArrayList<>(patrons);
47+
}
48+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codedifferently.lesson9.JadasLibrary;
2+
3+
import java.util.List;
4+
5+
public interface Patron {
6+
7+
void checkOutBook(Book book);
8+
9+
void returnBook(Book book);
10+
11+
List<Book> getCheckedOutBooks();
12+
13+
String getName();
14+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.codedifferently.lesson9.JadasLibrary;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class PatronImpl implements Patron {
7+
private String name;
8+
private List<Book> checkedOutBooks;
9+
10+
public PatronImpl(String name) {
11+
this.name = name;
12+
this.checkedOutBooks = new ArrayList<>();
13+
}
14+
15+
@Override
16+
public void checkOutBook(Book book) {
17+
checkedOutBooks.add(book);
18+
}
19+
20+
@Override
21+
public void returnBook(Book book) {
22+
checkedOutBooks.remove(book);
23+
}
24+
25+
@Override
26+
public List<Book> getCheckedOutBooks() {
27+
return checkedOutBooks;
28+
}
29+
30+
@Override
31+
public String getName() {
32+
return name;
33+
}
34+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.codedifferently.lesson9.JadasLibrary;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class JadaslibraryTest {
9+
10+
@Test
11+
public void testAddBookToLibrary() {
12+
Jadaslibrary library = new Jadaslibrary();
13+
Book book = new Book("The Bluest Eye", "978-0140088298", "Toni Morrison", 226);
14+
library.addBook(book);
15+
assertTrue(library.getBooks().contains(book));
16+
}
17+
18+
@Test
19+
public void testRemoveBookFromLibrary() {
20+
Jadaslibrary library = new Jadaslibrary();
21+
Book book = new Book("Sula", "978-0452260108", "Toni Morrison", 193);
22+
library.addBook(book);
23+
library.removeBook(book);
24+
assertFalse(library.getBooks().contains(book));
25+
}
26+
27+
@Test
28+
public void testRegisterPatron() {
29+
Jadaslibrary library = new Jadaslibrary();
30+
Patron patron = new PatronImpl("Stacy Law");
31+
library.registerPatron(patron);
32+
assertTrue(library.getPatrons().contains(patron));
33+
}
34+
35+
@Test
36+
public void testCheckOutBook() {
37+
Jadaslibrary library = new Jadaslibrary();
38+
Book book = new Book("Liden Hills", "978-3293006853", "Gloria Naylor", 320);
39+
Patron patron = new PatronImpl("Clay Brown");
40+
library.addBook(book);
41+
library.registerPatron(patron);
42+
library.checkOutBook(book, patron);
43+
assertTrue(patron.getCheckedOutBooks().contains(book));
44+
assertTrue(book.isCheckedOut());
45+
}
46+
47+
@Test
48+
public void testReturnBook() {
49+
Jadaslibrary library = new Jadaslibrary();
50+
Book book = new Book("Nobody Knows My Name", "978-0679744733", "James Baldwin", 256);
51+
Patron patron = new PatronImpl("Bob Willson");
52+
library.addBook(book);
53+
library.registerPatron(patron);
54+
library.checkOutBook(book, patron);
55+
library.returnBook(book, patron);
56+
assertFalse(patron.getCheckedOutBooks().contains(book));
57+
assertFalse(book.isCheckedOut());
58+
}
59+
}

0 commit comments

Comments
 (0)