diff --git a/src/main/java/com/booleanuk/core/Article.java b/src/main/java/com/booleanuk/core/Article.java index 1e359b0..6ffbe52 100644 --- a/src/main/java/com/booleanuk/core/Article.java +++ b/src/main/java/com/booleanuk/core/Article.java @@ -1,35 +1,30 @@ package com.booleanuk.core; -public class Article { - String title; +public class Article extends Items{ - boolean onLoan = false; + + Author author; public Article(String title) { - this.title = title; - } + super(title); - public boolean isOnLoan() { - return onLoan; + } + public Article(String title, Author author) { + super(title); + this.author = author; } - public String checkIn() { - if (!this.isOnLoan()) { - return "item is not currently on loan"; - } + public String getAuthorName() { - this.onLoan = false; + if (author != null) { + return author.getName(); + } - return "item has been checked in"; + return "We have nothing"; } - public String checkOut() { - if (this.isOnLoan()) { - return "item is currently on loan"; - } - this.onLoan = true; - return "item has been checked out"; - } + + } 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..8bba2e4 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Author.java @@ -0,0 +1,29 @@ +package com.booleanuk.core; + +public class Author { + private String name; + private String website; + private String phonenumber; + + public Author(String name, String website, String phonenumber){ + this.name = name; + this.website = website; + this.phonenumber = phonenumber; + + + } + + public String getName() { + return name; + } + + public String getWebsite() { + return website; + } + + public String getPhonenumber() { + return phonenumber; + } +} + + diff --git a/src/main/java/com/booleanuk/core/Book.java b/src/main/java/com/booleanuk/core/Book.java index 9261f65..805f024 100644 --- a/src/main/java/com/booleanuk/core/Book.java +++ b/src/main/java/com/booleanuk/core/Book.java @@ -1,35 +1,30 @@ package com.booleanuk.core; -public class Book { - String title; +public class Book extends Items{ - boolean onLoan = false; + + Author author; public Book(String title) { - this.title = title; - } + super(title); - public boolean isOnLoan() { - return onLoan; + } + public Book(String title, Author author) { + super(title); + this.author = author; } - public String checkIn() { - if (!this.isOnLoan()) { - return "item is not currently on loan"; - } + public String getAuthorName() { - this.onLoan = false; + if (author != null) { + return author.getName(); + } - return "item has been checked in"; + return "We have nothing"; } - public String checkOut() { - if (this.isOnLoan()) { - return "item is currently on loan"; - } - this.onLoan = true; - return "item has been checked out"; - } + + } diff --git a/src/main/java/com/booleanuk/core/Items.java b/src/main/java/com/booleanuk/core/Items.java new file mode 100644 index 0000000..92a4d4d --- /dev/null +++ b/src/main/java/com/booleanuk/core/Items.java @@ -0,0 +1,38 @@ +package com.booleanuk.core; + +public class Items { + String title; + String authorName; + boolean onLoan = false; + + public Items(String title) { + this.title = title; + } + + public boolean isOnLoan() { + return onLoan; + } + public String getAuthorName() { + return authorName; + } + + 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"; + } +} diff --git a/src/main/java/com/booleanuk/core/Library.java b/src/main/java/com/booleanuk/core/Library.java index f03ecf0..bb7216d 100644 --- a/src/main/java/com/booleanuk/core/Library.java +++ b/src/main/java/com/booleanuk/core/Library.java @@ -1,53 +1,29 @@ package com.booleanuk.core; +import java.util.ArrayList; import java.util.List; public class Library { List
articles; List books; List newspapers; + List items; - public void addToStock(Article item) { - this.articles.add(item); - } - public void addToStock(Book item) { - this.books.add(item); - } + public Library() { + this.articles = new ArrayList<>(); + this.books = new ArrayList<>(); + this.newspapers = new ArrayList<>(); + this.items = new ArrayList<>(); - 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 void addToStock(Items item) { + this.items.add(item); } - 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)) + public String checkInItem(String title) { + List filtered = this.items.stream() + .filter(items -> items.title.equals(title)) .toList(); if (filtered.size() < 1) { @@ -57,9 +33,9 @@ public String checkInBook(String title) { return filtered.get(0).checkIn(); } - public String checkOutBook(String title) { - List filtered = this.books.stream() - .filter(book -> book.title.equals(title)) + public String checkOutItem(String title) { + List filtered = this.items.stream() + .filter(items -> items.title.equals(title)) .toList(); if (filtered.size() < 1) { @@ -69,27 +45,13 @@ public String checkOutBook(String title) { return filtered.get(0).checkOut(); } - public String checkInNewspaper(String title) { - List filtered = this.newspapers.stream() - .filter(newspaper -> newspaper.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 checkOutNewspaper(String title) { - List filtered = this.newspapers.stream() - .filter(newspaper -> newspaper.title.equals(title)) - .toList(); + public String getAuthor(String name) { - if (filtered.size() < 1) { - return "item is not part of the library's collection"; + for (Items item : items) { + if (item.equals(name)) { + return name; + } } - - return filtered.get(0).checkOut(); + return ""; } } diff --git a/src/main/java/com/booleanuk/core/Newspaper.java b/src/main/java/com/booleanuk/core/Newspaper.java index 8411d9c..4111a10 100644 --- a/src/main/java/com/booleanuk/core/Newspaper.java +++ b/src/main/java/com/booleanuk/core/Newspaper.java @@ -1,23 +1,24 @@ package com.booleanuk.core; -public class Newspaper { - String title; +public class Newspaper extends Items{ - boolean onLoan = false; public Newspaper(String title) { - this.title = title; - } - public boolean isOnLoan() { - return onLoan; + super(title); } + @Override public String checkIn() { return "newspapers are not available for loan"; } + + @Override public String checkOut() { return "newspapers are not available for loan"; } + + + } 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..c269b3f --- /dev/null +++ b/src/test/java/com/booleanuk/core/LibraryTest.java @@ -0,0 +1,151 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class LibraryTest { + + @Test + public void testCheckInArticle() { + Article article = new Article("Here"); + Library check = new Library(); + check.addToStock(article); + article.checkOut(); + + + String result = check.checkInItem("Here"); + + Assertions.assertEquals("item has been checked in", result); + + } + @Test + public void testCheckOutArticle() { + Article article = new Article("Here"); + Library check = new Library(); + check.addToStock(article); + article.checkIn(); + + + String result = check.checkOutItem("Here"); + + Assertions.assertEquals("item has been checked out", result); + + + } + @Test + public void testCheckInBook() { + Book book = new Book("Here"); + Library check = new Library(); + check.addToStock(book); + book.checkOut(); + + + String result = check.checkInItem("Here"); + + Assertions.assertEquals("item has been checked in", result); + + } + @Test + public void testCheckOutBook() { + Book book = new Book("Here"); + Library check = new Library(); + check.addToStock(book); + book.checkIn(); + + + String result = check.checkOutItem("Here"); + + Assertions.assertEquals("item has been checked out", result); + + } + @Test + public void testCheckInNewspaper() { + Newspaper newspaper = new Newspaper("Here"); + Library check = new Library(); + check.addToStock(newspaper); + newspaper.checkOut(); + + + String result = check.checkInItem("Here"); + + Assertions.assertEquals("newspapers are not available for loan", result); + + } + @Test + public void testCheckOutNewspaper() { + Newspaper newspaper = new Newspaper("Here"); + Library check = new Library(); + check.addToStock(newspaper); + newspaper.checkIn(); + + + String result = check.checkOutItem("Here"); + + Assertions.assertEquals("newspapers are not available for loan", result); + + + } + + @Test + public void testCheckAuthorBook() { + Author hassan = new Author("Hassan", "Elbrus", "343434"); + Book book = new Book("Here", hassan); + Library check = new Library(); + check.addToStock(book); + + + String result = book.getAuthorName(); + + Assertions.assertEquals("Hassan", result); + + + } + + + @Test + public void testCheckBookWithNoAuthor() { + Author hassan = new Author("Hassan", "Elbrus", "343434"); + Book book = new Book("Here"); + Library check = new Library(); + check.addToStock(book); + + + String result = book.getAuthorName(); + + Assertions.assertEquals("We have nothing", result); + + + } + + @Test + public void testCheckArticleWithAuthor() { + Author hassan = new Author("Hassan", "Elbrus", "343434"); + Article article = new Article("Here", hassan); + Library check = new Library(); + check.addToStock(article); + + + String result = article.getAuthorName(); + + Assertions.assertEquals("Hassan", result); + + + } + @Test + public void testCheckArticleWithNoAuthor() { + Author hassan = new Author("Hassan", "Elbrus", "343434"); + Article article = new Article("Here"); + Library check = new Library(); + check.addToStock(article); + + + String result = article.getAuthorName(); + + Assertions.assertEquals("We have nothing", result); + + + } + + + +}