Skip to content

Petter Karstensen #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 7 additions & 29 deletions src/main/java/com/booleanuk/core/Article.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/booleanuk/core/Author.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
35 changes: 7 additions & 28 deletions src/main/java/com/booleanuk/core/Book.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/booleanuk/core/Item.java
Original file line number Diff line number Diff line change
@@ -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";
}
}
84 changes: 14 additions & 70 deletions src/main/java/com/booleanuk/core/Library.java
Original file line number Diff line number Diff line change
@@ -1,92 +1,36 @@
package com.booleanuk.core;

import java.util.ArrayList;
import java.util.List;

public class Library {
List<Article> articles;
List<Book> books;
List<Newspaper> newspapers;
List<Item> 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<Article> 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<Article> 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<Book> 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<Book> 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<Newspaper> filtered = this.newspapers.stream()
.filter(newspaper -> newspaper.title.equals(title))
public String checkInItem(String title) {
List<Item> 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<Newspaper> filtered = this.newspapers.stream()
.filter(newspaper -> newspaper.title.equals(title))
public String checkOutItem(String title) {
List<Item> 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";
}

Expand Down
13 changes: 2 additions & 11 deletions src/main/java/com/booleanuk/core/Newspaper.java
Original file line number Diff line number Diff line change
@@ -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";
}
Expand Down
12 changes: 8 additions & 4 deletions src/test/java/com/booleanuk/core/ArticleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,33 @@
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());
}

@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());
}

@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());
}
Expand Down
12 changes: 8 additions & 4 deletions src/test/java/com/booleanuk/core/BookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,33 @@
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());
}

@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());
}

@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());
}
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/com/booleanuk/core/ItemTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading