Skip to content

Commit e4eab0e

Browse files
authored
feat: adds Lesson 09 classes + test (mekhi) (#246)
1 parent 261cb50 commit e4eab0e

File tree

6 files changed

+283
-0
lines changed

6 files changed

+283
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.codedifferently.lesson9.library;
2+
3+
import java.util.List;
4+
5+
/** Represents a Marvel comic in the library. */
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) {
14+
this.title = title;
15+
this.isbn = isbn;
16+
this.authors = authors;
17+
this.numPages = numPages;
18+
this.checkedOut = false;
19+
}
20+
21+
public String getTitle() {
22+
return title;
23+
}
24+
25+
public void setTitle(String title) {
26+
this.title = title;
27+
}
28+
29+
public String getIsbn() {
30+
return isbn;
31+
}
32+
33+
public void setIsbn(String isbn) {
34+
this.isbn = isbn;
35+
}
36+
37+
public List<String> getAuthors() {
38+
return authors;
39+
}
40+
41+
public void setAuthors(List<String> authors) {
42+
this.authors = authors;
43+
}
44+
45+
public int getNumPages() {
46+
return numPages;
47+
}
48+
49+
public void setNumPages(int numPages) {
50+
this.numPages = numPages;
51+
}
52+
53+
public boolean isCheckedOut() {
54+
return checkedOut;
55+
}
56+
57+
public void setCheckedOut(boolean checkedOut) {
58+
this.checkedOut = checkedOut;
59+
}
60+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.codedifferently.lesson9.library;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/** Represents a library with Marvel comics and patrons. */
7+
public class Library {
8+
private List<Book> marvelComics;
9+
private List<Patron> patrons;
10+
11+
public Library() {
12+
this.marvelComics = new ArrayList<>();
13+
this.patrons = new ArrayList<>();
14+
}
15+
16+
public void addComic(Book comic) {
17+
marvelComics.add(comic);
18+
}
19+
20+
public void removeComic(Book comic) {
21+
marvelComics.remove(comic);
22+
}
23+
24+
public void registerPatron(Patron patron) {
25+
patrons.add(patron);
26+
}
27+
28+
public void checkOutComic(Book comic, Patron patron) {
29+
if (marvelComics.contains(comic) && patrons.contains(patron)) {
30+
patron.checkOutBook(comic);
31+
} else {
32+
System.out.println("Marvel comic or patron not found in the library.");
33+
}
34+
}
35+
36+
public void returnComic(Book comic, Patron patron) {
37+
if (marvelComics.contains(comic) && patrons.contains(patron)) {
38+
patron.returnBook(comic);
39+
} else {
40+
System.out.println("Marvel comic or patron not found in the library.");
41+
}
42+
}
43+
44+
public List<Book> getMarvelComics() {
45+
return marvelComics;
46+
}
47+
48+
public void setMarvelComics(List<Book> marvelComics) {
49+
this.marvelComics = marvelComics;
50+
}
51+
52+
public List<Patron> getPatrons() {
53+
return patrons;
54+
}
55+
56+
public void setPatrons(List<Patron> patrons) {
57+
this.patrons = patrons;
58+
}
59+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.codedifferently.lesson9.library;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/** Represents a patron of the library. */
7+
public class Patron {
8+
private String name;
9+
private List<Book> checkedOutBooks;
10+
11+
public Patron(String name) {
12+
this.name = name;
13+
this.checkedOutBooks = new ArrayList<>();
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public List<Book> getCheckedOutBooks() {
25+
return checkedOutBooks;
26+
}
27+
28+
public void setCheckedOutBooks(List<Book> checkedOutBooks) {
29+
this.checkedOutBooks = checkedOutBooks;
30+
}
31+
32+
public void checkOutBook(Book book) {
33+
if (!book.isCheckedOut()) {
34+
book.setCheckedOut(true);
35+
checkedOutBooks.add(book);
36+
} else {
37+
System.out.println("Marvel comic is already checked out.");
38+
}
39+
}
40+
41+
public void returnBook(Book book) {
42+
if (checkedOutBooks.contains(book)) {
43+
book.setCheckedOut(false);
44+
checkedOutBooks.remove(book);
45+
} else {
46+
System.out.println("Marvel comic is not checked out by this patron.");
47+
}
48+
}
49+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.codedifferently.lesson9.test;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import com.codedifferently.lesson9.library.Book;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import org.junit.jupiter.api.Test;
11+
12+
public class BookTest {
13+
14+
@Test
15+
public void testComicConstructor() {
16+
List<String> authors = new ArrayList<>();
17+
authors.add("Stan Lee");
18+
Book comic = new Book("Spider-Man", "9781302920528", authors, 200);
19+
assertEquals("Spider-Man", comic.getTitle());
20+
assertEquals("9781302920528", comic.getIsbn());
21+
assertEquals(authors, comic.getAuthors());
22+
assertEquals(200, comic.getNumPages());
23+
assertFalse(comic.isCheckedOut());
24+
}
25+
26+
@Test
27+
public void testSetCheckedOut() {
28+
Book comic = new Book("Iron Man", "9781302921556", List.of("Stan Lee"), 150);
29+
assertFalse(comic.isCheckedOut());
30+
comic.setCheckedOut(true);
31+
assertTrue(comic.isCheckedOut());
32+
}
33+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.codedifferently.lesson9.test;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import com.codedifferently.lesson9.library.Book;
7+
import com.codedifferently.lesson9.library.Library;
8+
import com.codedifferently.lesson9.library.Patron;
9+
import java.util.List;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class LibraryTest {
14+
private Library library;
15+
private Book comic;
16+
private Patron patron;
17+
18+
@BeforeEach
19+
public void setup() {
20+
library = new Library();
21+
comic = new Book("X-Men", "9781302921785", List.of("Stan Lee"), 250);
22+
patron = new Patron("Natasha Romanoff");
23+
}
24+
25+
@Test
26+
public void testAddComic() {
27+
library.addComic(comic);
28+
assertEquals(1, library.getMarvelComics().size());
29+
assertTrue(library.getMarvelComics().contains(comic));
30+
}
31+
32+
@Test
33+
public void testRemoveComic() {
34+
library.addComic(comic);
35+
assertEquals(1, library.getMarvelComics().size());
36+
library.removeComic(comic);
37+
assertEquals(0, library.getMarvelComics().size());
38+
}
39+
40+
@Test
41+
public void testRegisterPatron() {
42+
library.registerPatron(patron);
43+
assertEquals(1, library.getPatrons().size());
44+
assertTrue(library.getPatrons().contains(patron));
45+
}
46+
47+
@Test
48+
public void testCheckOutComic() {
49+
library.addComic(comic);
50+
library.registerPatron(patron);
51+
library.checkOutComic(comic, patron);
52+
assertEquals(1, patron.getCheckedOutBooks().size());
53+
assertTrue(patron.getCheckedOutBooks().contains(comic));
54+
}
55+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.codedifferently.lesson9.test;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.codedifferently.lesson9.library.Book;
6+
import com.codedifferently.lesson9.library.Patron;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class PatronTest {
12+
13+
@Test
14+
public void testPatronConstructor() {
15+
Patron patron = new Patron("Peter Parker");
16+
assertEquals("Peter Parker", patron.getName());
17+
assertEquals(new ArrayList<Book>(), patron.getCheckedOutBooks());
18+
}
19+
20+
@Test
21+
public void testCheckOutComic() {
22+
Patron patron = new Patron("Tony Stark");
23+
Book comic = new Book("Avengers", "9781302921686", List.of("Stan Lee"), 300);
24+
patron.checkOutBook(comic);
25+
assertEquals(1, patron.getCheckedOutBooks().size());
26+
}
27+
}

0 commit comments

Comments
 (0)