Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds Lesson 09 classes + test (mekhi) (#246)
- Loading branch information
Showing
6 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/library/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.codedifferently.lesson9.library; | ||
|
||
import java.util.List; | ||
|
||
/** Represents a Marvel comic in the library. */ | ||
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) { | ||
this.title = title; | ||
this.isbn = isbn; | ||
this.authors = authors; | ||
this.numPages = numPages; | ||
this.checkedOut = false; | ||
} | ||
|
||
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; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/library/Library.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.codedifferently.lesson9.library; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** Represents a library with Marvel comics and patrons. */ | ||
public class Library { | ||
private List<Book> marvelComics; | ||
private List<Patron> patrons; | ||
|
||
public Library() { | ||
this.marvelComics = new ArrayList<>(); | ||
this.patrons = new ArrayList<>(); | ||
} | ||
|
||
public void addComic(Book comic) { | ||
marvelComics.add(comic); | ||
} | ||
|
||
public void removeComic(Book comic) { | ||
marvelComics.remove(comic); | ||
} | ||
|
||
public void registerPatron(Patron patron) { | ||
patrons.add(patron); | ||
} | ||
|
||
public void checkOutComic(Book comic, Patron patron) { | ||
if (marvelComics.contains(comic) && patrons.contains(patron)) { | ||
patron.checkOutBook(comic); | ||
} else { | ||
System.out.println("Marvel comic or patron not found in the library."); | ||
} | ||
} | ||
|
||
public void returnComic(Book comic, Patron patron) { | ||
if (marvelComics.contains(comic) && patrons.contains(patron)) { | ||
patron.returnBook(comic); | ||
} else { | ||
System.out.println("Marvel comic or patron not found in the library."); | ||
} | ||
} | ||
|
||
public List<Book> getMarvelComics() { | ||
return marvelComics; | ||
} | ||
|
||
public void setMarvelComics(List<Book> marvelComics) { | ||
this.marvelComics = marvelComics; | ||
} | ||
|
||
public List<Patron> getPatrons() { | ||
return patrons; | ||
} | ||
|
||
public void setPatrons(List<Patron> patrons) { | ||
this.patrons = patrons; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/library/Patron.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.codedifferently.lesson9.library; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** Represents a patron of the library. */ | ||
public class Patron { | ||
private String name; | ||
private List<Book> checkedOutBooks; | ||
|
||
public Patron(String name) { | ||
this.name = name; | ||
this.checkedOutBooks = new ArrayList<>(); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public List<Book> getCheckedOutBooks() { | ||
return checkedOutBooks; | ||
} | ||
|
||
public void setCheckedOutBooks(List<Book> checkedOutBooks) { | ||
this.checkedOutBooks = checkedOutBooks; | ||
} | ||
|
||
public void checkOutBook(Book book) { | ||
if (!book.isCheckedOut()) { | ||
book.setCheckedOut(true); | ||
checkedOutBooks.add(book); | ||
} else { | ||
System.out.println("Marvel comic is already checked out."); | ||
} | ||
} | ||
|
||
public void returnBook(Book book) { | ||
if (checkedOutBooks.contains(book)) { | ||
book.setCheckedOut(false); | ||
checkedOutBooks.remove(book); | ||
} else { | ||
System.out.println("Marvel comic is not checked out by this patron."); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/test/BookTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.codedifferently.lesson9.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.codedifferently.lesson9.library.Book; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class BookTest { | ||
|
||
@Test | ||
public void testComicConstructor() { | ||
List<String> authors = new ArrayList<>(); | ||
authors.add("Stan Lee"); | ||
Book comic = new Book("Spider-Man", "9781302920528", authors, 200); | ||
assertEquals("Spider-Man", comic.getTitle()); | ||
assertEquals("9781302920528", comic.getIsbn()); | ||
assertEquals(authors, comic.getAuthors()); | ||
assertEquals(200, comic.getNumPages()); | ||
assertFalse(comic.isCheckedOut()); | ||
} | ||
|
||
@Test | ||
public void testSetCheckedOut() { | ||
Book comic = new Book("Iron Man", "9781302921556", List.of("Stan Lee"), 150); | ||
assertFalse(comic.isCheckedOut()); | ||
comic.setCheckedOut(true); | ||
assertTrue(comic.isCheckedOut()); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/test/LibraryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.codedifferently.lesson9.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.codedifferently.lesson9.library.Book; | ||
import com.codedifferently.lesson9.library.Library; | ||
import com.codedifferently.lesson9.library.Patron; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class LibraryTest { | ||
private Library library; | ||
private Book comic; | ||
private Patron patron; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
library = new Library(); | ||
comic = new Book("X-Men", "9781302921785", List.of("Stan Lee"), 250); | ||
patron = new Patron("Natasha Romanoff"); | ||
} | ||
|
||
@Test | ||
public void testAddComic() { | ||
library.addComic(comic); | ||
assertEquals(1, library.getMarvelComics().size()); | ||
assertTrue(library.getMarvelComics().contains(comic)); | ||
} | ||
|
||
@Test | ||
public void testRemoveComic() { | ||
library.addComic(comic); | ||
assertEquals(1, library.getMarvelComics().size()); | ||
library.removeComic(comic); | ||
assertEquals(0, library.getMarvelComics().size()); | ||
} | ||
|
||
@Test | ||
public void testRegisterPatron() { | ||
library.registerPatron(patron); | ||
assertEquals(1, library.getPatrons().size()); | ||
assertTrue(library.getPatrons().contains(patron)); | ||
} | ||
|
||
@Test | ||
public void testCheckOutComic() { | ||
library.addComic(comic); | ||
library.registerPatron(patron); | ||
library.checkOutComic(comic, patron); | ||
assertEquals(1, patron.getCheckedOutBooks().size()); | ||
assertTrue(patron.getCheckedOutBooks().contains(comic)); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
lesson_09/oop/oop_app/src/test/java/com/codedifferently/lesson9/test/PatronTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.codedifferently.lesson9.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import com.codedifferently.lesson9.library.Book; | ||
import com.codedifferently.lesson9.library.Patron; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class PatronTest { | ||
|
||
@Test | ||
public void testPatronConstructor() { | ||
Patron patron = new Patron("Peter Parker"); | ||
assertEquals("Peter Parker", patron.getName()); | ||
assertEquals(new ArrayList<Book>(), patron.getCheckedOutBooks()); | ||
} | ||
|
||
@Test | ||
public void testCheckOutComic() { | ||
Patron patron = new Patron("Tony Stark"); | ||
Book comic = new Book("Avengers", "9781302921686", List.of("Stan Lee"), 300); | ||
patron.checkOutBook(comic); | ||
assertEquals(1, patron.getCheckedOutBooks().size()); | ||
} | ||
} |