Skip to content

Osamah Al-maliki #53

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
35 changes: 11 additions & 24 deletions src/main/java/com/booleanuk/core/Article.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
package com.booleanuk.core;

public class Article {
String title;

boolean onLoan = false;
public class Article extends ReadingMaterial {
Author author;

public Article(String title) {
this.title = title;
}

public boolean isOnLoan() {
return onLoan;
super(title);
}

public String checkIn() {
if (!this.isOnLoan()) {
return "item is not currently on loan";
}

this.onLoan = false;

return "item has been checked in";
public Article(String title, Author author) {
super(title);
this.author = author;
}

public String checkOut() {
if (this.isOnLoan()) {
return "item is currently on loan";
public String getAuthorName() {
if (author != null) {
return "The author is " + author.getName();
} else {
return "This reading material has no author";
}

this.onLoan = true;

return "item has been checked out";
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/booleanuk/core/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.booleanuk.core;

public class Author {
String name;
String email;
String website;

public Author(String name, String email, String website) {
this.name = name;
this.email = email;
this.website = website;
}

public String getName() {
return this.name;
}
}
35 changes: 11 additions & 24 deletions src/main/java/com/booleanuk/core/Book.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
package com.booleanuk.core;

public class Book {
String title;

boolean onLoan = false;
public class Book extends ReadingMaterial {
Author author;

public Book(String title) {
this.title = title;
}

public boolean isOnLoan() {
return onLoan;
super(title);
}

public String checkIn() {
if (!this.isOnLoan()) {
return "item is not currently on loan";
}

this.onLoan = false;

return "item has been checked in";
public Book(String title, Author author) {
super(title);
this.author = author;
}

public String checkOut() {
if (this.isOnLoan()) {
return "item is currently on loan";
public String getName() {
if (author != null) {
return "The author is " + author.getName();
} else {
return "This reading material has no author";
}

this.onLoan = true;

return "item has been checked out";
}
}
81 changes: 12 additions & 69 deletions src/main/java/com/booleanuk/core/Library.java
Original file line number Diff line number Diff line change
@@ -1,92 +1,35 @@
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<ReadingMaterial> type = new ArrayList<>();

public void addToStock(Article item) {
this.articles.add(item);
}

public void addToStock(Book item) {
this.books.add(item);
}

public void addToStock(Newspaper item) {
this.newspapers.add(item);
public void addToStock(ReadingMaterial item) {
this.type.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 checkInMaterial(ReadingMaterial material) {
List<ReadingMaterial> filtered = this.type.stream()
.filter(type -> type.title.equals(material.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 checkOutMaterial(ReadingMaterial material) {
List<ReadingMaterial> filtered = this.type.stream()
.filter(type -> type.title.equals(material.title))
.toList();

if (filtered.size() < 1) {
if (filtered.isEmpty()) {
return "item is not part of the library's collection";
}

Expand Down
13 changes: 4 additions & 9 deletions src/main/java/com/booleanuk/core/Newspaper.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package com.booleanuk.core;

public class Newspaper {
String title;

boolean onLoan = false;
public class Newspaper extends ReadingMaterial {

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";
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/booleanuk/core/ReadingMaterial.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.booleanuk.core;

public class ReadingMaterial {
String title;

boolean onLoan = false;

public ReadingMaterial(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 String checkOut() {
if (this.isOnLoan()) {
return "item is currently on loan";
}

this.onLoan = true;

return "item has been checked out";
}
}
3 changes: 2 additions & 1 deletion src/test/java/com/booleanuk/core/ArticleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
class ArticleTest {
@Test
public void shouldCheckOutIfAvailable() {
Article article = new Article("JUnit Rocks");
Author Osamah = new Author("Osamah Al-maliki", "[email protected]", "osamah.com");
Article article = new Article("JUnit Rocks", Osamah);
Assertions.assertEquals("item has been checked out", article.checkOut());
}

Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/booleanuk/core/AuthorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.booleanuk.core;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class AuthorTest {
@Test
public void shouldReturnAuthorsName() {
Author Osamah = new Author("Osamah Al-maliki", "[email protected]", "osamah.com");
Article article = new Article("JUnit Rocks", Osamah);
Assertions.assertEquals("The author is Osamah Al-maliki", article.getAuthorName());
}

@Test
public void shouldDeclineIfNotAvailableToCheckout() {
Article article = new Article("JUnit Rocks");

Assertions.assertEquals("This reading material has no author", article.getAuthorName());
}
}
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 Osamah = new Author("Osamah Al-maliki", "[email protected]", "osamah.com");
Book book = new Book("JUnit Rocks", Osamah);
Assertions.assertEquals("item has been checked out", book.checkOut());
}

@Test
public void shouldDeclineIfNotAvailableToCheckout() {
Book book = new Book("JUnit Rocks");
Author Dave = new Author("David Ames", "[email protected]", "dave.com");
Book book = new Book("JUnit Rocks", Dave);
book.checkOut();

Assertions.assertEquals("item is currently on loan", book.checkOut());
}

@Test
public void shouldCheckInIfOnLoan() {
Book book = new Book("JUnit Rocks");
Author JK = new Author("JK Rowling", "[email protected]", "jk.com");
Book book = new Book("JUnit Rocks", JK);
book.checkOut();

Assertions.assertEquals("item has been checked in", book.checkIn());
}

@Test
public void shouldDeclineCheckInIfNotOnLoan() {
Book book = new Book("JUnit Rocks");
Author Arjela = new Author("Arjela Hamiti", "[email protected]", "ah.com");
Book book = new Book("JUnit Rocks", Arjela);

Assertions.assertEquals("item is not currently on loan", book.checkIn());
}
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/com/booleanuk/core/LibraryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.booleanuk.core;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class LibraryTest {
@Test
public void testIfMaterialGetsAddedToStock() {
Library library = new Library();
Book book = new Book("Harry Potter");

Assertions.assertEquals(0, library.type.size());

library.addToStock(book);

Assertions.assertEquals(1, library.type.size());
}

@Test
public void testingCheckInAndCheckOutMethods() {
Library library = new Library();
Book book = new Book("Harry Potter");

Assertions.assertEquals(0, library.type.size());

library.addToStock(book);

Assertions.assertEquals(1, library.type.size());

Assertions.assertEquals(false, book.isOnLoan());

Assertions.assertEquals("item has been checked out", library.checkOutMaterial(book));

Assertions.assertEquals(true, book.isOnLoan());

Assertions.assertEquals("item has been checked in", library.checkInMaterial(book));

Assertions.assertEquals(false, book.isOnLoan());
}
}