diff --git a/src/main/java/com/booleanuk/core/Article.java b/src/main/java/com/booleanuk/core/Article.java index 1e359b0..16b823c 100644 --- a/src/main/java/com/booleanuk/core/Article.java +++ b/src/main/java/com/booleanuk/core/Article.java @@ -1,35 +1,13 @@ package com.booleanuk.core; -public class Article { - String title; - - boolean onLoan = false; - - public Article(String title) { - this.title = title; - } - - public boolean isOnLoan() { - return onLoan; - } - - public String checkIn() { - if (!this.isOnLoan()) { - return "item is not currently on loan"; - } - - this.onLoan = false; - - return "item has been checked in"; +public class Article extends Item{ + private Author author; + public Article(String title, Author author){ + super(title); + this.author = author; } - public String checkOut() { - if (this.isOnLoan()) { - return "item is currently on loan"; - } - - this.onLoan = true; - - return "item has been checked out"; + public Author getAuthor() { + return author; } } diff --git a/src/main/java/com/booleanuk/core/Author.java b/src/main/java/com/booleanuk/core/Author.java new file mode 100644 index 0000000..9d1b21d --- /dev/null +++ b/src/main/java/com/booleanuk/core/Author.java @@ -0,0 +1,25 @@ +package com.booleanuk.core; + +public class Author { + private String author; + public String contact; + public String website; + + public Author(String author, String contact, String website){ + this.author = author; + this.contact = contact; + this.website = website; + } + + public String getAuthor() { + return author; + } + + public String getContact() { + return contact; + } + + public String getWebsite() { + return website; + } +} diff --git a/src/main/java/com/booleanuk/core/Book.java b/src/main/java/com/booleanuk/core/Book.java index 9261f65..89a78a7 100644 --- a/src/main/java/com/booleanuk/core/Book.java +++ b/src/main/java/com/booleanuk/core/Book.java @@ -1,35 +1,14 @@ package com.booleanuk.core; -public class Book { - String title; +public class Book extends Item { + private Author author; - boolean onLoan = false; - - public Book(String title) { - this.title = title; - } - - public boolean isOnLoan() { - return onLoan; + public Book(String tile, Author author){ + super(tile); + this.author = author; } - public String checkIn() { - if (!this.isOnLoan()) { - return "item is not currently on loan"; - } - - this.onLoan = false; - - return "item has been checked in"; - } - - public String checkOut() { - if (this.isOnLoan()) { - return "item is currently on loan"; - } - - this.onLoan = true; - - return "item has been checked out"; + public Author getAuthor() { + return author; } } diff --git a/src/main/java/com/booleanuk/core/Item.java b/src/main/java/com/booleanuk/core/Item.java new file mode 100644 index 0000000..be3f030 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Item.java @@ -0,0 +1,29 @@ +package com.booleanuk.core; + +public class Item { + String title; + Boolean onLoan = false; + + public Item(String title){ + this.title = title; + } + public boolean isOnLoan(){ + return onLoan; + } + public String checkIn() { + if (!this.onLoan){ + return "item is not currently on loan"; + } + this.onLoan = false; + + return "item has been checked in"; + } + + public String checkOut() { + if (this.onLoan){ + return "item is currently on loan"; + } + this.onLoan = true; + return "item has been checked out"; + } +} diff --git a/src/main/java/com/booleanuk/core/Library.java b/src/main/java/com/booleanuk/core/Library.java index f03ecf0..b8c7eec 100644 --- a/src/main/java/com/booleanuk/core/Library.java +++ b/src/main/java/com/booleanuk/core/Library.java @@ -1,92 +1,36 @@ package com.booleanuk.core; +import java.util.ArrayList; import java.util.List; public class Library { - List
articles; - List books; - List newspapers; + List libraryItems; - public void addToStock(Article item) { - this.articles.add(item); + public Library(){ + this.libraryItems = new ArrayList<>(); } - public void addToStock(Book item) { - this.books.add(item); + public void addToStock(Item item){ + this.libraryItems.add(item); } - - public void addToStock(Newspaper item) { - this.newspapers.add(item); - } - - // The following methods may contain code that you are unfamiliar with. The strange syntax of article -> something - // is called a lambda expression (https://www.w3schools.com/java/java_lambda.asp) - public String checkInArticle(String title) { - List
filtered = this.articles.stream() - .filter(article -> article.title.equals(title)) - .toList(); - - if (filtered.size() < 1) { - return "item is not part of the library's collection"; - } - - return filtered.get(0).checkIn(); - } - - public String checkOutArticle(String title) { - List
filtered = this.articles.stream() - .filter(article -> article.title.equals(title)) - .toList(); - - if (filtered.size() < 1) { - return "item is not part of the library's collection"; - } - - return filtered.get(0).checkOut(); - } - - public String checkInBook(String title) { - List filtered = this.books.stream() - .filter(book -> book.title.equals(title)) - .toList(); - - if (filtered.size() < 1) { - return "item is not part of the library's collection"; - } - - return filtered.get(0).checkIn(); - } - - public String checkOutBook(String title) { - List filtered = this.books.stream() - .filter(book -> book.title.equals(title)) - .toList(); - - if (filtered.size() < 1) { - return "item is not part of the library's collection"; - } - - return filtered.get(0).checkOut(); - } - - public String checkInNewspaper(String title) { - List filtered = this.newspapers.stream() - .filter(newspaper -> newspaper.title.equals(title)) + public String checkInItem(String title) { + List filtered = this.libraryItems.stream() + .filter(item -> item.title.equals(title)) .toList(); - if (filtered.size() < 1) { + if (filtered.isEmpty()) { return "item is not part of the library's collection"; } return filtered.get(0).checkIn(); } - public String checkOutNewspaper(String title) { - List filtered = this.newspapers.stream() - .filter(newspaper -> newspaper.title.equals(title)) + public String checkOutItem(String title) { + List filtered = this.libraryItems.stream() + .filter(item -> item.title.equals(title)) .toList(); - if (filtered.size() < 1) { + if (filtered.isEmpty()) { return "item is not part of the library's collection"; } diff --git a/src/main/java/com/booleanuk/core/Newspaper.java b/src/main/java/com/booleanuk/core/Newspaper.java index 8411d9c..74bfcbf 100644 --- a/src/main/java/com/booleanuk/core/Newspaper.java +++ b/src/main/java/com/booleanuk/core/Newspaper.java @@ -1,22 +1,13 @@ package com.booleanuk.core; -public class Newspaper { - String title; - - boolean onLoan = false; +public class Newspaper extends Item{ public Newspaper(String title) { - this.title = title; - } - - public boolean isOnLoan() { - return onLoan; + super(title); } - public String checkIn() { return "newspapers are not available for loan"; } - public String checkOut() { return "newspapers are not available for loan"; } diff --git a/src/test/java/com/booleanuk/core/ArticleTest.java b/src/test/java/com/booleanuk/core/ArticleTest.java index 00bbaeb..6033561 100644 --- a/src/test/java/com/booleanuk/core/ArticleTest.java +++ b/src/test/java/com/booleanuk/core/ArticleTest.java @@ -6,13 +6,15 @@ class ArticleTest { @Test public void shouldCheckOutIfAvailable() { - Article article = new Article("JUnit Rocks"); + Author author = new Author("Name", "email", "Home site"); + Item article = new Book("JUnit Rocks", author); Assertions.assertEquals("item has been checked out", article.checkOut()); } @Test public void shouldDeclineIfNotAvailableToCheckout() { - Article article = new Article("JUnit Rocks"); + Author author = new Author("Name", "email", "Home site"); + Item article = new Book("JUnit Rocks", author); article.checkOut(); Assertions.assertEquals("item is currently on loan", article.checkOut()); @@ -20,7 +22,8 @@ public void shouldDeclineIfNotAvailableToCheckout() { @Test public void shouldCheckInIfOnLoan() { - Article article = new Article("JUnit Rocks"); + Author author = new Author("Name", "email", "Home site"); + Item article = new Book("JUnit Rocks", author); article.checkOut(); Assertions.assertEquals("item has been checked in", article.checkIn()); @@ -28,7 +31,8 @@ public void shouldCheckInIfOnLoan() { @Test public void shouldDeclineCheckInIfNotOnLoan() { - Article article = new Article("JUnit Rocks"); + Author author = new Author("Name", "email", "Home site"); + Item article = new Book("JUnit Rocks", author); Assertions.assertEquals("item is not currently on loan", article.checkIn()); } diff --git a/src/test/java/com/booleanuk/core/BookTest.java b/src/test/java/com/booleanuk/core/BookTest.java index 180a165..fd0b1ec 100644 --- a/src/test/java/com/booleanuk/core/BookTest.java +++ b/src/test/java/com/booleanuk/core/BookTest.java @@ -6,13 +6,15 @@ public class BookTest { @Test public void shouldCheckOutIfAvailable() { - Book book = new Book("JUnit Rocks"); + Author author = new Author("Name", "email", "Home site"); + Item book = new Book("JUnit Rocks", author); Assertions.assertEquals("item has been checked out", book.checkOut()); } @Test public void shouldDeclineIfNotAvailableToCheckout() { - Book book = new Book("JUnit Rocks"); + Author author = new Author("Name", "email", "Home site"); + Item book = new Book("JUnit Rocks", author); book.checkOut(); Assertions.assertEquals("item is currently on loan", book.checkOut()); @@ -20,7 +22,8 @@ public void shouldDeclineIfNotAvailableToCheckout() { @Test public void shouldCheckInIfOnLoan() { - Book book = new Book("JUnit Rocks"); + Author author = new Author("Name", "email", "Home site"); + Item book = new Book("JUnit Rocks", author); book.checkOut(); Assertions.assertEquals("item has been checked in", book.checkIn()); @@ -28,7 +31,8 @@ public void shouldCheckInIfOnLoan() { @Test public void shouldDeclineCheckInIfNotOnLoan() { - Book book = new Book("JUnit Rocks"); + Author author = new Author("Name", "email", "Home site"); + Item book = new Book("JUnit Rocks", author); Assertions.assertEquals("item is not currently on loan", book.checkIn()); } diff --git a/src/test/java/com/booleanuk/core/ItemTest.java b/src/test/java/com/booleanuk/core/ItemTest.java new file mode 100644 index 0000000..f785a06 --- /dev/null +++ b/src/test/java/com/booleanuk/core/ItemTest.java @@ -0,0 +1,29 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ItemTest { + @Test + public void testCheckOutOK(){ + Item testItem = new Item("testItem"); + Assertions.assertEquals("item has been checked out", testItem.checkOut()); + } + @Test + public void testCheckOutNotOK(){ + Item testItem = new Item("testItem"); + testItem.checkOut(); + Assertions.assertEquals("item is currently on loan", testItem.checkOut()); + } + @Test + public void testCheckInOK() { + Item testItem = new Item("testItem"); + Assertions.assertEquals("item is not currently on loan", testItem.checkIn()); + } + @Test + public void testCheckInNotOK() { + Item testItem = new Item("testItem"); + testItem.checkOut(); + Assertions.assertEquals("item has been checked in", testItem.checkIn()); + } +} diff --git a/src/test/java/com/booleanuk/core/LibraryTest.java b/src/test/java/com/booleanuk/core/LibraryTest.java new file mode 100644 index 0000000..84d77b1 --- /dev/null +++ b/src/test/java/com/booleanuk/core/LibraryTest.java @@ -0,0 +1,26 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class LibraryTest { + @Test + public void testAddItem() { + Library library = new Library(); + Author author = new Author("Name", "email", "Home site"); + Item book = new Book("Sovjietistan", author); + library.addToStock(book); + Assertions.assertEquals("item is not currently on loan", library.checkInItem("Sovjietistan")); + Assertions.assertEquals("item is not part of the library's collection", library.checkInItem("NoTitle")); + } + @Test + public void testCheckOut() { + Library library = new Library(); + Author author = new Author("Name", "email", "Home site"); + Item book = new Book("Sovjietistan", author); + library.addToStock(book); + Assertions.assertEquals("item has been checked out", library.checkOutItem("Sovjietistan")); + Assertions.assertEquals("item is not part of the library's collection", library.checkOutItem("NoTitle")); + } + +} diff --git a/src/test/java/com/booleanuk/core/NewspaperTest.java b/src/test/java/com/booleanuk/core/NewspaperTest.java index 38195af..6e63ed3 100644 --- a/src/test/java/com/booleanuk/core/NewspaperTest.java +++ b/src/test/java/com/booleanuk/core/NewspaperTest.java @@ -6,13 +6,13 @@ public class NewspaperTest { @Test public void shouldBeUnavailableForCheckIn() { - Newspaper newspaper = new Newspaper("The Daily Java"); + Item newspaper = new Newspaper("The Daily Java"); Assertions.assertEquals("newspapers are not available for loan", newspaper.checkIn()); } @Test public void shouldBeUnavailableForCheckOut() { - Newspaper newspaper = new Newspaper("The Daily Java"); + Item newspaper = new Newspaper("The Daily Java"); Assertions.assertEquals("newspapers are not available for loan", newspaper.checkOut()); } }